简体   繁体   English

为什么 java 中的 JTextfield 在运行时不显示文本?

[英]Why doesn't JTextfield in java display text at runtime?

Hope you are all well!希望你们一切都好!

Hope you understand my Java question... I have created a JFrame window with text to display, but it doesn't display at run-time (as it should) unless I maximise the Frame window. Hope you understand my Java question... I have created a JFrame window with text to display, but it doesn't display at run-time (as it should) unless I maximise the Frame window.

Can't understand it?看不懂?

Here's some code:这是一些代码:

package test;

import javax.swing.*;

class Test{

    private String x;

    private Test() {
        x="150";
    }

    public static void main(String[] args) {

        Test o1 = new Test();


        JTextField l = new JTextField(o1.x, JTextField.CENTER);
        l.setAlignmentX(0);
        l.setAlignmentY(0);

        JFrame window = new JFrame("Hello World!");
        window.setSize(800, 600);
        window.setResizable(true);
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.add(l);

        }

}

Altering the size of the frame (by maximising it) will cause it to be repainted.改变框架的大小(通过最大化它)将导致它被重新绘制。 The reason it needs to be repainted is that you added content to it after you already made it visible.它需要重新绘制的原因是您在使其可见之后添加了内容。

Instead, you could move window.setVisible(true);相反,您可以移动window.setVisible(true); to the end, so you don't show the window until you've added everything to it.到最后,所以在您将所有内容添加到其中之前,您不会显示 window。

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

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