简体   繁体   English

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

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

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. 干杯。

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. 或者,您也可以将DocumentListener添加到文本字段的Document中。 A DocumentEvent is fired every time a change is made to the text field. 每次对文本字段进行更改时都会触发DocumentEvent。

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. 感谢您的所有帮助。

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) 这是假设您正在尝试访问该值的时间点已经输入了该值(与尝试每次按下一个键来更新变量相反)

Normally a JTextField sits on a form and the user gets to play with it until he hits the Ok button on the form. 通常,JTextField位于窗体上,并且用户可以使用它,直到他单击窗体上的“ 确定”按钮。 The handler for that button (an ActionListener ) then grabs the current text value from the field and does something with it. 然后,该按钮的处理程序( 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. 当然,如果您只是为自己执行此操作,那么一切都会进行。

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();
}

} }

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

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