让 JPanel 在 Java 的 GridBagLayout 中填满整个 Space
Make JPanel to Fill Up Entire Space in GridBagLayout in Java
参考上图,我的Content Panel JPanel Form会根据点击Navigation Bar Panel上的哪个按钮而改变。在我的内容面板中有一个桌面面板。这是我的按钮之一的代码:
Panel1 = p1;
Panel2 = p2;
Panel3 = p3;
Panel4 = p4;
Panel5 = p5;
public MainFrame() {
initComponents();
GridBagLayout gbLayout = new GridBagLayout();
p1 = new Panel1();
p2 = new Panel2();
p3 = new Panel3();
p4 = new Panel4();
p5 = new Panel5();
desktop.setLayout(gbLayout);
desktop.add(p1);
desktop.add(p2);
desktop.add(p3);
desktop.add(p4);
desktop.add(p5);
}
private void btn1Performed(java.awt.event.ActionEvent evt) {
pnl1.setVisible(true);
pnl2.setVisible(false);
pnl3.setVisible(false);
pnl4.setVisible(false);
pnl5.setVisible(false);
}
我还发现我只能使用 GridBagLayout 来得到这个结果,但是我的 JPanel Form 不会填满整个内容面板,我也试过将布局更改为 Border Layout 这样我会得到填满整个内容面板结果,但在单击按钮时显示 JPanel Form 时会出现问题,JPanel Form 不会使用 Border Layout 显示。
Make JPanel to Fill Up Entire Space in GridBagLayout in Java
您想查看 GridBagConstraints#weightx
、GridBagConstraints#weightxy
和 GridBagConstraints#fill
属性。
查看 How to Use GridBagLayout 了解更多详情。
根据btn1Performed
中的操作,我建议你看一下How to use CardLayout
参考上图,我的Content Panel JPanel Form会根据点击Navigation Bar Panel上的哪个按钮而改变。在我的内容面板中有一个桌面面板。这是我的按钮之一的代码:
Panel1 = p1;
Panel2 = p2;
Panel3 = p3;
Panel4 = p4;
Panel5 = p5;
public MainFrame() {
initComponents();
GridBagLayout gbLayout = new GridBagLayout();
p1 = new Panel1();
p2 = new Panel2();
p3 = new Panel3();
p4 = new Panel4();
p5 = new Panel5();
desktop.setLayout(gbLayout);
desktop.add(p1);
desktop.add(p2);
desktop.add(p3);
desktop.add(p4);
desktop.add(p5);
}
private void btn1Performed(java.awt.event.ActionEvent evt) {
pnl1.setVisible(true);
pnl2.setVisible(false);
pnl3.setVisible(false);
pnl4.setVisible(false);
pnl5.setVisible(false);
}
我还发现我只能使用 GridBagLayout 来得到这个结果,但是我的 JPanel Form 不会填满整个内容面板,我也试过将布局更改为 Border Layout 这样我会得到填满整个内容面板结果,但在单击按钮时显示 JPanel Form 时会出现问题,JPanel Form 不会使用 Border Layout 显示。
Make JPanel to Fill Up Entire Space in GridBagLayout in Java
您想查看 GridBagConstraints#weightx
、GridBagConstraints#weightxy
和 GridBagConstraints#fill
属性。
查看 How to Use GridBagLayout 了解更多详情。
根据btn1Performed
中的操作,我建议你看一下How to use CardLayout