【发布时间】:2017-06-11 00:24:33
【问题描述】:
我遇到了一些无法按预期工作的 ActionListener 的问题。这是他们的代码:
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();
}
}
当我按下 gameMode 菜单中的一个菜单项时,应用程序会冻结,我无法执行任何其他操作,只能通过 Eclipse 停止按钮将其停止。我也尝试使用 addMouseListener 但它仅适用于在控制台中编写,它不会运行预期的方法。我还必须提到,如果在类构造函数中调用 StartDemo 和 StartRandom 方法可以工作,但如果在动作侦听器方法中调用它们只会冻结应用程序。此外,即使对于实际上什么都不做的 StartCustom 方法,应用程序也会冻结。
编辑: 我用 Swing Timer 更改了 Thread.sleep 函数,问题还是一样。当我尝试从菜单按钮选择游戏模式时,应用程序仍然冻结,但当从类构造函数调用 StartDemo 或 StartRandom 方法时,它可以正常工作。
【问题讨论】:
-
仅供参考:您已将
ActionListeners 添加到custom,其中调用StartCustom和StartDemo,可能不是您想要的 -
方法名称不应以大写字符开头。向我展示一个来自 Java API 的方法。遵循 Java 约定。
标签: java swing actionlistener menuitem