简体   繁体   English

JTextField将用户输入另存为字符串

[英]JTextField save user input as a String

So I am attempting to create a login screen that prompts the user with a text box and 2 buttons (Login and Cancel). 因此,我试图创建一个登录屏幕,用一个文本框和2个按钮(“登录”和“取消”)提示用户。 When the user hits login, I want the value of the JTextField to be stored in a variable or at least be usable. 当用户单击登录时,我希望将JTextField的值存储在变量中或至少可用。 When I attempt to do anything with the playerNameTxt.getText() method, I get an error as if playerNameTxt doesn't exist. 当我尝试使用playerNameTxt.getText()方法执行任何操作时,出现错误,好像playerNameTxt不存在。

public class GUI extends JPanel implements ActionListener {

protected JTextField playerNameTxt;

public GUI() {
    JTextField playerNameTxt = new JTextField(20);

    JLabel playerNameLbl = new JLabel("Enter Player Name");

    JButton loginBtn = new JButton("Login");
    loginBtn.setVerticalTextPosition(AbstractButton.BOTTOM);
    loginBtn.setHorizontalTextPosition(AbstractButton.LEFT);
    loginBtn.setMnemonic(KeyEvent.VK_D);
    loginBtn.setActionCommand("login");
    loginBtn.addActionListener(this);
    loginBtn.setToolTipText("Click this to Login");

    JButton cancelBtn = new JButton("Cancel");
    cancelBtn.setVerticalTextPosition(AbstractButton.BOTTOM);
    cancelBtn.setHorizontalTextPosition(AbstractButton.RIGHT);
    cancelBtn.setMnemonic(KeyEvent.VK_M);
    cancelBtn.setActionCommand("cancel");
    cancelBtn.addActionListener(this);
    cancelBtn.setToolTipText("Click this to Cancel");

    add(playerNameLbl);
    add(playerNameTxt);
    add(loginBtn);
    add(cancelBtn);
}

public void actionPerformed(ActionEvent e) {
    if ("login".equals(e.getActionCommand())) {
        System.out.println(playerNameTxt);
    } else {
        System.exit(0);
    }
}

private static void createAndShowGUI() {
    JFrame frame = new JFrame("-- Munitions Login --");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocation(400, 200);

    GUI newContentPane = new GUI();
    newContentPane.setOpaque(true);
    frame.setContentPane(newContentPane);

    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

You firstly declare the field outside of the constructor, then you declare it inside the constructor again, so that it will be deleted after the constructor returns and the GUI was destroyed, and it will not be usable for your class after the constructor has finished. 您首先要在构造函数外部声明该字段,然后再次在构造函数内部声明该字段,以便在构造函数返回并且GUI被销毁后将其删除,并且在构造函数完成后,该字段将无法用于您的类。 You should change this line: 您应该更改此行:

JTextField playerNameTxt = new JTextField(20);

to this: 对此:

playerNameTxt = new JTextField(20);

In your constructor, you aren't referencing the instance variable playerNameTxt - you're making a new local variable. 在构造函数中,您没有引用实例变量playerNameTxt ,而是在创建新的局部变量。 You should change JTextField playerNameTxt = new JTextField(20); 您应该更改JTextField playerNameTxt = new JTextField(20); to playerNameTxt = new JTextField(20); playerNameTxt = new JTextField(20); (or this.playerNameTxt = new JTextField(20); ) to properly initialize the variable. (或this.playerNameTxt = new JTextField(20); )正确初始化变量。 You should then be able to call methods on it without warnings or errors, assuming everything else is correct. 然后,假设其他所有内容都正确,则应该能够在没有警告或错误的情况下调用其上的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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