打印 JTextField 在控制台上显示空白 [英] Printing JTextField gives out a blank on console

查看:52
本文介绍了打印 JTextField 在控制台上显示空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 新手,刚刚尝试了 Java 的 Swing,我尝试制作一个登录表单,将 JTextField 的内容打印到控制台,但是当我尝试时控制台没有显示任何内容.

>

这是我的代码:

<预><代码>导入 java.awt.EventQueue;导入 java.awt.event.ActionEvent;导入 java.awt.event.ActionListener;导入 javax.swing.JFrame;导入 javax.swing.JTextField;导入 javax.swing.JButton;公共类 JavaTextField {私人 JFrame 框架;私人 JTextField text1;私人 JTextField text2;/*** 启动应用程序.*/公共静态无效主(字符串 [] args){EventQueue.invokeLater(new Runnable() {公共无效运行(){尝试 {JavaTextField window = new JavaTextField();window.frame.setVisible(true);} 捕获(异常 e){e.printStackTrace();}}});}/*** 创建应用程序.*/公共 JavaTextField() {初始化();}/*** 初始化框架的内容.*/私有无效初始化(){框架 = 新 JFrame();frame.setBounds(100, 100, 450, 300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(null);text1 = new JTextField();text1.setBounds(114, 38, 122, 40);frame.getContentPane().add(text1);text1.setColumns(10);String majorText = text1.getText();text2 = new JTextField();text2.setBounds(114, 117, 86, 20);frame.getContentPane().add(text2);text2.setColumns(10);String minorText = text2.getText();JButton btnButton = new JButton("Button");btnButton.setBounds(132, 192, 159, 40);frame.getContentPane().add(btnButton);btnButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.out.println(majorText);System.out.println(minorText);}});}}

如果有人能指出我正确的方向,我很高兴,因为我还没有在互联网上看到这个问题的解决方案.

解决方案

这里的问题是,您在错误的时间从 JTextField 检索内容.当前,您在初始化组件时立即调用 getText().当然,getText()返回的内容会是一个空的String.

因此,要解决逻辑中的问题,您实际上应该仅在 JTextField 中检索 majorTextminorText>JButton 已被按下.因为在那个时间点,当您按下按钮时,您的文本字段的内容应该是正确的.为此,请将文本的检索移至 ActionListener.

更新后的 ActionListener 应该如下所示:

btnButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {String majorText = text1.getText();String minorText = text2.getText();System.out.println(majorText);System.out.println(minorText);}}

或者简单地说:

btnButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.out.println(text1.getText());System.out.println(text2.getText());}}

旁注(另一个答案也提到了):

不鼓励使用 null 布局,因为它是不必要错误的常见来源.查看不同的 LayoutManagers 以及如何以及何时可用使用它们.

I am new to Java and have just tried out Java's swing, I tried making a log in form that would print the content of a JTextField to the console, but the console doesn't show anything when I tried it.

Here's my code:


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

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;

public class JavaTextField {

    private JFrame frame;
    private JTextField text1;
    private JTextField text2;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JavaTextField window = new JavaTextField();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public JavaTextField() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        text1 = new JTextField();
        text1.setBounds(114, 38, 122, 40);
        frame.getContentPane().add(text1);
        text1.setColumns(10);
        String majorText = text1.getText();
        
        
        text2 = new JTextField();
        text2.setBounds(114, 117, 86, 20);
        frame.getContentPane().add(text2);
        text2.setColumns(10);
        String minorText = text2.getText();
        
        JButton btnButton = new JButton("Button");
        btnButton.setBounds(132, 192, 159, 40);
        frame.getContentPane().add(btnButton);
        btnButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(majorText);
                System.out.println(minorText);
            }
            }
    );
}
    }

I'm glad if anyone could point me in the right direction, because I haven't seen the solution to this problem on the internet yet.

解决方案

The issue here is, that you retrieve the content from the JTextFields at the wrong time. Currently you call getText() right when the components are being initialized. Of course, the content returned from getText() will be an empty String.

So to fix the issue in your logic, you should actually retrieve the majorText and minorText from the JTextFields only once your JButton has been pressed. Because at that point in time, when your button is pressed, the content of your text fields should be correct. To do that, move the retrieval of the text to the ActionListener.

The updated ActionListener should look as follows:

btnButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String majorText = text1.getText();
        String minorText = text2.getText();
        System.out.println(majorText);
        System.out.println(minorText);
    }
}

or simply:

btnButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        System.out.println(text1.getText());
        System.out.println(text2.getText());
    }
}

Sidenote (as also mentioned by the other answer):

Using null layout is highly discouraged, as it is a frequent source for unnecessary errors. Have a look at the different LayoutManagers available and how and when to use them.

这篇关于打印 JTextField 在控制台上显示空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆