2016-10-19 8 views
2

私の質問は、3つのJPanelを私のGrigBadLayoutに拡張する方法です。 私は2枚の写真を追加しました。最初のサンプルが表示され、結果と2番目のサンプルが表示されます。JpanelsをGridBagLayoutに展開する方法

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

+0

ありがとう@luka !!!!!! – Arkhan6

答えて

1

あなたが好きなの両方に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のパネルは、パネルを両方向に伸ばす必要があります。

weightsは、行または列の空間を重みの総和に比例してどのように分割するかを示します。

+0

ありがとうございます!解決策が有効です。 – Arkhan6

+0

パネルにパーセンテージを与えたいと思っています。私が共有する解決策を見つけたら、センターパネルの高さはさらに高くなります。 – Arkhan6

+0

行を変更するだけです: 'gridBagLayout.rowWeights = new double [] {1、1、1};' 'gridBagLayout.rowWeights = new double [] {1、2、1};'または、他の何か –

関連する問題