1

运行后未正确显示 JFrame。(我用想法)

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);
        }
    }
4

1 回答 1

2

添加所有JFrame组件时 已经可见

frame.setVisible(true);

添加组件后调用此方法。

于 2013-06-18T23:10:27.830 回答