简体   繁体   English

用 Java swing 等待一个事件

[英]Waiting for an event with Java swing

I've got a Java class called "Text" which uses swing to create 2 textboxes.我有一个名为“Text”的 Java class ,它使用 swing 创建 2 个文本框。 There's a listener in it that copies text from the first box to the end of the second box.其中有一个侦听器将文本从第一个框复制到第二个框的末尾。 So far I have it set up to where other classes can call a function to output text to the second box.到目前为止,我已经将它设置为其他类可以调用 function 到 output 文本到第二个框的位置。

What I want to do is have a function that causes the program to wait until the user enters a string of text and presses enter (which would trigger the event listener I already have), and then returns that string of text to a user.我想要做的是有一个 function 导致程序等到用户输入一个文本字符串并按下回车键(这将触发我已经拥有的事件侦听器),然后将该文本字符串返回给用户。

I tried doing something like:我尝试做类似的事情:

String lastInput;
boolean waiting;

    public void actionPerformed(ActionEvent evt) {
        String text = textField.getText();
        println(text);
        lastInput = text;
        waiting = false;
        parse(text);
        textField.selectAll();
    }

    public String get() {
        waiting = true;
        while(waiting){}
        return lastInput;
    }

But this just causes the program to freeze and not register any input (including any typing in the textbox).但这只会导致程序冻结而不注册任何输入(包括在文本框中输入的任何内容)。 I'm pretty sure that I'd need to make use of java's thread system, but I've never used it before and I can't find anything online about how I could use it to solve this problem in particular.我很确定我需要使用 java 的线程系统,但我以前从未使用过它,而且我在网上找不到任何关于如何使用它来解决这个问题的信息。

Thanks!谢谢!

I'm pretty sure that I'd need to make use of java's thread system,我很确定我需要使用java的线程系统,

That is not how you design a GUI.这不是您设计 GUI 的方式。 Generally a GUI sits there doing nothing.通常,GUI 坐在那里什么都不做。 Think of your browser, it only does something when the user generates an event, for example, by using the keyboard or the mouse.想想你的浏览器,它只在用户生成事件时才做一些事情,例如,通过使用键盘或鼠标。

I want to do is have a function that causes the program to wait until the user enters a string of text and presses enter我想做的是有一个 function 导致程序等到用户输入一串文本并按回车键

If you want to prompt the user to enter text then you should probably use a JOptionPane .如果您想提示用户输入文本,那么您可能应该使用JOptionPane Read the Swing tutorial on How to Make Dialogs for some examples.阅读有关如何制作对话框的 Swing 教程以获取一些示例。

If you truly have a situation where you need to do processing when text is entered in a text field of a form then you would add an ActionListener to the text field.如果您确实遇到需要在表单的文本字段中输入文本时进行处理的情况,那么您可以在文本字段中添加一个ActionListener

See: How to Use Text Fields .请参阅: 如何使用文本字段

The TextDemo example will copy text from the text field to the text area when Enter is pressed on the text field.当在文本字段上按下Enter时, TextDemo示例会将文本从文本字段复制到文本区域。

First, the main issue here is the while loop, which is an infinite loop.首先,这里的主要问题是while循环,它是一个无限循环。 If you use a single thread, then once the loop is executed, there will be no free context that can invoke the "actionPerformed" method.如果使用单线程,那么一旦执行循环,就没有可以调用“actionPerformed”方法的空闲上下文。

The main issue is having a different contexts: one will be executing the "get" method, and one will be executing the "actionPerformed" once the trigger invoked.主要问题是具有不同的上下文:一个将执行“get”方法,一个将在触发器调用后执行“actionPerformed”。

Two more things:还有两件事:

  1. Your "get" function name is bad, and may be confusing.您的“获取” function 名称不好,可能会造成混淆。 Get is used for getters (accessing class'es members). Get 用于 getter(访问类的成员)。
  2. Use a thread-safe waiting boolean (AtomicBoolean or something).使用线程安全等待 boolean (AtomicBoolean 或其他东西)。

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

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