我有个问题。我创造了一个游戏。当我打开它时,我必须按ENTER键才能开始游戏(只需按enter键)。现在我用一个名为“退出游戏”的按钮升级了游戏。我不知道为什么我的enter键因为这个按钮不再起作用了。如果我删除了它,然后我可以再次按enter键并玩游戏。
我必须为那个按钮或类似的东西设置只点击按下事件?请帮帮我。
public class LeftPanel extends JPanel implements ActionListener {
JButton ExitGame;
public LeftPanel(Tetris tetris) {
this.tetris = tetris;
setPreferredSize(new Dimension(400, 480));
setBackground(Color.getHSBColor(17f, 0.87f, 0.52f));
add(new JButton("Exit Game"));
{
ExitGame.addActionListener(this);
}
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}发布于 2013-05-29 09:14:29
您可以尝试:
public class LeftPanel extends JPanel implements ActionListener {
public LeftPanel(Tetris tetris) {
this.tetris = tetris;
setPreferredSize(new Dimension(400, 480));
setBackground(Color.getHSBColor(17f, 0.87f, 0.52f));
JButton ExitGame = new JButton("Exit Game");
ExitGame.addActionListener(this);
ExitGame.setActionCommand("Exit");
add(ExitGame );
}
public void actionPerformed(ActionEvent e) {
if("Exit".equals(e.getActionCommand())
System.exit(0);
}
}发布于 2013-05-29 09:14:38
这一行看起来像一个语法错误:
add(new JButton("Exit Game"));
{
ExitGame.addActionListener(this);
}我认为应该是这样的:
ExitGame= new JButton("Exit");
this.add(ExitGame);
ExitGame.addActionListener(this);我还没有测试过这一点,但我认为通过一些调整,你应该能够让它做你想要的事情。我希望这能起作用!
-Frank
发布于 2014-09-08 17:02:18
public void actionPerformed(ActionEvent e) {
if("Exit".equals(e.getActionCommand())System.exit(0);}
https://stackoverflow.com/questions/16803851
复制相似问题