java 启动时 JTextField 未显示在 JPanel 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13225960/
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-10-31 11:57:13  来源:igfitidea点击:

JTextField not showing up in JPanel on launch

javaswingjpaneljtextfieldlayout-manager

提问by tazboy

The JTextFieldis there because when I move the mouse over where it's supposed to be the mouse icon changes to a cursor, then when I click it shows up. But it's invisible at launch. What am I missing?

JTextField是因为当我将鼠标移到它应该是鼠标图标的位置时,它会变成一个光标,然后当我点击它时就会显示出来。但它在启动时是不可见的。我错过了什么?

public class JavaSwingTextfield extends JFrame {
    private static final long serialVersionUID = 1L;

    JTextField myTextField;

    public JavaSwingTextfield(){

        /***** JFrame setup *****/

        // Set the size of the window
        setSize(600,600);

        // Make the application close when the X is clicked on the window
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // Makes the JFrame visible to the user
        setVisible(true);

        /***** JFrame setup END *****/


        /***** JButton setup *****/

        // Create a JLabel and set its label to "Start"
        myTextField = new JTextField("Start");

        // Set the label's size
        myTextField.setSize(100, 50);

        // Put the label in a certain spot
        myTextField.setLocation(200, 50);

        // Set a font type for the label
        //Font myFont = new Font("Serif", Font.BOLD, 24);
        //myTextField.setFont(myFont);

        // Add the label to the JFrame
        add(myTextField);

        /***** JButton setup END *****/

    }


    /***** The main method *****/
    public static void main(String[] args){ 

        new JavaSwingTextfield();

    }

}

回答by David Kroukamp

  • Use Event Dispatch Threadfor creating GUI components
  • Do not call setVisible(..)before all components have been added to the JFrame( this is the above code snippets actual error +1 to @Clark)
  • Do not unnecessarily extend JFrameclass
  • Do not call setSize(..), rather simply call JFrame#pack()before setting the JFramevisible
  • Do not call setSize()on JTextFieldrather look at its constructor: JTextField(String text,int columns)
  • Use appropriate LayoutManagers, see here for some examples: A Visual Guide to Layout Managers
  • 使用Event Dispatch Thread用于创建GUI组件
  • setVisible(..)在将所有组件添加到JFrame(这是上面的代码片段实际错误 +1 到 @Clark)之前不要调用
  • 不要不必要地扩展JFrame
  • 不要调用setSize(..),而只是JFrame#pack()在设置JFrame可见之前调用
  • 不要叫setSize()JTextField,而看它的构造函数:JTextField(String text,int columns)
  • 使用适当的LayoutManagers,请参见此处的一些示例:布局管理器的可视化指南

Here is an example I made (basically your code with fixes):

这是我制作的示例(基本上是您的修复代码):

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class JavaSwingTextfield {

    private JTextField myTextField;

? ? public JavaSwingTextfield() {
? ? ? ? JFrame frame = new JFrame();
? ? ? ? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
? ? ? ??
? ? ? ? myTextField = new JTextField("Start");
? ? ? ??
? ? ? ? // Add the label to the JFrame
? ? ? ? frame.add(myTextField);
? ? ? ??
? ? ? ? //pack frame to component preferred sizes
? ? ? ? frame.pack();
? ? ? ? frame.setVisible(true);
? ? }

? ? public static void main(String[] args) {
? ? ? ? //Create UI on EDT
? ? ? ? SwingUtilities.invokeLater(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? new JavaSwingTextfield();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

回答by Clark

You're adding the JTextfieldto the JFrameAFTERyou've made the JFramevisible. Just add the JTextFieldbefore hand.

您正在将 加入JTextfield到使可见JFrameAFTER 中。只需添加前手。JFrameJTextField