我正在尝试使用Swing在Java中制作一个内存游戏。但是我被困住了,因为我不知道如何检查是否设置了previousButton变量。
我尝试检查文本是否为该变量的空白,但是它给了我NullPointerException。我知道为什么了,因为它正在尝试检查不存在的JButton的文本。
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == gameView.button1) {
currentButton = gameView.button1;
gameView.setFrontOfCard(gameView.button1);
} else if (e.getSource() == gameView.button2) {
currentButton = gameView.button2;
gameView.setFrontOfCard(gameView.button2);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button3) {
currentButton = gameView.button3;
gameView.setFrontOfCard(gameView.button3);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button4) {
currentButton = gameView.button4;
gameView.setFrontOfCard(gameView.button4);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button5) {
currentButton = gameView.button5;
gameView.setFrontOfCard(gameView.button5);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button6) {
currentButton = gameView.button6;
gameView.setFrontOfCard(gameView.button6);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button7) {
currentButton = gameView.button7;
gameView.setFrontOfCard(gameView.button7);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button8) {
currentButton = gameView.button8;
gameView.setFrontOfCard(gameView.button8);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button9) {
currentButton = gameView.button9;
gameView.setFrontOfCard(gameView.button9);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button10) {
currentButton = gameView.button10;
gameView.setFrontOfCard(gameView.button10);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button11) {
currentButton = gameView.button11;
gameView.setFrontOfCard(gameView.button11);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button12) {
currentButton = gameView.button12;
gameView.setFrontOfCard(gameView.button12);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button13) {
currentButton = gameView.button13;
gameView.setFrontOfCard(gameView.button13);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button14) {
currentButton = gameView.button14;
gameView.setFrontOfCard(gameView.button14);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button15) {
currentButton = gameView.button15;
gameView.setFrontOfCard(gameView.button15);
System.out.println(e.getActionCommand());
} else if (e.getSource() == gameView.button16) {
currentButton = gameView.button16;
gameView.setFrontOfCard(gameView.button16);
System.out.println(e.getActionCommand());
}
if (currentButton == previousButton) {
gameView.setScoreLabel(gameModel.incrementScore());
} else if (!previousButton.getText().isEmpty()) {
gameView.setBackOfCard(currentButton);
gameView.setBackOfCard(previousButton);
}
previousButton = currentButton;
}
}
我希望它检查前一个按钮是否不为空,因此我可以“转动”卡。但这给了我NullPointerException。