将 JTextField 的内容放入变量 - Java & Swing

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1731100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:59:43  来源:igfitidea点击:

Putting the contents of a JTextField into a Variable - Java & Swing

javaswingjtextfield

提问by TheQuizitor

So i creating a little java app and am just wondering how i can get the contents of a JTextField and then assign the value into a String variable, I thought below would work:

所以我创建了一个小 Java 应用程序,我只是想知道如何获取 JTextField 的内容,然后将值分配给一个 String 变量,我认为下面会起作用:

JTextField txt_cust_Name = new JTextField();
String cust_Name;
txt_cust_Name.addActionListener(new ActionListener() 
{
    public void actionPerformed(ActionEvent e) 
    {
         cust_Name = txt_cust_Name.getText();
    }
});

Now i thought that this would send the value of the JtextField into the String Cust_Name.

现在我认为这会将 JtextField 的值发送到字符串 Cust_Name 中。

Anyone have any ideas to do this?

任何人都有任何想法来做到这一点?

Cheers.

干杯。

回答by camickr

An ActionListener is only fired when the Enter key is pressed.

只有在按下 Enter 键时才会触发 ActionListener。

Maybe you should use a FocusListener and handle the focusLost() event.

也许您应该使用 FocusListener 并处理 focusLost() 事件。

Or you can also add a DocumentListener to the Document of the text field. A DocumentEvent is fired every time a change is made to the text field.

或者您也可以在文本字段的 Document 中添加一个 DocumentListener。每次对文本字段进行更改时都会触发 DocumentEvent。

回答by Carl Smotricz

Normally a JTextField sits on a form and the user gets to play with it until he hits the Okbutton on the form. The handler for that button (an ActionListener) then grabs the current text value from the field and does something with it.

通常 JTextField 位于表单上,用户可以使用它直到他点击Ok表单上的按钮。该按钮的处理程序 (an ActionListener) 然后从字段中获取当前文本值并对其进行处理。

Do you want to do something wildly different? Do you need to respond to input as it changes, or only when the user leaves the field? Or is it important that he hit ENTER?

你想做一些完全不同的事情吗?您需要在输入更改时响应输入,还是仅在用户离开字段时响应?或者他按 ENTER 很重要吗?

Note that such non-standard behavior would likely confuse a user in real life. If you're just doing this for yourself, of course, anything goes.

请注意,这种非标准行为可能会使现实生活中的用户感到困惑。如果你只是为自己做这件事,当然,一切都会好起来的。

回答by Tom Neyland

Where ever you need to actually use your string variable, you can just say:

无论何时您需要实际使用字符串变量,您都可以说:

String cust_Name = txt_cust_Name.getText();

This is assuming that at the point in time you are trying to access this value it has already been entered... (As opposed trying to update the variable each time a key is pressed)

这是假设在您尝试访问该值的时间点它已经被输入......(与每次按下键时尝试更新变量相反)

回答by TheQuizitor

Thanks all, What i chose to do is to assign the values when a button is pressed:

谢谢大家,我选择做的是在按下按钮时分配值:

JButton btn_cust_Save = new JButton("Save Customer");
                       btn_cust_Save.addActionListener(new ActionListener()
                       {
                            public void actionPerformed(ActionEvent ae)
                            {
                                final String cust_Name = txt_cust_Name.getText();
                                final String cust_Door = txt_cust_Door.getText();
                                final String cust_Street1 = txt_cust_Street1.getText();
                                final String cust_Street2 = txt_cust_Street2.getText();
                                final String cust_City = txt_cust_City.getText();
                                final String cust_PCode = txt_cust_PCode.getText();
                                final String cust_Phone = txt_cust_Phone.getText();
                                final String cust_Email = txt_cust_Email.getText();
                            }
                        });

Thanks for all the help.

感谢所有的帮助。

回答by TheQuizitor

I've found that this code works pretty well:

我发现这段代码工作得很好:

package test;

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

public class Test extends JFrame {

private static final long serialVersionUID = -5624404136485946868L;

String userWord = "";
JTextField userInput;

public Test() {
    JFrame jf = new JFrame();
    JPanel panel = new JPanel();
    JLabel jl = new JLabel("Test");
    JButton jButton = new JButton("Click");
    userInput = new JTextField("", 30);
    jButton.addActionListener( (e) -> {
        submitAction();
    });
    jf.setSize(500, 500);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.add(panel);
    panel.add(jl);
    panel.add(userInput);
    panel.add(jButton);
}

private void submitAction() {
    userWord = userInput.getText();
    System.out.println(userWord);//do whatever you want with the variable, I just printed it to the console
}

public static void main(String[] args) {
    new Test();
}

}

}