简体   繁体   中英

how to get value on TextField Java Swing

I have a simple Java Swing form with a JTextField , I get value on JTextField by getText() method but I can't use it to main program. Can you help me What problem and how to fix? This is my code :

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Login {
private String name;

public Login() {
    JFrame main = new JFrame("LOGIN");
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setResizable(false);
    main.setLayout(null);
    main.setPreferredSize(new Dimension(200, 300));
    main.setLocation(400, 200);

    // Heading: LOGIN
    JLabel heading = new JLabel("LOGIN");
    heading.setBounds(80, 20, 50, 20);
    main.add(heading);

    // Label Username
    JLabel username_label = new JLabel("username: ");
    username_label.setBounds(5, 70, 80, 20);
    main.add(username_label);
    // Textfield Username
    final JTextField username_field = new JTextField();
    username_field.setBounds(70, 70, 120, 20);
    main.add(username_field);
    this.name = username_field.getText();

    // Button Login
    JButton loginBtn = new JButton("LOGIN");
    loginBtn.setBounds(40, 150, 120, 25);
    main.add(loginBtn);
    main.pack();
    main.setVisible(true);

    loginBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            name = username_field.getText();
            // System.out.println(name); //IT WORKS
        }
    });
}

public static void main(String[] args) {
    Login me = new Login();
    me.print();//I EXPECT IT WILL PRINT @name BUT NO, WHY? 
}

public void print() {
    System.out.println(name);
}
}

Thanks very much!

You shouldn't print the value before user clicks the button

public static void main(String[] args) {
    HelloWorldSwing me = new HelloWorldSwing();
    //me.print();//DON't PRINT HERE
}

Instead, you can do this in actionPerformed() method,

 loginBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            name = username_field.getText();
            HelloWorldSwing.this.print();
        }
    });

This will print the value, you enter in your text field

You are trying to print the name value immediately after showing your GUI:

Login me = new Login();
me.print(); // This executes immediately after the statement above

But this value is not set until the user presses the login button:

loginBtn.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        name = username_field.getText();
        // System.out.println(name); //IT WORKS
    }
});

Adding a text field to a GUI class does not magically add a getText() method to that class. I mean think about it, if there were two text fields, which one should be used for getText() ? The field should be declared as an attribute of the class, and a method defined that will getText() of that field.

BTW:

  1. don't use a JTextField when the GUI actually requires a JPasswordField .
  2. Instead of JFrame this should probably be a modal JDialog . See How to Use Modality in Dialogs for details.

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