【发布时间】:2019-11-27 14:48:44
【问题描述】:
我已经研究了好几个小时的代码,但找不到任何适合我的代码的解决方案 当我点击我的 Water 、 Fire 或 Nature 按钮时,游戏 GUI 不应该再显示了,因为每个按钮都会添加 1 存储到我的游戏播放中,最多只能存储 5 次,我真的想不通这个出来请帮帮我!真的很感激。
public class Game {
private JPanel Game;
private JButton Water;
private JButton Nature;
private JButton Fire;
private int myChoice;
private int computerChoice;
int gamePlayed = 0; //store the times of game played
public Game() {
JFrame frame = new JFrame("The Elements");
frame.setContentPane(this.Game);
frame.setMinimumSize(new Dimension(500, 500));
frame.pack();
frame.setVisible(true);
if (gamePlayed <= 5) { //if the times of game played is less than 5 times , execute the following code
Water.addActionListener(new ActionListener() {
@Override
//Water counter Fire , Nature counter water
public void actionPerformed(ActionEvent e) {
int select;
select = JOptionPane.showConfirmDialog(null,
"You're using the power of water", "Water",
JOptionPane.YES_NO_OPTION);
if (select == JOptionPane.YES_OPTION) {
myChoice = 0;
computerChoice = computerPlays();
conditionsDisplayResults(myChoice, computerChoice);
gamePlayed+=1;//add 1 time of played game when the yes option clicked
}
}
});
Fire.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int select = JOptionPane.showConfirmDialog(null,
"You're using the power of fire", "Fire",
JOptionPane.YES_NO_OPTION);
if (select == JOptionPane.YES_OPTION) {
myChoice = 1;
computerChoice = computerPlays();
conditionsDisplayResults(myChoice, computerChoice);
gamePlayed+=1;//add 1 time of played game when the yes option clicked
}
}
});
Nature.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int select = JOptionPane.showConfirmDialog(null,
"You're using the power of nature", "Nature",
JOptionPane.YES_NO_OPTION);
if (select == JOptionPane.YES_OPTION) {
myChoice = 2;
computerChoice = computerPlays();
conditionsDisplayResults(myChoice, computerChoice);
gamePlayed+=1;//add 1 time of played game when the yes option clicked
}
}
});
else{ //else which when the times of game played is more than 5 times , execute the following code
GameResult();
MainMenu mainMenu = new MainMenu();
}
}
【问题讨论】:
-
为什么投反对票?怀疑是“请调试我的代码?” @ShangHuang,这个问题真的不清楚。有什么错误?你熟悉debugging吗?
-
对不起,我刚刚意识到我提出了一个错误的问题标题。谢谢你给我建议!我会记住这一点! :D
-
我试图编辑标题,现在可以吗?
标签: java swing user-interface