如何在 TextField Java Swing 上获取价值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20839177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:42:58  来源:igfitidea点击:

how to get value on TextField Java Swing

javaswingjtextfield

提问by dientmUET

I have a simple Java Swing form with a JTextField, I get value on JTextFieldby 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 :

我有一个带有 的简单 Java Swing 表单JTextField,我JTextField通过getText()方法获得了值,但我不能将它用于主程序。你能帮我什么问题以及如何解决?这是我的代码

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!

非常感谢!

采纳答案by Keerthivasan

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,

相反,您可以在 actionPerformed() 方法中执行此操作,

 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

这将打印您在文本字段中输入的值

回答by Duncan Jones

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

您尝试在显示 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
    }
});

回答by Andrew Thompson

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 twotext 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.

向 GUI 类添加文本字段并不会神奇地getText()向该类添加方法。我的意思是想想,如果有两个文本字段,应该使用哪一个getText()?该字段应声明为类的属性,并定义getText()该字段的意志的方法。

BTW:

顺便提一句:

  1. don't use a JTextFieldwhen the GUI actually requires a JPasswordField.
  2. Instead of JFramethis should probably be a modal JDialog. See How to Use Modality in Dialogsfor details.
  1. 不使用JTextField时,GUI实际需要JPasswordField
  2. 而不是JFrame这应该是一个 modal JDialog。有关详细信息,请参阅如何在对话框中使用模态