我想知道为什么我无法调整下面的框架大小,并且在调整框架大小时无法调整所有组件的大小。谢谢!!!
public class TPASimulatorGUI extends JFrame{
JPanel mainPanel = new JPanel();
BoxLayout layout = new BoxLayout(mainPanel,BoxLayout.Y_AXIS);
mainPanel.setLayout(layout);
// add things to main panel
JPanel it = new JPanel(new FlowLayout(FlowLayout.LEADING));
it.add(mainPanel);
this.getContentPane().add(it);
this.setSize(new Dimension(1190,770));
this.setVisible(true);
}
最佳答案
JPanel it = new JPanel(new FlowLayout(FlowLayout.LEADING));
it.add(mainPanel);
FlowLayout 始终遵循添加到其中的组件的首选大小。
摆脱“it”面板并尝试仅使用
//this.getContentPane().add(it);
add(mainPanel);
框架的默认布局是 BorderLayout,它将尝试增加/减少添加到其中的所有组件的大小。
关于java - 如何使 java swing JFrame 正确调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15977420/