我遇到一个问题,即在调用frame.pack-method时我的框架变得非常小。代码如下:
public class ManagementView extends JFrame{
private CardLayout cl;
private JPanel panel;
private MainlandingPanel mainLandingPanel;
private MasterViewPanel masterViewPanel;
ManagementView() {
masterViewPanel = new MasterViewPanel();
mainLandingPanel = new MainLandingPanel();
cl = new CardLayout();
panel = new JPanel(cl);
panel.setSize(1100, 770);
panel.add(mainLandingPanel, "Home");
panel.add(masterViewPanel, "MasterView");
//Create the main Frame
this.setTitle("Hello World");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setContentPane(panel);
this.pack();
this.setVisible(true);
cl.show(panel, "Home");
}
}
MasterViewPanel和MainLandingPanel都有一个null布局,但名为panel的成员有一个给定的大小,我添加了一个CardLayout,其中包含另外两个面板。 我调用frame.pack()期望它是1100 x 770,但我只得到一个空窗口。
我做错了什么? (是的我明白不建议不使用LayoutManager,但我在设计更复杂的面板时无法理解它是如何工作的。)