[英]GridBagLayout in Java Column Issue
我正在嘗試使用GridBagLayout布局管理器來實現此目的:
但是,我目前得到的是:
問題是橙色和棕色/灰色面板應該占據第二列,但似乎只想在運行代碼時占據第三列。
我用於布局的代碼:
Container contentPane = form.getContentPane();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel pnlGame = new JPanel();
pnlGame.setBackground(Color.green); //temp
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.85;
c.weighty = 0.65;
contentPane.add(pnlGame, c);
JPanel pnlBuy = new JPanel();
c.gridx = 2;
pnlBuy.setBackground(Color.blue); //temp
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.15;
c.weighty = 0.46;
contentPane.add(pnlBuy, c);
JPanel pnlUpgrade = new JPanel();
pnlUpgrade.setBackground(Color.yellow); //temp
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.15;
c.weighty = 0.19;
contentPane.add(pnlUpgrade, c);
JPanel pnlStats = new JPanel();
pnlStats.setBackground(Color.red); //temp
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.61;
c.weighty = 0.35;
contentPane.add(pnlStats, c);
JPanel pnlSpeed = new JPanel();
pnlSpeed.setBackground(Color.orange); //temp
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.04;
contentPane.add(pnlSpeed, c);
JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 2;
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);
那么,我做錯了什么? 對不起,如果我的英語有點糟糕,和/或我犯的錯誤是顯而易見的......早上20到5點,我度過了漫長的一天。 很快就應該打干草了。
更新:
看來,如果我改變棕色/灰色面板的網格寬度,一切似乎都正確對齊,但最終我的布局中存在一個令人討厭的差距。 這里:
i.imgur.com/6JUx2.png
該小組的代碼(包括Kevin S建議的修正案):
JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);
那么,有什么我做錯了,或者這只是GridBagLayout的一些奇怪的行為,我將不得不忍受?
不幸的是,多虧了我編輯,我已經失去了Bala R在那里所有的嵌入。 所以,我害怕,我們回到圖像的鏈接。 現在似乎我不能發布超過兩個超鏈接,因此鏈接已在最后一個中被殺死,您需要復制並粘貼它。
謝謝,山姆
中間列中的所有組件也至少在另一列中。 因此,GridBagLayout將中間列的首選寬度計算為0,這就是您所看到的效果。
如果要確保中間列的寬度更大,請在其中放置一些僅在此列中的組件。
所以看起來你需要修復pnlRounds JPanel:
JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1; // NEEDS TO BE 1 (NOT 2)
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);
看起來你的問題是你的代碼中有c.gridx = 2
,而c.gridx = 1
應該是真正存在的。
另外,作為旁注,底部面板的重量不會增加到1.00,它們會增加到0.99。 只是認為你應該知道。
不完全適合你,但更像是一個臨時的解決方法。 如果你添加這個新的面板
JPanel pnlTemp = new JPanel();
pnlTemp.setBackground(Color.pink); //temp
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlTemp, c);
灰色面板后,它修復了布局。 不確定為什么,但也許這將有所幫助,直到你(或其他人)能夠找出正確的解決方案。 這個新的粉紅色面板不可見,但只是幫助修復布局。 這是布局后的樣子
你只是想讓布局工作嗎?
我做了一些修改。
Container contentPane = form.getContentPane();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel pnlGame = new JPanel();
pnlGame.setBackground(Color.green); //temp
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1; // one row
c.gridheight = 1; // one column
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.60;
c.weighty = 0.40;
contentPane.add(pnlGame, c);
// Added two new components
// pnlgGame and pnlggGame
// Covers up 2nd column and rows 0 and 1
JPanel pnlgGame = new JPanel();
pnlgGame.setBackground(Color.green); //temp
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 1; // one row
c.gridheight = 2; // two column
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.20;
c.weighty = 0.40;
contentPane.add(pnlgGame, c);
// Covers 2nd row and column 1
JPanel pnlggGame = new JPanel();
pnlggGame.setBackground(Color.green); //temp
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.60;
c.weighty = 0.20;
contentPane.add(pnlggGame, c);
JPanel pnlBuy = new JPanel();
pnlBuy.setBackground(Color.blue); //temp
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.20;
c.weighty = 0.40;
contentPane.add(pnlBuy, c);
JPanel pnlUpgrade = new JPanel();
pnlUpgrade.setBackground(Color.yellow); //temp
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.20;
c.weighty = 0.20;
contentPane.add(pnlUpgrade, c);
JPanel pnlSpeed = new JPanel();
pnlSpeed.setBackground(Color.BLUE); //temp
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.40;
c.weighty = 0.05;
contentPane.add(pnlSpeed, c);
JPanel pnlStats = new JPanel();
pnlStats.setBackground(Color.red); //temp
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.60;
c.weighty = 0.40;
contentPane.add(pnlStats, c);
JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.40;
c.weighty = 0.35;
contentPane.add(pnlRounds, c);
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setVisible(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.