【问题标题】:Not displayed correctly JFrame after run [closed]运行后未正确显示 JFrame [关闭]
【发布时间】:2013-06-18 23:03:50
【问题描述】:

运行后没有正确显示 JFrame。 (我使用 IDEA)

http://i.stack.imgur.com/UH3DS.png

但是,在我隐藏窗口并重新打开后一切正常

http://i.imgur.com/lslY7D3.gif

这是我的代码或 IDEA 的问题? 代码:pastebin .com/zAmYf1GV

public class Main {

        public static void  main(String[] args){

            int width   = 300;
            int height  = 200;

    // Main frame
            JFrame frame = new JFrame("Calculator");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setMinimumSize(new Dimension(width, height));
            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

    // Main panel
            JPanel panel = new JPanel();
            frame.add(panel);

    // Box 1
            Box box1;
            box1 = Box.createHorizontalBox();
            JLabel labelNumb1 = new JLabel("Number 1:");
            JTextField textField1 = new JTextField(5);
            textField1.setMinimumSize(textField1.getPreferredSize());

            JLabel labelNumb2 = new JLabel("Number 2:");
            JTextField textField2 = new JTextField(5);
            textField2.setMinimumSize(textField1.getPreferredSize());

            box1.add(Box.createHorizontalStrut(3));
            box1.add(labelNumb1);
            box1.add(Box.createHorizontalStrut(5));
            box1.add(textField1);
            box1.add(Box.createHorizontalStrut(15));
            box1.add(labelNumb2);
            box1.add(Box.createHorizontalStrut(5));
            box1.add(textField2);

    // Box 2
            Box box2;
            box2 = Box.createHorizontalBox();
            JButton buttonPlus      = new JButton("plus");
            JButton buttonMinus     = new JButton("minus");
            JButton buttonMultiply  = new JButton("multiply");
            JButton buttonDivide    = new JButton("divide");

            box2.add(buttonPlus);
            box2.add(Box.createHorizontalStrut(2));
            box2.add(buttonMinus);
            box2.add(Box.createHorizontalStrut(2));
            box2.add(buttonMultiply);
            box2.add(Box.createHorizontalStrut(2));
            box2.add(buttonDivide);

    // Box 3
            Box box3;
            box3 = Box.createHorizontalBox();

            JLabel labelRes = new JLabel("Result:");
            box3.add(labelRes);

            panel.add(box1);
            panel.add(box2);
            panel.add(box3);
        }
    }

【问题讨论】:

  • 你的代码很可能有问题。请在此处发布您的代码。
  • @michael-myers pastebin.com/zAmYf1GV - 这里

标签: java swing intellij-idea


【解决方案1】:

添加所有组件后,JFrame 已经可见

frame.setVisible(true);

在添加组件后调用此方法。

【讨论】:

    猜你喜欢
    • 2011-12-11
    • 2015-07-29
    • 2012-09-02
    • 2013-12-27
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多