JavaSwing:带有文本字段的表单

JavaSwing:带有文本字段的表单,java,forms,swing,actionlistener,jtextfield,Java,Forms,Swing,Actionlistener,Jtextfield,我正在用java做一个表单 这个想法是: 当用户插入他的名字和第二个名字时,他必须按“提交”按钮。现在我想用submit按钮来听这个操作,但我不知道如何获得用户在文本字段中输入的内容:“name”和“secondname” 我想通过提交按钮查看这些数据,但我不知道是否可能 或者,我想知道当用户单击“提交”时,我可以以何种方式查看所有这些数据 谢谢,这是密码 public class appNegozio extends JFrame { private JPanel contentPa

我正在用java做一个表单

这个想法是:

当用户插入他的名字和第二个名字时,他必须按“提交”按钮。现在我想用submit按钮来听这个操作,但我不知道如何获得用户在文本字段中输入的内容:“name”和“secondname”

我想通过提交按钮查看这些数据,但我不知道是否可能

或者,我想知道当用户单击“提交”时,我可以以何种方式查看所有这些数据

谢谢,这是密码

public class appNegozio extends JFrame {
     private JPanel contentPane;
     private JTextField textField;
     private JTextField textField_1;

/**
 * Launch the application.
 */
     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
            public void run() {
              try {
                appNegozio frame = new appNegozio();
                frame.setVisible(true);
              } catch (Exception e) {
                e.printStackTrace();
              }
           }
        });
    }

/**
 * Create the frame.
 */
    public appNegozio() {
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setBounds(100, 100, 450, 300);
       contentPane = new JPanel();
       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
       setContentPane(contentPane);
       SpringLayout sl_contentPane = new SpringLayout();
       contentPane.setLayout(sl_contentPane);

       textField = new JTextField();
       sl_contentPane.putConstraint(SpringLayout.NORTH, textField, 10,    
                   SpringLayout.NORTH, contentPane);
       sl_contentPane.putConstraint(SpringLayout.WEST, textField, 62, 
                   SpringLayout.WEST, contentPane);
       contentPane.add(textField);
       textField.setColumns(10);

       textField_1 = new JTextField();
       sl_contentPane.putConstraint(SpringLayout.NORTH, textField_1, 16, 
                  SpringLayout.SOUTH, textField);
       sl_contentPane.putConstraint(SpringLayout.WEST, textField_1, 0, 
                  SpringLayout.WEST, textField);
       contentPane.add(textField_1);
       textField_1.setColumns(10);

      JLabel lblNome = new JLabel("Nome");
      sl_contentPane.putConstraint(SpringLayout.NORTH, lblNome, 10,  
                  SpringLayout.NORTH, contentPane);
      sl_contentPane.putConstraint(SpringLayout.EAST, lblNome, -7, 
                  SpringLayout.WEST, textField);
      contentPane.add(lblNome);

      JLabel lblCognome = new JLabel("Cognome");
      sl_contentPane.putConstraint(SpringLayout.NORTH, lblCognome, 0, 
                  SpringLayout.NORTH, textField_1);
      sl_contentPane.putConstraint(SpringLayout.EAST, lblCognome, -6, 
                  SpringLayout.WEST, textField_1);
      contentPane.add(lblCognome);

    JButton btnSubmit = new JButton("Submit");
    sl_contentPane.putConstraint(SpringLayout.NORTH, btnSubmit, 24, 
                  SpringLayout.SOUTH, textField_1);
    sl_contentPane.putConstraint(SpringLayout.WEST, btnSubmit, 10, 
                  SpringLayout.WEST, contentPane);
    contentPane.add(btnSubmit);
   }
 }

一种简单的方法是为每个
JTextField
使用
final
局部变量。 您可以从添加到
JButton
ActionListener
中访问它们:

final JTextField textField = new JTextField();

btnSubmit.addActionListener(new ActionListener() {

    public void actionPerformed(final ActionEvent e) {
        System.out.println(textField.getText());
    }
});

一种简单的方法是为每个
JTextField
使用
final
局部变量。 您可以从添加到
JButton
ActionListener
中访问它们:

final JTextField textField = new JTextField();

btnSubmit.addActionListener(new ActionListener() {

    public void actionPerformed(final ActionEvent e) {
        System.out.println(textField.getText());
    }
});

不要扩展JFrame,您不会向该框架添加任何新功能

阅读上Swing教程中的部分,以获得一个工作示例,该示例将向您展示一种更好的方法来构造程序,以便您可以从ActionListener访问文本字段中的数据


Swing教程提供了所有Swing组件的示例。

不要扩展JFrame,您不会向该框架添加任何新功能

阅读上Swing教程中的部分,以获得一个工作示例,该示例将向您展示一种更好的方法来构造程序,以便您可以从ActionListener访问文本字段中的数据


Swing教程中有所有Swing组件的示例。

使用JTextField object.getText()并将其本地存储在按钮的事件函数中的字符串中。使用该本地字符串作为参考

范例

String name=jtextfield.getText();

使用JTextField object.getText()并将其本地存储在按钮的事件函数中的字符串中。使用该本地字符串作为参考

范例

String name=jtextfield.getText();

@GiovanniFar,你不认为你应该先阅读教程(在决定使用哪个答案之前)来理解这个建议吗。将变量设置为final并不是一个好主意,只是为了让程序能够编译。你应该从一开始就正确地设计代码。@GiovanniFar,你不认为你应该先阅读教程(在决定使用哪个答案之前)来理解这个建议吗。将变量设置为final并不是一个好主意,只是为了让程序能够编译。您应该从一开始就正确地设计代码。