[英]JPanel GridBagLayout
目前,我已經成功實現了使用jpanel的網格袋布局,並添加了7個顯示面板和一些“填充”面板以獲取所需的布局。
現在唯一的問題是這些面板可以更改大小,當它們變得非常大時,它們會影響它們上方/下方或左側/右側的面板,原因是上方/下方,左側/右側的面板共享同一列/行索引,當面板3(參見圖片)超出其默認大小時,上方/下方的面板將受到影響。
當面板只顯示幾個元素時
更常規的設置
很多(不是很經常)系統
這是更新面板的方法。 網格約束分配有很多重復的代碼,我可能應該使一個函數接受這些參數,以使其更具可讀性。
public void updateView() {
int gridXPos, gridYPos, gridWidth, gridHeight;
int maxGridWidth = 30;
Rack r;
SuctionGroup sg;
JLabel label;
JPanel panel;
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
_Panel_MainPanel.setLayout(gbl);
_Panel_MainPanel.removeAll();
// Store panel info at top
//===========================================================
// Positioning
gridXPos = 0;
gridYPos = 0;
gridWidth = maxGridWidth;
gridHeight = 5;
// Constraints
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
panel = panelTop(img, storeName);
_Panel_MainPanel.add(panel, c);
// Pressure/temp
//===========================================================
// Positioning
gridXPos = 0;
gridYPos += gridHeight;
gridWidth = 15;
gridHeight = 5;
// Constraints
c.fill = GridBagConstraints.BOTH;
c.weightx = 0;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
panel = panelPressureTemps();
_Panel_MainPanel.add(panel, c);
// SEI blank
//===========================================================
// Positioning
gridXPos += gridWidth;
//gridYPos = gridHeight;
gridWidth = 5;
gridHeight = 5;
// Constraints
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
label = new JLabel(" ");
//label.setBorder(border);
panel.add(label);
// performance
//===========================================================
// Positioning
gridXPos += gridWidth;
//gridYPos += gridHeight;
gridWidth = 10;
gridHeight = 10;
// Constraints
//c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
panel = panelPerformance();
_Panel_MainPanel.add(panel, c);
//===========================================================
// Filler area
gridYPos += gridHeight;
gridHeight = 10;
c.gridx = 0;
c.gridy = gridYPos;
c.gridwidth = 20;
c.gridheight = gridHeight;
c.ipady = 50;
JPanel p1 = new JPanel();
p1.setPreferredSize(new Dimension(panel.getWidth(), 225));
p1.setBackground(Color.black);
//p1.setBorder(border);
panel.add(p1, c);
//===========================================================
// Condenser
//===========================================================
// Positioning
gridXPos = 0;
gridYPos += gridHeight;
gridWidth = 20;
gridHeight = 5;
c.ipady = 0;
// Constraints
//c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
panel = panelCondenser();
_Panel_MainPanel.add(panel, c);
// Fan images - blanks
//===========================================================
// Positioning
gridXPos += gridWidth;
//gridYPos = gridHeight;
gridWidth = 2;
gridHeight = 5;
// Constraints
//c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
label = new JLabel("");
//label.setBorder(border);
panel.add(label);
_Panel_MainPanel.add(label, c);
// Load and efficiency
//===========================================================
// Positioning
gridXPos += gridWidth;
//gridYPos = gridHeight;
gridWidth = 8;
gridHeight = 10;
// Constraints
//c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
panel = panelLE();
_Panel_MainPanel.add(panel, c);
//===========================================================
// Filler area
gridYPos += gridHeight;
gridHeight = 10;
c.gridx = 0;
c.gridy = gridYPos;
c.gridwidth = 20;
c.gridheight = gridHeight;
c.ipady = 150;
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(panel.getWidth(), 225));
p.setBackground(Color.black);
//p.setBorder(border);
panel.add(p, c);
//===========================================================
// Compressor status
//===========================================================
// Positioning
gridXPos = 0;
gridYPos += gridHeight;
gridWidth = 15;
gridHeight = 7;
// Constraints
//c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
c.ipady = 0;
//c.ipady = 0;
// End of Constraints
//===========================================================
panel = panelCompressor();
_Panel_MainPanel.add(panel, c);
//==============================================================
// make labels white
setLabels(_Panel_MainPanel, Colours.White.getCol());
// do it before last panel
// Bottom Panel
//===========================================================
// Constraints
c.fill = GridBagConstraints.BOTH;
//c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
gridXPos = 0;
gridYPos += gridHeight;
gridWidth = maxGridWidth;
gridHeight = 5; // 5 per row for performance
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = 1;
//c.ipady = 100;
//c.ipady = 0;
// We dont setup next position because we are adding suction groups still
//gridYPos += gridHeight;
// End of Constraints
//===========================================================
panel = panelBottom(this.numRacks);
_Panel_MainPanel.add(panel, c);
_Panel_MainPanel.revalidate();
_Panel_MainPanel.repaint();
}
問題:如何在面板上設置約束,因此它們的大小不會影響相同col / row索引中面板的大小。
目前,我已經成功實現了使用jpanel的網格袋布局,並添加了7個顯示面板和一些“填充”面板以獲取所需的布局。
您永遠不會被迫使用單個布局管理器。 因此,我建議您從使用BorderLayout的主面板開始。 那么您的代碼將類似於:
add(header, BorderLayout.PAGE_START);
add(gridPanel, BorderLayout.CENTER);
add(footer, BorderLayout.PAGE_END);
不能解決您的問題,但是現在您只處理網格面板中的5個面板。
如何在面板上設置約束,以使它們的大小不會影響相同col / row索引中面板的大小。
不要以為可以。 GridBagLayout的重點是使用行/列中每個組件的信息來確定單元格的大小。 我嘗試設置組件的最大大小,但是GridBagLayout似乎不尊重它。
因此,可能的解決方案是創建自己的自定義布局管理器。 應該很困難。 您可以從BorderLayout作為模板開始,因為它允許5個組件在特定位置定位。 就您而言,您似乎在不同的位置也有5個組件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.