簡體   English   中英

如何將Jpanels擴展為GridBagLayout

[英]How to expand Jpanels into 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);

}
}

我的結果是:

在此輸入圖像描述

我想那樣:

在此輸入圖像描述

你需要設置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告訴我們如何按行與列的總重量成比例地划分行或列的空間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM