简体   繁体   English

使JPanel填充Java的GridBagLayout中的整个空间

[英]Make JPanel to Fill Up Entire Space in GridBagLayout in Java

设计

Refer to the picture above, my Content Panel JPanel Form will change based on which button is clicked on the Navigation Bar Panel. 参考上面的图片,我的内容面板JPanel表单将根据在导航栏面板上单击哪个按钮而变化。 Inside my Content Panel there's a Desktop Pane. 在我的内容面板中有一个桌面窗格。 This is my code for one of the button: 这是我的按钮之一的代码:

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

I also found out that I can only use GridBagLayout to get this result, but my JPanel Form will not fill up the entire Content Panel, I've also tried changing the layout to Border Layout which I'll get the fill up entire Content Panel result but there will be problem when displaying JPanel Form on button clicks, JPanel Form will not be showing up using Border Layout. 我还发现我只能使用GridBagLayout来获得此结果,但是我的JPanel表单不会填满整个内容面板,我还尝试过将布局更改为Border Layout,这样我就可以填满整个内容面板结果,但是在单击按钮时显示JPanel表单时会出现问题,使用边框布局不会显示JPanel表单。

Make JPanel to Fill Up Entire Space in GridBagLayout in Java 使JPanel填充Java的GridBagLayout中的整个空间

You want to look at GridBagConstraints#weightx , GridBagConstraints#weightxy and GridBagConstraints#fill properties. 你想看看GridBagConstraints#weightxGridBagConstraints#weightxyGridBagConstraints#fill性能。

Have a look at How to Use GridBagLayout for more details. 有关更多详细信息,请参阅如何使用GridBagLayout

Based on the operations in btn1Performed , I would however suggest you have a look at How to use CardLayout 基于btn1Performed的操作,但是我建议您看看如何使用CardLayout。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM