我有个问题。我创造了一个游戏。当我打开它时,我必须按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);
}
}https://stackoverflow.com/questions/16803851
复制相似问题