簡體   English   中英

GridBagLayout ipadY和定位

[英]GridBagLayout ipadY and positioning

我有一個JFrame,在窗口的左上角添加一個窗格。 這很好。 出於某種原因,構成左上角窗格的gridbaglayout並不像我想要的那樣。

我添加的前三個按鈕位於第一個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);

這是一張更清晰的圖片,我希望綠色面板低於其他三個按鈕:

在此輸入圖像描述

代替

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

它應該讀

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

不應該嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM