简体   繁体   English

用于从JTextField获取文本的KeyBinding

[英]KeyBinding for getting text from JTextField

I have a JTextField that will hold the user's input before they submit it (like a console Scanner object if you will). 我有一个JTextField ,它将在用户提交输入之前保存用户的输入(如果愿意的话,就像控制台Scanner对象一样)。 My main class has a method for getting input from the user. 我的主类有一种从用户那里获取输入的方法。 With a Scanner object this was easy to do: scannerObject.nextLine() . 使用Scanner对象,这很容易做到: scannerObject.nextLine() With this JTextField , I decided to use Key Bindings to determine when the Enter key was pressed in order to "submit" the contents of the JTextField . 通过此JTextField ,我决定使用“键绑定”来确定何时按下Enter键,以便“提交” JTextField的内容。 I set up the Key Bindings no problem, but I can't figure out how to get the main program to wait until the Enter key is pressed to look for the text. 我设置了键绑定没有问题,但是我无法弄清楚如何让主程序等到按下Enter键以查找文本。 If I use a loop, the program gets stuck and cannot register the key press. 如果使用循环,则程序将卡住,无法注册按键。

Here is a method that tries to get the input from the JTextField : 这是一种尝试从JTextField获取输入的方法:

public static String getStrInput() {
    // Request input
    display.print(">> ");
    needInput = true;
    nextInput = "";

    // Wait until a string is input from the text field
    while (nextInput.isEmpty());    // Wait

    needInput = false;

    return nextInput;
}

And here is the Event Handler for when the Enter key is pressed in my Key Binding class: 这是在我的键绑定类中按下Enter键时的事件处理程序:

private class SubmitAction extends AbstractAction implements ActionListener {
    /**
     * Makes Java happy.
     */
    private static final long serialVersionUID = 1L;

    /* -- Constructor -- */
    public SubmitAction() {
    }

    /* -- Method -- */

    @Override
    public void actionPerformed(ActionEvent e) {
        if (Game.needInput())
            Game.submitTextField();
    }
}

Note: All the submitTextField() method does is call the textField.getText() method and set the nextInput field (see my first method) to the returned String. 注意: submitTextField()方法所做的只是调用textField.getText()方法,并将nextInput字段(请参阅我的第一个方法)设置为返回的String。

What is supposed to happen is the request for input should be registered (this is the needInput = true line), and the Key Binding class should then submit the text field when the Enter key is pressed (since the Game.needInput() condition now returns true). 应该发生的是应该注册输入请求(这是needInput = true行),然后在按下Enter键时,Key Binding类应该提交文本字段(因为现在出现了Game.needInput()条件Game.needInput()返回true)。 However, in my getStrInput() method, the program gets stuck in the while (nextInput.isEmpty()); 但是,在我的getStrInput()方法中,程序被卡在while (nextInput.isEmpty()); loop. 环。 I figured that this would happen, but I have no clue how to get the main program to wait until the Key Binding's event handler has the text field submit its contents. 我认为会发生这种情况,但是我不知道如何让主程序等待键绑定的事件处理程序让文本字段提交其内容。

If this makes no sense at all or I need to reveal more code, I will happily elaborate. 如果这根本没有道理,或者我需要公开更多代码,我将很乐于阐述。 I've spent all day working on this little problem just to find frustration. 我整天都在研究这个小问题,目的只是为了发现沮丧。

With this JTextField, I decided to use Key Bindings to determine when the Enter key was pressed in order to "submit" the contents of the JTextField. 通过此JTextField,我决定使用“键绑定”来确定何时按下Enter键以“提交” JTextField的内容。

Actually you should just be adding an ActionListener to the text field. 实际上,您应该只在文本字段中添加一个ActionListener即可。 The ActionListener will be invoked when the Enter key is pressed. 当按下Enter键时,将调用ActionListener。

If you want to prompt and wait for text to be entered, then you should be using a JOptionPane . 如果要提示并等待输入文本,则应使用JOptionPane

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

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