【问题标题】:Putting the contents of a JTextField into a Variable - Java & Swing将 JTextField 的内容放入变量 - Java 和 Swing
【发布时间】:2009-11-13 18:47:19
【问题描述】:

所以我创建了一个小型 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。

有人有什么想法吗?

干杯。

【问题讨论】:

    标签: java swing jtextfield


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

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

      感谢大家的帮助。

      【讨论】:

        【解决方案3】:

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

        String cust_Name = txt_cust_Name.getText();
        

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

        【讨论】:

          【解决方案4】:

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

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

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

          【讨论】:

            【解决方案5】:

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

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

            }

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-03-28
              • 2022-11-20
              • 2013-04-29
              • 1970-01-01
              • 1970-01-01
              • 2012-01-08
              • 2018-10-08
              • 1970-01-01
              相关资源
              最近更新 更多