ウィンドウの左上隅にペインを追加する JFrame があります。それはうまくいきます。何らかの理由で、左上隅にあるペインを構成する gridbaglayout が、私が望むように正確に動作しません。
追加する最初の 3 つのボタンは最初の y 行の内側にあり、水平に追加されますが、gridY = 1 に追加する informationPane は次の行に移動せず、ボタンと同じように右に追加されます。
また、iPadY はボタンを垂直方向に拡大していないため、各ボタンに優先サイズを設定する必要がありました。
private JPanel setUpTop()
{
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(1, 2));
JPanel controlPane = new JPanel();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
controlPane.setBackground(Color.RED);
c.gridx = 0;
c.gridy = 0;
previousSongBT = new JButton("<<");
previousSongBT.setPreferredSize(new Dimension(CTRL_BT_WIDTH, CTRL_BT_HEIGHT));
controlPane.add(previousSongBT, c);
c.gridx = 1;
c.gridy = 0;
playBT = new JButton(">");
playBT.setPreferredSize(new Dimension(CTRL_BT_WIDTH, CTRL_BT_HEIGHT));
controlPane.add(playBT, c);
c.gridx = 2;
c.gridy = 0;
nextSongBT = new JButton(">>");
nextSongBT.setPreferredSize(new Dimension(CTRL_BT_WIDTH, CTRL_BT_HEIGHT));
controlPane.add(nextSongBT, c);
c.gridx = 0;
c.gridy = 1;
c.weighty = 1;
c.gridwidth = 3;
c.anchor = GridBagConstraints.PAGE_END;
JPanel informationPane = new JPanel();
informationPane.add(new JButton("Bottom pane"));
informationPane.setBackground(Color.GREEN);
controlPane.add(informationPane, c);
pane.add(controlPane);
return pane;
}
そのペインを追加するコードは次のとおりです。
//Top-Left
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.insets = new Insets(20,20,0,0); //top padding
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 0.5;
this.getContentPane().add(setUpTop(), c);
より明確にするための写真を次に示します。緑色のパネルを他の 3 つのボタンの下に配置します。