0
我試圖做一個3x2網格。在網格中我想要一張圖片和一個相應的單選按鈕。所以圖片將在(0,0)和圖片(1,0)中。 我無法使weightx和weighty系統正常工作,並且所有組件都停留在一條線上。現在我已經刪除了權重。代碼如下所示:不能使GridBagLayout與重量工作的位置
//the two numbers are corresponding with coordinates. For field00, x=0 and y=0
GridBagConstraints field00 = new GridBagConstraints();
field00.fill = GridBagConstraints.HORIZONTAL;
field00.gridx = 0;
field00.gridy = 0;
this.add(nokiaPic, field00);
GridBagConstraints field10 = new GridBagConstraints();
field10.fill = GridBagConstraints.HORIZONTAL;
field10.gridx = 1;
field10.gridy = 0;
this.add(nokiaButton, field10);
GridBagConstraints field01 = new GridBagConstraints();
field01.fill = GridBagConstraints.HORIZONTAL;
field01.gridx = 0;
this.add(princessPic, field01);
GridBagConstraints field11 = new GridBagConstraints();
field11.gridx = 1;
field11.gridy = 1;
this.add(princessButton, field11);
To give you an idea:
+--------+--------+
| | |
| pic | Button |
| | |
+--------+--------+
| | |
| pic | Button |
| | |
+--------+--------+
| | |
| pic | Button |
| | |
+--------+--------+
我知道,我沒有使用GridBagConstraints的通常慣例,但我不明白爲什麼這種方法不應該工作一樣好。我希望你可以幫助我增加重量,使它看起來像圖片在左欄中,右欄有相應的單選按鈕。
我「認爲:你想'gridwidth'和'gridheight'描述的量該組件可以擴展的單元格 – MadProgrammer
我只希望每個對象都佔用一個空間,該對象應該放在兩列之間。 – AugustM
認爲您可能需要繪製圖片 – MadProgrammer