目前,我已经成功地使用 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();
}
问题:如何在面板上设置约束,使其大小不会影响相同列/行索引中面板的大小。
最佳答案
Currently I have had success implementing a grid bag layout with a jpanel and have added 7 display panels and a few "filler" panels to get a layout I wanted.
您永远不会被迫使用单个布局管理器。所以我建议您从使用 BorderLayout 的主面板开始。那么你的代码将类似于:
add(header, BorderLayout.PAGE_START);
add(gridPanel, BorderLayout.CENTER);
add(footer, BorderLayout.PAGE_END);
不能解决您的问题,但现在您只处理网格面板中的 5 个面板。
How can I set constraints on the panels, so their sizing doesn't affect the size of panels in the same col/row indexes.
别以为你可以。 GridBagLayout 的要点是使用行/列中每个组件的信息来确定单元格的大小。我尝试设置组件的最大大小,但 GridBagLayout 似乎不尊重这一点。
因此,一个可能的解决方案是创建您自己的自定义布局管理器。应该是那么困难。您可以从 BorderLayout 作为模板开始,因为它允许 5 个组件放置在特定位置。在你的情况下,看起来你在不同的位置也有 5 个组件。
关于java - JPanel GridBagLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33285101/