意図したとおりに動作しないActionListenersに問題があります。ここでは、それらのコードは次のとおりです。 Java ActionListenerがJMenuItemで動作しない
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GameOfLife extends JFrame implements ActionListener
{
Timer timer = new Timer(700, this);
Table world;
JMenuBar menuBar;
JMenu gameMode;
JMenu actions;
JMenuItem custom, demo, random, start, pause, save, load;
public GameOfLife(int width, int height)
{
super();
world = new Table(width, height);
CreateMenu();
this.setContentPane(world);
this.setJMenuBar(menuBar);
this.setPreferredSize(new Dimension(1200, 900));
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
StartRandom();
}
private void CreateMenu()
{
menuBar = new JMenuBar();
gameMode = new JMenu("Game Mode");
actions = new JMenu("Actions");
custom = new JMenuItem("Custom Game");
custom.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
StartCustom();
}
});
gameMode.add(custom);
demo = new JMenuItem("Demo Game");
demo.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
StartDemo();
}
});
gameMode.add(demo);
random = new JMenuItem("Random Game");
random.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
StartRandom();
}
});
gameMode.add(random);
menuBar.add(gameMode);
}
private void Demo()
{
int[] x =
{
5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 12, 12, 12, 12, 12,
12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 17, 17, 17, 17, 17, 17
};
int[] y =
{
7, 8, 9, 13, 14, 15, 5, 10, 12, 17, 5, 10, 12, 17, 5, 10, 12, 17, 7, 8, 9, 13, 14, 15, 7, 8, 9,
13, 14, 15, 5, 10, 12, 17, 5, 10, 12, 17, 5, 10, 12, 17, 7, 8, 9, 13, 14, 15
};
int i = 0;
while (i < x.length)
{
world.SetStartPosition(x[i], y[i++]);
}
}
private void StartCustom()
{
// TO-DO
}
private void StartDemo()
{
Demo();
Game();
}
private void StartRandom()
{
world.RandomTable();
Game();
}
private void Game()
{
while (world.CountAliveCells() > 0)
{
timer.start();
}
}
public static void main(String[] args) {
new GameOfLife(20,20);
}
@Override
public void actionPerformed(ActionEvent e) {
world.UpdateCellNeighbors();
world.UpdateTable();
}
}
私はゲームモードメニュー、アプリケーションがフリーズからのメニュー項目のいずれかを押すと、私は他に何もなく、ただEclipseの停止ボタンからそれを止めることはできない
。私も addMouseListenerで試してみましたが、コンソールでの書き込みのみで動作します。 StartDemoと StartRandomメソッドは、クラスコンストラクタで呼び出された場合に機能しますが、アクションリスナメソッドで呼び出された場合にはアプリケーションをフリーズします。また、文字通り何もしない StartCustomメソッドでもアプリケーションがフリーズします。編集: 私はSwing TimerでThread.sleep関数を変更しましたが、問題はまだ同じです。メニューボタンからゲームモードを選択しようとすると、アプリケーションがフリーズしますが、クラスコンストラクターからStartDemoまたはStartRandomメソッドが呼び出されたときは、完全に機能します。
作り、EDTのコンテキスト内で登録
ActionListener
SactionPerformed
方法をトリガー:あなたは 'StartCustom'と'呼び出すcustom' 'にActionListener's'に追加しましたStartDemo'、おそらくあなたが意図したものではない – MadProgrammerメソッド名は大文字で始めるべきではありません。 Java APIからメソッドを見せてください。 Javaの規約に従ってください。 – camickr