简体   繁体   English

Java:JTextField不会出现

[英]Java: JTextField won't appear

public class HandleUI {     
    public static void setUpUI(){   
        JPanel jPan = new JPanel();
        FlowLayout flow = new FlowLayout();
        jPan.setLayout(flow);           
        txtFld = new JTextField();
        txtFld.setSize(550,5);
        jPan.add(txtFld);
        jPan.setSize(10,200);
        MainClass.mainFrame.add(jPan);
        int gapX = MainClass.mainFrame.getX()-(txtFld.getX()/2);
    }   
    //Instance variables.
    public static JTextField txtFld;
    public JButton [] buttons;
}   
public class MainClass { 
    public static void main (String [] args){           
        int frameX = Constants.FRAME_WIDTH;
        int frameY = Constants.FRAME_HEIGHT;
        mainFrame = new JFrame();
        mainFrame.setSize(frameX,frameY);
        mainFrame.setResizable(false);
        mainFrame.setVisible(true);
        HandleUI.setUpUI(); 
    }
    //Instance variables
    public static JFrame mainFrame;
}

It's supposed to show JTextField , but as you might have guessed - JFrame shows nothing. 它应该显示JTextField ,但是您可能已经猜到了JFrame什么也不显示。 I didn't type in imports on purpose, but they are all there. 我没有故意输入,但它们都在那里。 I can't find the problem. 我找不到问题。 Can anyone help? 有人可以帮忙吗?

1.) Simply write: 1.)简单地写:

JTextField tField = new JTextField(10);

Here In the constructor you are passing the number of columns, which is sufficient for a layout like FlowLayout to set the size of the JTextField 在这里,在构造函数中,您传递的是列数,这足以使FlowLayout类的布局设置JTextField的大小。

2.) The line mainFrame.setVisible(true); 2.)mainFrame.setVisible(true); must be the last line of the main method. 必须是main方法的最后一行。 You need to put the code at main() method, inside SwingUtilities.invokeLater(...) thingy. 您需要将代码放在SwingUtilities.invokeLater(...)内部的main()方法中。

3.) Instead of setting size on the JFrame use JFrame.pack() , to set the window to the preferred size. 3.)不用在JFrame上设置大小,而是使用JFrame.pack()将窗口设置为首选大小。

4.) Creation of unnecessary static members is a design flaw. 4.)创建不必要的static成员是设计缺陷。 Try to keep yourself away from such thingies. 尝试使自己远离此类事情。

5.) Read a bit about Concurrency in Swing 5.)阅读一些有关Swing中的并发性的知识

One Example Program for help( Use the order of lines as specified in this answer ): 一个示例程序来获得帮助( 使用此答案中指定的行顺序 ):

import java.awt.*;
import javax.swing.*;

public class Example {

    private void displayGUI() {
        JFrame frame = new JFrame("Example Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        JTextField tField = new JTextField(10);
        contentPane.add(tField);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new Example().displayGUI();
            }           
        };
        EventQueue.invokeLater(runnable);
    }
}

You have to call setVisible(true) on your JFrame AFTER having initialised your UI. 初始化UI 后,必须在JFrame上调用setVisible(true)

Simply pulling the following line: 只需拉以下行:

HandleUI.setUpUI(); 

... right before: ... 就在之前:

mainFrame.setVisible(true);

... will do the trick. ...将达到目的。

As a side note, I'd like to point out that setting the size of your text field won't really work like you did. 附带说明一下,我想指出的是,设置文本字段的大小实际上并不会像您那样工作。 You probably would use setPreferredSize(Dimension) instead. 您可能会改用setPreferredSize(Dimension) Or, even better, organise your UI only using Layouts and not manually setting any component's size. 或者,甚至更好的是,仅使用布局来组织UI,而不是手动设置任何组件的大小。

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

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