0

当我在文本框中输入内容时,如果我按 Enter 键,我的应用程序会冻结并停止响应。它是一个您尝试获取 1 到 100 之间的数字的游戏。我想我需要一个提交按钮或 Enter 键上的 ActionListener。有人可以帮我解决这个问题。还有一种方法可以在 FlowLayout 中换行吗?还是另一种布局更好?这是我的代码:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class game extends JFrame {

    private static final long serialVersionUID = 1L;
    private Random rand = new Random();
    private int number = rand.nextInt(101);
    private JLabel result, prompt;
    private JTextField input;


    game() {
        super("Numbers");
        setSize(500, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        prompt = new JLabel("Enter a number between 1 and 100");
        add(prompt);

        input = new JTextField(3);
        add(input);

        result = new JLabel("");
        add(result);



        event a = new event();
        input.addActionListener(a);

    }

    public class event implements ActionListener {
        public void actionPerformed(ActionEvent a) {
            int guess = 0;

            try {
                guess = Integer.parseInt(input.getText());
            } catch(NumberFormatException e) {
                result.setText("Error - Illegal Data Entered");
                result.setForeground(Color.RED);
            }


            while(guess!=number) {
                if(guess>number) {
                    result.setText("Guess Too High. Try Again.");
                    result.setForeground(Color.CYAN);
                } else if(guess<number) {
                    result.setText("Guess Too Low. Try Again.");
                    result.setForeground(Color.CYAN);
                } else {
                    result.setText("Unknown Error");
                    result.setForeground(Color.RED);
                }
            }
            if(guess==number){
                result.setText("Right the number was "+number);
                result.setForeground(Color.GREEN);
            }
        }
    }

    public static void main(String[] args) {
        new game().setVisible(true);

    }

}

提前致谢。

更新

所以我删除了 While 循环,一切正常。它做对了吗?

4

3 回答 3

1

while-loop您正在使用永远无法破坏的事件调度线程...

while(guess!=number) {...}

更新...

代替...

while(guess!=number) {
    if(guess>number) {
        result.setText("Guess Too High. Try Again.");
        result.setForeground(Color.CYAN);
    } else if(guess<number) {
        result.setText("Guess Too Low. Try Again.");
        result.setForeground(Color.CYAN);
    } else {
        result.setText("Unknown Error");
        result.setForeground(Color.RED);
    }
}

尝试

if(guess>number) {
    result.setText("Guess Too High. Try Again.");
    result.setForeground(Color.CYAN);
} else if(guess<number) {
    result.setText("Guess Too Low. Try Again.");
    result.setForeground(Color.CYAN);
} else {
    result.setText("Unknown Error");
    result.setForeground(Color.RED);
}

Swing 是一个事件驱动的环境。那就是您的程序将(自动)等待,直到用户执行某些操作并且您的程序将对其做出响应

于 2013-06-05T02:27:38.923 回答
1

你的问题就在这里,guess或者number永远不会改变值,无限循环

 while(guess!=number) {
                    if(guess>number) {
                        result.setText("Guess Too High. Try Again.");
                        result.setForeground(Color.CYAN);
                    } else if(guess<number) {
                        result.setText("Guess Too Low. Try Again.");
                        result.setForeground(Color.CYAN);
                    } else {
                        result.setText("Unknown Error");
                        result.setForeground(Color.RED);
                    }
                }

你只需要删除while,是不必要的

所以你的内部班会是这样的

public class MyEvent implements ActionListener {
        public void actionPerformed(ActionEvent a) {
            int guess = 0;

            try {

               guess = Integer.parseInt(input.getText());
                        if(guess>number) {
                            result.setText("Guess Too High. Try Again.");
                            result.setForeground(Color.CYAN);
                        } else if(guess<number) {
                            result.setText("Guess Too Low. Try Again.");
                            result.setForeground(Color.CYAN);
                        } else {
                           result.setText("Right the number was "+number);
                           result.setForeground(Color.GREEN);                      
                        }

            }catch(NumberFormatException nfe){
                result.setText("Error - Illegal Data Entered");
                result.setForeground(Color.RED);               
            }

  }
}

当该整数失败时,我尝试捕获所有阻塞原因,您必须结束事件的执行..

注意遵循约定,声明性名称和类应以大写开头

于 2013-06-05T02:27:42.070 回答
0

你有一个无限循环。 guess永远不会在循环构造的主体中更新,因此每次迭代时条件始终为假。

于 2013-06-05T02:28:23.820 回答