これら 3 つのパネルの制約を設定するときは、必ず
- GridBagConstraint プロパティ
weighty
を 0 より大きい値 (1.0 など) に設定します。
- プロパティ
anchor
をNORTH
またはNORTHEAST
またはに設定しますNORTHWEST
。
もちろん、プロパティはまたはにfill
のみ設定できます。そうしないと、すべてのパネルが垂直方向に引き伸ばされます。これは望ましくないと思います。NONE
HORIZONTAL
これが私が説明するものの例です。3 つの大きなパネルを 3 つのボタンに置き換えることでケースを単純化しました (そのうちの 1 つは他のボタンよりも高くなっています)。
結果 (上に 3 つのボタンがどのように配置されているかを確認してください):

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestLayout {
protected void initUI() {
final JFrame frame = new JFrame(TestLayout.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel btmPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1.0;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = GridBagConstraints.NORTH;
JButton comp = new JButton("Panel-1");
btmPanel.add(comp, gbc);
JButton comp2 = new JButton("Panel-2");
btmPanel.add(comp2, gbc);
JButton comp3 = new JButton("Panel-3");
comp3.setPreferredSize(new Dimension(comp.getPreferredSize().width, comp.getPreferredSize().height + 10));
btmPanel.add(comp3, gbc);
frame.add(btmPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestLayout().initUI();
}
});
}
}