簡體   English   中英

Java GUI GridBagLayout 如何在一列中容納三個組合框?

[英]Java GUI GridBagLayout how to fit three combo box in one column?

我正在使用 Eclipse,用 Java 編程,使用 GridBagLayout 布局管理器進行學校項目。

到目前為止它看起來像這樣

在此處輸入圖片說明

我想將三個組合框(MM、DD、YYYY)放在一列(與 accNoT 和其他人對齊)但我不知道我是如何嘗試修改 weightx 或 weighty 但我什么也沒做

這是該部分的代碼

c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
c.ipadx = 50;
pane.add(accNoT, c);

c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
pane.add(cellNoT, c);



c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0.3;
c.gridx = 1;
c.gridy = 2;
pane.add(monthBox, c);

c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0.3;

c.gridx = 2;
c.gridy = 2;
pane.add(dayBox, c);

c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0.3;
c.gridx = 3;
c.gridy = 2;
pane.add(yearBox, c);

c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 3;
pane.add(planList, c);

我在圖像中添加了一些文字,所以它不會有點混亂 ps。 我正在關注https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html我剛剛修改了他們的例子

到目前為止,除了試圖找出 weightx 和 y 之外我還做了什么,直到現在我仍然不明白我將 DD 和 YYYY 組合框放在不同的列然后我將一些 gridWidth 設置為 accNoT 等於 2 但它沒有用, DD 就消失了

回答非常感謝 user85421///////////////////////////

就像user85421在評論中說的,添加gridwidth三個組合框應該是1,然后剩下的應該是3

編碼

  c.fill = GridBagConstraints.HORIZONTAL;

//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
c.ipadx = 50;
c.gridwidth = 3;
pane.add(accNoT, c);

c.fill = GridBagConstraints.HORIZONTAL;

//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 3;

pane.add(cellNoT, c);



c.fill = GridBagConstraints.HORIZONTAL;


c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;

pane.add(monthBox, c);

c.fill = GridBagConstraints.HORIZONTAL;

//c.weighty = 0.3;

c.gridx = 2;
c.gridy = 2;
c.gridwidth = 1;
pane.add(dayBox, c);

c.fill = GridBagConstraints.HORIZONTAL;

//c.weighty = 0.3;
c.gridx = 3;
c.gridy = 2;
c.gridwidth = 1;
pane.add(yearBox, c);

c.fill = GridBagConstraints.HORIZONTAL;

//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 3;
pane.add(planList, c);

c.fill = GridBagConstraints.HORIZONTAL;

//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 4;
c.gridwidth = 3;
pane.add(adjT, c);

c.fill = GridBagConstraints.HORIZONTAL;

//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 5;
c.gridwidth = 3;
pane.add(eUsageT, c);

c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 6;
c.gridwidth = 3;
pane.add(prevBAT, c);

////THIRD COLUMN//////////////////////////////////////////
c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 4;
c.gridy = 1;
c.gridwidth = 3;
pane.add(monPLR, c);

結果

在此處輸入圖片說明

是的,它看起來很可怕,但至少三個組合框可以放在一列中

您可能會在“第三列”部分注意到 gridy = 4 那是因為 YYYY 組合框的 gridy = 3

再次感謝 user85421 拯救了我們小組的生命

暫無
暫無

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

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