这个JavaGUI代码有什么问题?
我刚刚开始学习JavaGUI,在练习事件处理时遇到了这个问题。 当我在文本字段中输入一个数字时,它应该说明猜测的数字是较高的、较低的还是匹配的。如果不匹配,将提示输入另一个号码。但是窗户就这么挂着。 我猜这是一个无限循环。这是密码帮我找出问题所在。谢谢。这个JavaGUI代码有什么问题?,java,user-interface,event-handling,infinite-loop,Java,User Interface,Event Handling,Infinite Loop,我刚刚开始学习JavaGUI,在练习事件处理时遇到了这个问题。 当我在文本字段中输入一个数字时,它应该说明猜测的数字是较高的、较低的还是匹配的。如果不匹配,将提示输入另一个号码。但是窗户就这么挂着。 我猜这是一个无限循环。这是密码帮我找出问题所在。谢谢。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RandomNumGame extends JFrame { priva
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RandomNumGame extends JFrame {
private JLabel promptLabel, resultLabel, answerLabel;
private int tries=1, randomNum, guessNum;
private JButton button;
private JTextField txt;
private boolean guessed;
public RandomNumGame() {
setLayout(new FlowLayout());
promptLabel = new JLabel("Guess a number(1-1000): ");
add(promptLabel);
txt = new JTextField(7);
add(txt);
button = new JButton("Guess!");
add(button);
resultLabel = new JLabel("");
add(resultLabel);
/*answerLabel = new JLabel("");
add(answerLabel);
*/
Event e = new Event();
button.addActionListener(e);
}
private class Event implements ActionListener{
public void actionPerformed(ActionEvent e){
randomNum = (int )(Math.random() * 1000 + 1);
guessed=false;
do{
try{
guessNum = (int)(Double.parseDouble(txt.getText()));
if(guessNum>randomNum){
resultLabel.setText("Your number is higher. Try Again");
}
else if(guessNum<randomNum){
resultLabel.setText("Your number is lower. Try Again");
}
else{
resultLabel.setText("Your number matched!");
guessed=true;
}
}
catch(Exception ee){
resultLabel.setText("Enter a legit number. What are you stupid?");
}
}while(!guessed);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
RandomNumGame ran = new RandomNumGame();
ran.setDefaultCloseOperation(EXIT_ON_CLOSE);
ran.setSize(300, 120);
//ran.pack();
ran.setVisible(true);
ran.setTitle("Random Number Game");
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类RandomNumGame扩展了JFrame{
私人JLabel promptLabel、resultLabel、answerLabel;
private int trys=1,randomNum,guessNum;
私人按钮;
私有JTextField-txt;
私人布尔猜测;
公共随机数numgame(){
setLayout(新的FlowLayout());
promptLabel=新的JLabel(“猜一个数字(1-1000):”;
添加(即时标签);
txt=新的JTextField(7);
添加(txt);
按钮=新的JButton(“猜!”);
添加(按钮);
resultLabel=新的JLabel(“”);
添加(结果标签);
/*应答标签=新的JLabel(“”);
添加(应答标签);
*/
事件e=新事件();
addActionListener(e);
}
私有类事件实现ActionListener{
已执行的公共无效操作(操作事件e){
randomNum=(int)(Math.random()*1000+1);
猜测=错误;
做{
试一试{
guessNum=(int)(Double.parseDouble(txt.getText());
如果(猜测数>随机数){
resultLabel.setText(“您的号码更高,请重试”);
}
else if(guessNum在循环之前,您的随机数和文本输入将是相同的。我假设该值不会落入else条件,其中guessed
变为true。或者您需要在while循环本身中添加随机数生成语句。例如
do{
randomNum = (int )(Math.random() * 1000 + 1); //It required here
try{
guessNum = (int)(Double.parseDouble(txt.getText()));
if(guessNum>randomNum){
resultLabel.setText("Your number is higher. Try Again");
}
else if(guessNum<randomNum){
resultLabel.setText("Your number is lower. Try Again");
}
else{
resultLabel.setText("Your number matched!");
guessed=true;
}
}
catch(Exception ee){
resultLabel.setText("Enter a legit number. What are you stupid?");
}
}while(!guessed);
do{
randomNum=(int)(Math.random()*1000+1);//这里需要它
试一试{
guessNum=(int)(Double.parseDouble(txt.getText());
如果(猜测数>随机数){
resultLabel.setText(“您的号码更高,请重试”);
}
else if(guessNumGUI框架有自己的事件循环,在执行用户代码时,其事件处理(包括响应文本输入、按钮按下等)将被阻止。您希望自己的事件处理程序尽快完成
将其结构为,每次按下按钮时,计算当前猜测,显示消息,并结束处理程序。在按下按钮之间,程序保持猜测计数、要猜测的数字等。您不应该读取用户在循环中写的内容。guess按钮上的事件侦听器应该只检查数字是否为hi更低或相等一次,然后停止。用户每次按下按钮都会再次调用它。现在的情况是,除了可能存在的其他问题外,它只检查一次,并一直保持循环检查,直到其正确为止。这不是实用且糟糕的编程,因为它将在活动等待时消耗大量cpu这样做。您不需要在actionPerformed
方法中执行循环。只需让它执行一次,然后检查它是否被猜中。基本上,当用户输入错误的数字时,您就陷入了循环中。为了使循环更加顺畅,请仅在guessed
为true
时创建随机数只需执行一次猜测和条件
// Change constructor
public RandomNumGame() {
...
guessed = true; // initialize to true to create a new number when you click
}
private class Event implements ActionListener{
public void actionPerformed(ActionEvent e){
if(guessed) { // If the number was guessed, on the next click you get a new random number
randomNum = (int )(Math.random() * 1000 + 1);
guessed = false;
}
try{
guessNum = (int)(Double.parseDouble(txt.getText()));
if(guessNum>randomNum){
resultLabel.setText("Your number is higher. Try Again");
}
else if(guessNum<randomNum){
resultLabel.setText("Your number is lower. Try Again");
}
else{
resultLabel.setText("Your number matched! Click again for a new Number");
guessed=true;
}
}
catch(Exception ee){
resultLabel.setText("Enter a legit number. What are you stupid?");
}
}
}
//更改构造函数
公共随机数numgame(){
...
guessed=true;//单击时初始化为true以创建新数字
}
私有类事件实现ActionListener{
已执行的公共无效操作(操作事件e){
如果(猜测){//如果该数字是猜测的,在下一次单击时,您将获得一个新的随机数
randomNum=(int)(Math.random()*1000+1);
猜测=错误;
}
试一试{
guessNum=(int)(Double.parseDouble(txt.getText());
如果(猜测数>随机数){
resultLabel.setText(“您的号码更高,请重试”);
}
否则,如果(guessNum您是对的,这是循环问题。如果您输入的第一个数字与randomNum
不匹配,则变量guessed
将永远不会设置为true
。我建议您执行以下操作:
不要在事件处理程序中初始化randomNum,而要在构造函数中初始化
删除do{…}while(!guessed)
。这里不需要循环,现在只需拉出循环中的逻辑即可
循环内的随机数应该保持不变,但我如何在每次循环后更改文本输入?根据您的要求。为什么要尝试使用循环?也可以删除它。它将按照您的预期工作。是的,这有助于退出循环。但我还希望它在用户猜对后生成另一个数字。在这种情况下,您可以在最后一个else
子句中重新初始化随机数,现在这里有guessed=true