3

所以我创建了一个小的 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();
    }
});

现在我认为这会将 JtextField 的值发送到 String Cust_Name。

有人有什么想法吗?

干杯。

4

5 回答 5

3

仅当按下 Enter 键时才会触发 ActionListener。

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

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

于 2009-11-13T18:50:11.103 回答
3

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

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

感谢所有的帮助。

于 2009-11-13T19:37:12.903 回答
2

无论您需要在哪里实际使用您的字符串变量,您都可以说:

String cust_Name = txt_cust_Name.getText();

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

于 2009-11-13T19:26:11.943 回答
0

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

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

请注意,这种非标准行为可能会使用户在现实生活中感到困惑。如果你只是为自己做这件事,当然,任何事情都会发生。

于 2009-11-13T18:59:38.867 回答
0

我发现这段代码运行良好:

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

}

于 2016-08-02T13:17:16.527 回答