2017-02-18 11 views
1

2つの列に配置された4つのコンポーネントで構成されるJPanelを作成したいとします。左側の列の上部コンポーネントは垂直スペースの60%を占め、残りのコンポーネントは残りの40%を占める必要があります。右の列では、それは逆の方法でなければなりません。上位コンポーネントは40%、下位コンポーネントは60%です。GridBagLayoutの不均一な列

だから、基本的に私は私のコンポーネントは、この絵のようにレイアウトされているしたいと思います:私はGridBagLayoutと、この動作を実現しようとしている

enter image description here

public class Test extends JFrame { 
JPanel testPanel = new JPanel(); 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(() -> { 
     new Test().setVisible(true); 
    }); 
} 

Test() { 
    prepareTestPanel(); 
    setContentPane(testPanel); 
    setSize(500, 500); 
    setTitle("Test"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

private void prepareTestPanel() { 
    testPanel.setLayout(new GridBagLayout()); 
    addComp(makeUpperLeft()); 
    addComp(makeLowerLeft()); 
    addComp(makeUpperRight()); 
    addComp(makeLowerRight()); 
} 

private void addComp(Pair p) { 
    testPanel.add(p.comp, p.gbc); 
} 

private Pair makeUpperLeft() { 
    JPanel panel = new JPanel(); 
    panel.setBorder(BorderFactory.createTitledBorder("Upper Left")); 

    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.gridx = 0; 
    gbc.gridy = 0; 
    gbc.gridheight = 60; 
    gbc.gridwidth = 50; 
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.anchor = GridBagConstraints.LINE_START; 
    gbc.weightx = 0.5; 
    gbc.weighty = 0.6; 

    return new Pair(panel, gbc); 
} 

private Pair makeLowerLeft() { 
    JPanel panel = new JPanel(); 
    panel.setBorder(BorderFactory.createTitledBorder("Lower Left")); 

    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.gridx = 0; 
    gbc.gridy = 60; 
    gbc.gridheight = 40; 
    gbc.gridwidth = 50; 
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.anchor = GridBagConstraints.LINE_START; 
    gbc.weightx = 0.5; 
    gbc.weighty = 0.4; 

    return new Pair(panel, gbc); 
} 

private Pair makeUpperRight() { 
    JPanel panel = new JPanel(); 
    panel.setBorder(BorderFactory.createTitledBorder("Upper Right")); 

    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.gridx = 50; 
    gbc.gridy = 0; 
    gbc.gridheight = 40; 
    gbc.gridwidth = 50; 
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.anchor = GridBagConstraints.LINE_START; 
    gbc.weightx = 0.5; 
    gbc.weighty = 0.4; 

    return new Pair(panel, gbc); 
} 

private Pair makeLowerRight() { 
    JPanel panel = new JPanel(); 
    panel.setBorder(BorderFactory.createTitledBorder("Lower Right")); 

    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.gridx = 50; 
    gbc.gridy = 40; 
    gbc.gridheight = 60; 
    gbc.gridwidth = 50; 
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.anchor = GridBagConstraints.LINE_START; 
    gbc.weightx = 0.5; 
    gbc.weighty = 0.6; 

    return new Pair(panel, gbc); 
} 


private class Pair { 
    Component comp; 
    GridBagConstraints gbc; 

    public Pair(Component comp, GridBagConstraints gbc) { 
     this.comp = comp; 
     this.gbc = gbc; 
    } 
} 
} 

は、残念ながら、私が得るものです:

enter image description here

私は自分のコードを修正する必要がありますどのように?また、ウィンドウのサイズを変更する方法に関係なく、この比率を残しておきたいと思います。 weightyはどのように設定する必要がありますか?

答えて

1

これは、1つのコンテナ内で直接行うことはできません。両方の列に2つのコンテナ要素(2つ以上、JPanelオブジェクト)を作成する必要があります。外側のコンテナに列を追加し、内側のパネルにコンポーネントを追加します。ここではそれを行ういくつかのコードです:

public class Test extends JFrame { 
public Test() { 

    JPanel testPanel = new JPanel(); 
    setContentPane(testPanel); 
    GridBagLayout gbl_testPanel = new GridBagLayout(); 
    gbl_testPanel.columnWidths = new int[]{0, 0}; 
    gbl_testPanel.rowHeights = new int[]{0}; 
    gbl_testPanel.columnWeights = new double[]{0.5, 0.5}; 
    gbl_testPanel.rowWeights = new double[]{1.0}; 
    testPanel.setLayout(gbl_testPanel); 

    JPanel leftPanel = new JPanel(); 
    GridBagConstraints gbc_leftPanel = new GridBagConstraints(); 
    gbc_leftPanel.fill = GridBagConstraints.BOTH; 
    gbc_leftPanel.insets = new Insets(0, 0, 0, 5); 
    gbc_leftPanel.gridx = 0; 
    gbc_leftPanel.gridy = 0; 
    testPanel.add(leftPanel, gbc_leftPanel); 
    GridBagLayout gbl_leftPanel = new GridBagLayout(); 
    gbl_leftPanel.columnWidths = new int[]{0}; 
    gbl_leftPanel.rowHeights = new int[]{0, 0}; 
    gbl_leftPanel.columnWeights = new double[]{0.0}; 
    gbl_leftPanel.rowWeights = new double[]{0.6, 0.4}; 
    leftPanel.setLayout(gbl_leftPanel); 

    JLabel lbl00 = new JLabel("Upper Left"); 
    GridBagConstraints gbc_lbl00 = new GridBagConstraints(); 
    gbc_lbl00.anchor = GridBagConstraints.NORTH; 
    gbc_lbl00.fill = GridBagConstraints.HORIZONTAL; 
    gbc_lbl00.insets = new Insets(0, 0, 5, 0); 
    gbc_lbl00.gridx = 0; 
    gbc_lbl00.gridy = 0; 
    leftPanel.add(lbl00, gbc_lbl00); 

    JLabel lbl10 = new JLabel("Lower Left"); 
    GridBagConstraints gbc_lbl10 = new GridBagConstraints(); 
    gbc_lbl10.anchor = GridBagConstraints.NORTH; 
    gbc_lbl10.fill = GridBagConstraints.HORIZONTAL; 
    gbc_lbl10.gridx = 0; 
    gbc_lbl10.gridy = 1; 
    leftPanel.add(lbl10, gbc_lbl10); 

    JPanel rightPanel = new JPanel(); 
    GridBagConstraints gbc_rightPanel = new GridBagConstraints(); 
    gbc_rightPanel.fill = GridBagConstraints.BOTH; 
    gbc_rightPanel.gridx = 1; 
    gbc_rightPanel.gridy = 0; 
    testPanel.add(rightPanel, gbc_rightPanel); 
    GridBagLayout gbl_rightPanel = new GridBagLayout(); 
    gbl_rightPanel.columnWidths = new int[]{0}; 
    gbl_rightPanel.rowHeights = new int[]{0, 0}; 
    gbl_rightPanel.columnWeights = new double[]{0.0}; 
    gbl_rightPanel.rowWeights = new double[]{0.4, 0.6}; 
    rightPanel.setLayout(gbl_rightPanel); 

    JLabel lbl01 = new JLabel("Upper Right"); 
    GridBagConstraints gbc_lbl01 = new GridBagConstraints(); 
    gbc_lbl01.insets = new Insets(0, 0, 5, 0); 
    gbc_lbl01.fill = GridBagConstraints.HORIZONTAL; 
    gbc_lbl01.anchor = GridBagConstraints.NORTH; 
    gbc_lbl01.gridx = 0; 
    gbc_lbl01.gridy = 0; 
    rightPanel.add(lbl01, gbc_lbl01); 

    JLabel lbl11 = new JLabel("Lower Right"); 
    GridBagConstraints gbc_lbl11 = new GridBagConstraints(); 
    gbc_lbl11.anchor = GridBagConstraints.NORTH; 
    gbc_lbl11.fill = GridBagConstraints.HORIZONTAL; 
    gbc_lbl11.gridx = 0; 
    gbc_lbl11.gridy = 1; 
    rightPanel.add(lbl11, gbc_lbl11); 
} 

}

2

はGridBagLayoutのは、簡単にこれをサポートしていません:

  1. あなたはちょうどあなたが望む比でgridwidth /高さを指定することはできません。 6/4の比率が必要な場合は、実際に同じサイズのダミーコンポーネントを10個作成してからパネルに追加する必要があります。次に、コンポーネントに、それらのダミーコンポーネントのグリッド高さが4(または6)に等しいと言うことができます。

  2. weightx/y制約は、フレームがサイズ変更されるときに余分なスペースにのみ適用されます。したがって、コンポーネントは最初は好みのサイズでパックされます。これにより、希望するサイズを手動で手動で設定する必要があります。

RelativeLayoutをチェックアウトすることができます。利用可能なスペースに基づいて相対的なサイズのコンポーネントを簡単に作成することができます。

このレイアウトを使用して、基本的なコードは次のようなものになるだろう:

RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS); 
rl.setFill(true); 
JPanel left = new JPanel(rl); 
left.add(upperLeft, new Float(60)); 
left.add(lowerLeft, new Float(40)); 

// repeat above for the right panel 

JPanel main = new JPanel(new GridLayout(0, 2)); 
main.add(left); 
main.add(right); 

frame.add(main); 
+0

RelativeLayout、非常に有用であると考えられます。私はそれをチェックします、ありがとう! –

関連する問題