简体   繁体   English

在GUI中等待侦听器执行…

[英]Wait for the listener in do… while GUI

I create a program for Random processes(such 2+2 , 14/2 etc). 我为随机过程(例如2 + 2,14/2等)创建了一个程序。

I try to create a GUI. 我尝试创建一个GUI。 I have 5 buttons(Add for Add, Div for the divide etc) When the user click on Add button i send a message to JTextArea to add a message: 我有5个按钮(“添加”用于添加,“ div”用于除法等)当用户单击“添加”按钮时,我向JTextArea发送消息以添加消息:

2 + 4 = 2 + 4 =

(Or other random numbers) (或其他随机数)

I have a JTextField that the user can write the answer. 我有一个JTextField,用户可以编写答案。 So if he write 6 a success message appears to JText area(the same if he put wrong answer but a failure message) and 2 other Random numbers too, so: 因此,如果他写6,则JText区域将显示一条成功消息(如果他输入错误答案,但失败消息,则显示相同),而其他2个随机数也是如此,因此:

2+4 = 6 2 + 4 = 6

Yes! 是!

3+4 = 3 + 4 =

And continues until the method c.isOverLimit() returns true(After 10 processes). 并继续直到方法c.isOverLimit()返回true(10个过程之后)。

int x = 10;

do {
    String string = String.format("%d %s %d =", p.getNumber1(),
                                  p.getOperator(), p.getNumber2());
    writeMessage(string);
    textField.addActionListener(
    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                p.checkAnswer(Double.parseDouble(e.getActionCommand()));
                writeMessage(p.getMessage(Double.parseDouble(
                                              e.getActionCommand())));
                textField.setText("");
            }
            catch(Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    );
    c = Proccess.getCounter();
    x--;
}
while (x != 0);

My problem is that when i push the Add Button i have this output: 我的问题是,当我按下“添加”按钮时,我得到以下输出:

4+3= 4 + 3 =

4+3= 4 + 3 =

4+3= 4 + 3 =

4+3= 4 + 3 =

4+3= 4 + 3 =

4+3= 4 + 3 =

4+3= 4 + 3 =

4+3= 4 + 3 =

4+3= 4 + 3 =

4+3= 4 + 3 =

The wrong is that the do...while runs 10 times and doesn't stop for the user's answers. 错误的是,do ... while运行10次,并不会停止为用户提供答案。 Dont pay attention on the methods i use. 不要注意我使用的方法。 My program runs correctly in console program so my fault is in the while...do 我的程序在控制台程序中正确运行,所以我的错误在一段时间内...执行

Sorry for my English. 对不起我的英语不好。 Thank you! 谢谢!

Counting the number of calls should be done in the actionPerformed method. 计算调用次数应在actionPerformed方法中完成。 Remove the do...while loop and use this instead: 删除do...while循环并改用它:

int x = 10;
textField.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if (x >= 0) {
            try {
                p.checkAnswer(Double.parseDouble(e.getActionCommand()));
                writeMessage(p.getMessage(Double.parseDouble(e.getActionCommand())));
                textField.setText("");
            } catch(Exception ex){
                System.out.println(ex.getMessage());
            }
            x--;
        } else {
            // quit, or whatever you want to do
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM