如何在Java Swing中获取JTextField的值?

How to get the value of JTextField in Java Swing?

我有一个 JTextField 和一个 JButton。我正在尝试获取用户单击按钮时在文本字段中键入的内容。

public String insideTextField;

public void login() {

    loginFrame = new JFrame("Test");
    loginFrame.setIconImage(imageIcon.getImage());
    loginFrame.setSize(400,400);
    loginFrame.setLayout(new GridLayout(12, 3));

    loginFrame.add(usernameField);
    loginFrame.add(loginButton);

    loginButton.addActionListener(e -> {

        insideTextField = usernameField.getText();
        loginFrame.setVisible(false);
    });

    loginFrame.setVisible(true);
}

我希望 insideTextField 是我调用此方法后用户在文本字段中键入的内容。但它是空的。但是,由于 loginFrame.setVisible(false);loginFrame 将被隐藏,所以按钮可以使用!

我做错了什么?

您的期望涉及神奇的思维。了解 insideTextField 包含一个 String 对象,该对象实际上是 immutable(无法更改)。是的,当您创建 insideTextField 时,您将恰好驻留在 JTextField 中的字符串分配给它,可能是一个空的 "" 字符串,但请理解,当 JTextField 的文本属性发生变化时,insideTextField 不会,事实上 不能 自己神奇地改变。解决方案是 不要 有一个 insideTextField 字段,而是给 class 一个 getter 方法,当它在 JTextField 中提取当前字符串时是需要的。例如:

public String getUserName() {
    return usernameField.getText();
}

旁注:我通常使用模式 JDialog 而 JFrame 来获取登录凭据。这样,由于对话框是模态的,我知道用户何时使用完它。事实上,JOptionPane 用作快速而肮脏的模式对话框创建器。例如,如果我们有一个像这样的登录 JPanel:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class LoginPanel extends JPanel {
    private static final int GAP = 3;
    private JTextField usernameField = new JTextField(10);
    private JPasswordField passwordField = new JPasswordField(10);

    public LoginPanel() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = getGbc(0, 0);
        add(new JLabel("User Name:"), gbc);
        gbc = getGbc(1, 0);
        add(usernameField, gbc);
        gbc = getGbc(0, 1);
        add(new JLabel("Password:"), gbc);
        gbc = getGbc(1, 1);
        add(passwordField, gbc);
        setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
    }

    private static GridBagConstraints getGbc(int x, int y) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.insets = new Insets(GAP, GAP, GAP, GAP);
        gbc.anchor = x == 0 ? GridBagConstraints.WEST : GridBagConstraints.EAST;        

        return gbc;
    }

    public String getUserName() {
        return usernameField.getText();
    }

    public char[] getPassword() {
        return passwordField.getPassword();
    }
}

再次使用 getter 方法提取 JTextField 中的文本,我们可以像这样在 JOptionPane 中显示它:

JFrame frame = new JFrame("LoginExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// fill the JFrame with the main GUI
// .... 

frame.pack();

// create the LoginPanel JPanel here
LoginPanel loginPanel = new LoginPanel();

// and display it in a JOptionPane here
int result = JOptionPane.showConfirmDialog(frame, loginPanel, 
        "User Log-In", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
    String userName = loginPanel.getUserName();
    char[] password = loginPanel.getPassword();

    // test to make sure code is working
    System.out.println("User Name: " + userName);

    // check for name/password validity here
    // ... if name/password OK, then show JFrame:
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}