Java 在GUI中使用按钮增加for循环
我和一个朋友正在为一个大学项目制作一个基于GUI的数学游戏。我们被困在一个特定的点上。我们有两个方程式显示在屏幕上,用户可以选择第一个方程式是更小、更大还是等于第二个方程式。我们希望这种情况发生10次。因此,每当用户选择3个选项中的一个时,必须显示下两个等式。在第一次迭代中,将显示两个方程,您可以选择一个答案。但完成后,方程保持不变,并且您可以无限期地单击3个按钮中的任何一个,而不会更改方程或在按下10个按钮后循环结束。有三种不同的游戏可供选择,它们在第一个语句中被选中。这两条语句在主GUI方法中运行(其中声明并创建了所有按钮): 这些发生在按钮处理程序和计算这两个方程的额外方法中Java 在GUI中使用按钮增加for循环,java,loops,user-interface,Java,Loops,User Interface,我和一个朋友正在为一个大学项目制作一个基于GUI的数学游戏。我们被困在一个特定的点上。我们有两个方程式显示在屏幕上,用户可以选择第一个方程式是更小、更大还是等于第二个方程式。我们希望这种情况发生10次。因此,每当用户选择3个选项中的一个时,必须显示下两个等式。在第一次迭代中,将显示两个方程,您可以选择一个答案。但完成后,方程保持不变,并且您可以无限期地单击3个按钮中的任何一个,而不会更改方程或在按下10个按钮后循环结束。有三种不同的游戏可供选择,它们在第一个语句中被选中。这两条语句在主GUI方法
private class ButtonHandler extends JFrame implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == backButton)
{
gameScreenFrame.dispose();
GameSelectGUI gameSelectGUI = new GameSelectGUI();
gameSelectGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameSelectGUI.setSize(gameScreenFrame.getSize());
gameSelectGUI.setLocation(gameScreenFrame.getLocation());
gameSelectGUI.setVisible(true);
}
while(gameCount < 10)
{
if(event.getSource() == smallButton)
{
// gameScreenFrame.dispose();
// ResultsGUI resultsGUI = new ResultsGUI();
// resultsGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// resultsGUI.setSize(gameScreenFrame.getSize());
// resultsGUI.setLocation(gameScreenFrame.getLocation());
// resultsGUI.setVisible(true);
calcEquations();
gameCount++;
if(eq1 < eq2)
{
System.out.println("Smaller Correct");
}
}
if(event.getSource() == equalButton)
{
// gameScreenFrame.dispose();
// ResultsGUI resultsGUI = new ResultsGUI();
// resultsGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// resultsGUI.setSize(gameScreenFrame.getSize());
// resultsGUI.setLocation(gameScreenFrame.getLocation());
// resultsGUI.setVisible(true);
calcEquations();
gameCount++;
if(eq1 == eq2)
{
System.out.println("Equal Correct");
}
}
if(event.getSource() == largeButton)
{
// gameScreenFrame.dispose();
// ResultsGUI resultsGUI = new ResultsGUI();
// resultsGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// resultsGUI.setSize(gameScreenFrame.getSize());
// resultsGUI.setLocation(gameScreenFrame.getLocation());
// resultsGUI.setVisible(true);
calcEquations();
gameCount++;
if(eq1 > eq2)
{
System.out.println("Larger Correct");
}
}
}
}
}
public void calcEquations()
{
if(game == 'A')
{
AccuracyGame objGame = new AccuracyGame(gameLvl, gameType);
eq1 = objGame.Equation();
eq1Str = objGame.toString();
eq2 = objGame.Equation();
eq2Str = objGame.toString();
eqOneLabel.setText(eq1Str);
eqTwoLabel.setText(eq2Str);
}
}
私有类ButtonHandler扩展JFrame实现ActionListener
{
已执行的公共无效操作(操作事件)
{
if(event.getSource()==backButton)
{
gameScreenFrame.dispose();
GameSelectGUI GameSelectGUI=新建GameSelectGUI();
gameSelectGUI.setDefaultCloseOperation(JFrame.EXIT\u ON\u CLOSE);
gameSelectGUI.setSize(gameScreenFrame.getSize());
gameSelectGUI.setLocation(gameScreenFrame.getLocation());
gameSelectGUI.setVisible(true);
}
而(游戏数<10)
{
if(event.getSource()==smallButton)
{
//gameScreenFrame.dispose();
//ResultsGUI ResultsGUI=新的ResultsGUI();
//resultsGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//resultsGUI.setSize(gameScreenFrame.getSize());
//resultsGUI.setLocation(gameScreenFrame.getLocation());
//resultsGUI.setVisible(true);
计算公式();
gameCount++;
if(eq1eq2)
{
System.out.println(“较大的正确值”);
}
}
}
}
}
公共无效计算公式()
{
如果(游戏=='A')
{
AccuracyGame objGame=新的AccuracyGame(游戏级别,游戏类型);
eq1=对象名称。方程();
eq1Str=objGame.toString();
eq2=objGame.Equation();
eq2Str=objGame.toString();
eqOneLabel.setText(eq1Str);
eqTwoLabel.setText(eq2Str);
}
}
它允许您回答一次,然后显示一组新的方程式,但在这一点上,三个按钮都不起作用。我们在按钮中添加了一个输出,以在控制台中显示发生了什么,它显示按下按钮一次即可回答所有10个问题。有没有办法阻止程序一次运行全部10个,并允许我们单击一次来回答每组方程式?当前,您的
计算公式
方法将从while循环调用,直到游戏计数
为10为止。删除while循环以调用并仅运行一次。例如:
...
if(gameCount < 10)
{
if(event.getSource() == smallButton)
...
。。。
如果(游戏计数<10)
{
if(event.getSource()==smallButton)
...
您的循环位于操作的处理程序内,因此只需按下一个按钮即可完成。将循环从操作处理程序中取出。您只想增加游戏计数,但不想循环。
...
if(gameCount < 10)
{
if(event.getSource() == smallButton)
...