JPanel GridBagLayout

JPanel GridBagLayout

目前,我已经成功地使用 jpanel 实现了网格包布局,并添加了 7 个显示面板和一些 "filler" 面板以获得我想要的布局。

现在唯一的问题是这些面板可以改变大小,当它们变得非常大时,它们会影响面板 above/below 或 left/right,因为面板 [=44] =]、left/right 共享相同的 column/row 索引,当面板 3(见图)超出其默认大小时,面板 above/below 会受到影响。

当面板显示的元素很少时

更规则的设置

很多(不经常)系统

这是更新面板的方法。网格约束分配有很多重复代码,我可能应该制作一个接受这些参数的函数以使其更具可读性。

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 索引中面板的大小。

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 的要点是使用 row/column 中每个组件的信息来确定单元格的大小。我尝试设置组件的最大尺寸,但 GridBagLayout 似乎不遵守该规定。

因此,一个可能的解决方案是创建您自己的自定义布局管理器。应该有那么难。您可以从 BorderLayout 作为模板开始,因为它允许 5 个组件定位在特定位置。在您的情况下,看起来您在不同位置也有 5 个组件。