java - 如何将 Jpanels 展开成 GridBagLayout

标签 java jpanel size gridbaglayout

我的问题是如何将我的 3 个 JPanel 扩展到我的 GrigBadLayout 中。 我添加了 2 张照片,第一张样本并显示结果,第二张是我想要的。

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JPanel;


public class ListMembersView extends JPanel{
    JPanel headerPanel;
    JPanel containerPanel;
    JPanel footerPanel;

    public ListMembersView(){

        initComponents();
        initLayout();
    }

private void initComponents(){

    this.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255),5));

    headerPanel = new JPanel();
    headerPanel.setBackground(Color.red);
    containerPanel = new JPanel();
    containerPanel.setBackground(Color.yellow);
    footerPanel = new JPanel();
    footerPanel.setBackground(Color.blue);
}    

private void initLayout(){    
    this.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    /*Not expand JPanel?*/
    c.fill = GridBagConstraints.HORIZONTAL;

    c.gridx=0; c.gridy=0; 
    this.add(headerPanel,c);

    c.gridx=0; c.gridy=1; 
    c.gridwidth = 3;
    this.add(containerPanel,c);

    c.gridx=0; c.gridy=2; 
    this.add(footerPanel,c);

}
}

我的结果是:

enter image description here

我愿意这样:

enter image description here

最佳答案

您需要将 columnWeights = {1}rowWeights = {1, 1,1}fill 都设置为:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class ListMembersView extends JPanel{
    JPanel headerPanel;
    JPanel containerPanel;
    JPanel footerPanel;

    public ListMembersView(){

        initComponents();
        initLayout();
    }

private void initComponents(){

    this.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255),5));

    headerPanel = new JPanel();
    headerPanel.setBackground(Color.red);
    containerPanel = new JPanel();
    containerPanel.setBackground(Color.yellow);
    footerPanel = new JPanel();
    footerPanel.setBackground(Color.blue);
}    

private void initLayout(){    
    GridBagLayout gridBagLayout = new GridBagLayout();
    this.setLayout(gridBagLayout);

    gridBagLayout.columnWeights = new double[] {1};
    gridBagLayout.rowWeights = new double[] {1, 1, 1};
    GridBagConstraints c = new GridBagConstraints();
    /*Not expand JPanel?*/
    c.fill = GridBagConstraints.BOTH;

    c.gridx=0; c.gridy=0; 
    this.add(headerPanel,c);

    c.gridx=0; c.gridy=1; 
    c.gridwidth = 3;
    this.add(containerPanel,c);

    c.gridx=0; c.gridy=2; 
    this.add(footerPanel,c);

}

public static void main(String[] args) {
    JFrame frame = new JFrame("test");
    frame.setLayout(new BorderLayout());
    frame.add(new ListMembersView());
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

fill=BOTH teels,面板应该在两个方向上延伸。

weights 说明如何根据权重的总和按比例划分行或列之间的空间。

关于java - 如何将 Jpanels 展开成 GridBagLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40128569/

相关文章:

java - 使用 PDFBox 解析 PDF 文件(尤其是带有表格的文件)

java - 在应用程序中添加到 JPanel

java - JPanel 仅显示添加的第一个自定义 JComponent

size - valgrind - 无效的写入大小

使用 FFMPeg 编码器库时 Android apk 文件太大

javascript - 动态 .length jquery 函数

java - 我们可以在 TestNG 中使用带有 expectedExceptions 的条件吗

java - Android ContentProvider getType() 调用的时间和原因

java - 以编程方式关闭 Eclipse 应用程序

java - JPanel 和 GridLayout