简体   繁体   中英

Java Swing: form with text fields

I'm doing a form in java.

The idea is:

When an user insert his name and second name he have to press the "submit" button. Now I would like to listen to this action with the submit button but I don't know how to get what the user put in the text fields: "name" and "second name".

I would like to see this data just with the submit button, but i don't know, if it's possible.

Or I would like to know in which way I can see all this data when the user clicks on "submit".

thanks, this is the code.

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

An easy approach would be to use a final local variable for each JTextField . You can access them from within an ActionListener added to your JButton :

final JTextField textField = new JTextField();

btnSubmit.addActionListener(new ActionListener() {

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

Don't extend a JFrame, you are not adding any new functionality to the frame.

Read the section from the Swing tutorial on How to Use Text Fields for a working example that will show you a better way to structure your program so that you have access to the data in the text fields from your ActionListener.

The Swing tutorial has examples for all Swing components.

use JTextField object.getText() and store it locally in a String in Event function of the button.Use that local String for reference.

Example

String name=jtextfield.getText();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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