【问题标题】:how to get value on TextField Java Swing如何在 TextField Java Swing 上获得价值
【发布时间】:2014-01-17 07:36:51
【问题描述】:

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

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

非常感谢!

【问题讨论】:

  • Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。

标签: java swing jtextfield


【解决方案1】:

您不应在用户单击按钮之前打印该值

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

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

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

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

【讨论】:

    【解决方案2】:

    您正尝试在显示 GUI 后立即打印名称值:

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

    但是在用户按下登录按钮之前不会设置这个值:

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

    【讨论】:

      【解决方案3】:

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

      顺便说一句:

      1. 当 GUI 实际需要 JPasswordField 时,不要使用 JTextField
      2. 而不是JFrame,这应该是一个模态JDialog。详情请见How to Use Modality in Dialogs

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-24
        • 1970-01-01
        • 2021-07-22
        • 1970-01-01
        • 2020-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多