目前,我已经成功地实现了一个网格袋布局与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/行索引中面板的大小。
发布于 2015-10-22 16:09:56
目前,我已经成功地实现了一个网格袋布局与jpanel,并增加了7个显示面板和一些“填充”面板,以获得我想要的布局。
您从未被迫使用单一的布局管理器。因此,我建议您从一个使用BorderLayout的主面板开始。那么您的代码将是这样的:
add(header, BorderLayout.PAGE_START);
add(gridPanel, BorderLayout.CENTER);
add(footer, BorderLayout.PAGE_END);不能解决您的问题,但是现在您只处理网格面板中的5个面板。
我如何在面板上设置约束,这样它们的大小就不会影响在相同的col/行索引中面板的大小。
别以为你能。GridBagLayout的要点是使用行/列中每个组件的信息来确定单元格的大小。我尝试设置组件的最大大小,但是GridBagLayout似乎并不尊重这一点。
因此,一个可能的解决方案是创建您自己的自定义布局管理器。应该有那么难。您可以从BorderLayout作为模板开始,因为它允许5个组件在特定位置定位。在您的情况下,您看起来也有5个组件在不同的位置。
https://stackoverflow.com/questions/33285101
复制相似问题