简体   繁体   English

JTextField清除Jframe

[英]JTextField clears Jframe

I have a JFrame that includes a working button, along with a label. 我有一个JFrame,其中包含一个工作按钮以及一个标签。 The program works fine, when I click the button I get a popup message that says "starting." 该程序运行正常,当我单击按钮时,我会弹出一条消息,提示“正在启动”。 But if I try to add a JTextField, when I run the program the frame is blank, for field, button, or label. 但是,如果我尝试添加JTextField,则在运行程序时,该框对于字段,按钮或标签来说是空白的。

The working code without the field is below. 没有该字段的工作代码如下。

    JFrame frame = new JFrame("Test");
    frame.setSize(750,300);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);



    JPanel panel = new JPanel(false);



    JLabel label = new JLabel("The Game.");



    JButton button = new JButton("Start");
    button.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e)
        {
            //Runs this code when button is pressed
            JOptionPane.showMessageDialog(null, "Starting");
        }
    }
            ); 
    button.setContentAreaFilled(true);
    button.setEnabled(true);
    button.setToolTipText("Starts");
    button.setVisible(true);






    frame.add(panel);
    panel.add(label);
    panel.add(button);

Code with the field that does not work is below. 带有无效字段的代码如下。

    JFrame frame = new JFrame("Test");
    frame.setSize(750,300);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);



    JPanel panel = new JPanel(false);



    JLabel label = new JLabel("The Game.");



    JButton button = new JButton("Start");
    button.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e)
        {
            //Runs this code when button is pressed
            JOptionPane.showMessageDialog(null, "Starting");
        }
    }
            ); 
    button.setContentAreaFilled(true);
    button.setEnabled(true);
    button.setToolTipText("Starts");
    button.setVisible(true);







    JTextField field = new JTextField("test", 20);
    field.setEnabled(true);
    field.setVisible(true);







    frame.add(panel);
    panel.add(label);
    panel.add(button);




    panel.add(field);

So somehow those 4 lines of code are clearing the frame. 因此,以某种方式,这四行代码清除了框架。

Call frame.setVisible(true) in the last line(after all components added). 在最后一行(添加所有组件之后frame.setVisible(true)调用frame.setVisible(true) )。 You are calling it before adding the components. 您在添加组件之前先调用它。 to frame 构图

if you put 如果你把

        frame.pack(); or
frame.setVisible(true);

at the bottom of the code it works. 在代码的底部。

Try yo move your: frame.setVisible(true); 尝试移动您的: frame.setVisible(true); as the last statement. 作为最后的陈述。

Move 移动

frame.setVisible(true);

as the last call. 作为最后一个电话。 To find out more information, please check the link below. 要了解更多信息,请检查下面的链接。

Why shouldn't I call setVisible(true) before adding components? 为什么在添加组件之前不应该调用setVisible(true)?

There is no need to explicitly set visible property of button to true. 无需将button的visible属性显式设置为true。 Add your panel to the Container of your frame and simple set the visible property of the fame to true at the end. 将面板添加到框架的Container中,然后在最后简单地将名望的visible属性设置为true。

frame.getContentPane().add(panel);
....
frame.setVisible(true);

It seems you never made the frame visible at the end. 看来您永远都不会在最后看到框架。 To do this, just put frame.setVisible(true); 为此,只需放入frame.setVisible(true); at the end. 在末尾。

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

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