【问题标题】:Make JPanel to Fill Up Entire Space in GridBagLayout in Java在Java中使JPanel填满GridBagLayout中的整个空间
【发布时间】:2017-10-08 05:40:00
【问题描述】:

参考上图,我的内容面板 JPanel 表单会根据导航栏面板上点击哪个按钮而改变。在我的内容面板中有一个桌面面板。这是我的其中一个按钮的代码:

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 表单不会填满整个内容面板,我也尝试将布局更改为边框布局,我会填满整个内容面板结果,但在单击按钮时显示 JPanel 表单时会出现问题,JPanel 表单将不会使用边框布局显示。

【问题讨论】:

  • 基于btn1Performed的操作,建议大家看看CardLayout

标签: java jpanel netbeans-8 gridbaglayout


【解决方案1】:

在 Java 中让 JPanel 填满 GridBagLayout 中的整个空间

您想查看GridBagConstraints#weightxGridBagConstraints#weightxyGridBagConstraints#fill 属性。

查看How to Use GridBagLayout了解更多详情。

根据btn1Performed 中的操作,我建议您看看How to use CardLayout

【讨论】:

    猜你喜欢
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多