让 input.getText 接收我的下一个输入而不是上一个

Making input.getText take my next input rather than my last one

我有一个 window(类似于命令提示符),当我输入 "calculate" 时,它应该会开始计算器进程。当我输入 "calculator" 时,它显示 "calculator activated" 和 "please input your first value".

问题是,当我输入这个值时,我不知道如何让它使用它。相反,它取自我之前说 "calculate" 时的文字。

关于如何让它接受数字而不是单词的任何想法 "calculate"?

public void calculate(){
    try{
        print("calculator activated" + "\n" + "please input your first value" + "\n", false, new Color(210, 190, 13));

        String words = input.getText();
        //trying to get it to take the number I input

        print (words +"\n", false, new Color(210, 190, 13));
        //this print is to test what text it's saving as the 'words' variable
        //all it prints is the word 'calculate'
    }
    catch (Exception ex){}
}

您正在打印输入数字的邀请,然后立即阅读输入的内容。所以是的,当时的输入仍然包含"calculate"。打印邀请,或在文本字段上调用 ​​getText(),在用户擦除文本并替换为其他内容之前不要阻塞。它只是 returns 字段中的当前文本。

GUI 应用程序不像控制台应用程序那样工作。它们是基于事件的。您的计算方法除了打印邀请之外不应执行任何其他操作。您应该在文本字段或按钮上有一个 ActionListener,这样,当用户在文本字段中键入输入或按下按钮时,将调用侦听器并从文本字段中获取文本。

阅读tutorial about events