0

ウィンドウの左上隅にペインを追加する 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 つのボタンの下に配置します。

ここに画像の説明を入力

4

1 に答える 1

2

それ以外の

JPanel controlPane = new JPanel();
pane.setLayout(new GridBagLayout());

それは読むべきです

JPanel controlPane = new JPanel();
controlPane.setLayout(new GridBagLayout());

すべきではないですか??

于 2012-10-20T17:14:53.993 に答える