gpt4 book ai didi

java - 运行后JFrame显示不正确

转载 作者:行者123 更新时间:2023-11-30 04:20:27 27 4
gpt4 key购买 nike

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

最佳答案

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

frame.setVisible(true);

添加组件后调用此方法。

关于java - 运行后JFrame显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17180438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com