В настоящее время я добился успеха в реализации макета сетки с помощью jpanel и добавил 7 панелей дисплея и несколько панелей «наполнителя», чтобы получить макет, который я хотел.JPanel GridBagLayout
Единственная проблема заключается в том, что эти панели могут изменять размер и когда они становятся чрезвычайно большими, они влияют на панели выше/ниже или слева/справа от них из-за того, что панели выше/ниже, доля слева и справа один и тот же индекс столбца/строки, а когда панель 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();
}
Вопрос: Как я могу установить ограничения на панели, поэтому их калибровка не влияет на размер панелей в одних и тех же индексов цв/строк.
Я попытался настроить c.gridwidth для отдельных панелей на основе количества вентиляторов/компрессоров и т. Д., Но затем воздействует на половину панели ниже нее, где первые 5 столбцов имеют размер, отличный от последних 5 столбцов. –
Вы забыли добавить вопрос – wero
Ха-ха, я не прояснил это. –