Java Swing Gridbaglayout外观

Java Swing Gridbaglayout外观,java,swing,gridbaglayout,Java,Swing,Gridbaglayout,我一直在开发一个程序,该程序创建一个动态大小的表,该表依赖于来自另一个窗口的用户输入。代码与我使用gridbaglayout显示的内容没有直接关系,所以我不会发布它。在我的图片中,您可以看到,在“SG”列“Comp#”和“System#”下,每个新项目都会添加到一个位置,即该列中的项目数在该列中的项目之间具有相等的空间。我希望这些项目只是添加和标题下的直接 下面是一些重要内容的源代码。无法执行 public JPanel panelCondenser(int numCond, int numS

我一直在开发一个程序,该程序创建一个动态大小的表,该表依赖于来自另一个窗口的用户输入。代码与我使用gridbaglayout显示的内容没有直接关系,所以我不会发布它。在我的图片中,您可以看到,在“SG”列“Comp#”和“System#”下,每个新项目都会添加到一个位置,即该列中的项目数在该列中的项目之间具有相等的空间。我希望这些项目只是添加和标题下的直接

下面是一些重要内容的源代码。无法执行

public JPanel panelCondenser(int numCond, int numSg) {

    // Condenser Panel will list the condensers 
    JLabel label;
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    // Return a panel containing condenser labels
    JPanel panel = new JPanel(gbl);
    int numCols = numSg + 1;
    double numPerCol = Math.ceil((double) numCond / numCols);
    //System.out.println("Ceil of " + numCond + "/" + numCols + "=" + numPerCol);

    int numAdded = 0;
    //===========================
    // RACK CONDENSER
    //===========================
    //label = new JLabel("Condensers");
    //label.setFont(font);
    //label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    c.fill = GridBagConstraints.HORIZONTAL;
    //c.gridx = 0;
    c.weightx = 1;
    c.weighty = 1;
    //c.gridy = 0;
    //c.gridheight = 1;
    //panel.add(label, c);

    int colIndex = 0;
    int rowIndex = 2;

    for (int i = 1; i <= numCond; i++) {

        if (numAdded < numPerCol) {
            c.gridx = colIndex;
            c.gridy = rowIndex++;
            numAdded++;
        } else {
            numAdded = 1;
            colIndex++;
            rowIndex = 2;
            c.gridx = colIndex;
            c.gridy = rowIndex++;
        }

        label = new JLabel("Fan " + i);
        label.setFont(font);
        label.setBorder(border);
        c.fill = GridBagConstraints.HORIZONTAL;
        label.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        panel.add(label, c);
    }

    panel.setBorder(border);
    return panel;
}

public JPanel panelCompressor(int numComp, int sgNum) {

    // Condenser Panel will list the condensers 
    JLabel label;
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    // Return a panel containing condenser labels
    JPanel panel = new JPanel(gbl);

    //===========================
    // RACK COMPRESSOR
    //===========================
    label = new JLabel("SG " + sgNum);
    label.setBorder(border);
    label.setFont(font);
    label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    c.gridx = 0;
    c.weightx = 1;
    c.weighty = 0;
    c.gridy = 0;
    c.gridheight = 1;

    panel.add(label, c);
    //c.fill = GridBagConstraints.BOTH;
    //==========================================================
    //                  Compressors
    //==========================================================
    for (int i = 1; i <= numComp; i++) {
        label = new JLabel("Comp " + i + "      ");
        label.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = i;
        //c.weighty = 1;
        label.setFont(font);
        label.setBorder(border);
        panel.add(label, c);
    }

    panel.setBorder(border);
    return panel;
}

public JPanel panelSystems(int numSystems, SuctionGroup sg, int sgNum) {

    // Condenser Panel will list the condensers 
    JLabel label;
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    // Return a panel containing condenser labels
    JPanel panel = new JPanel(gbl);
    //==========================================================
    // SYSTEMS
    //==========================================================
    label = new JLabel("SG " + sgNum);
    label.setBorder(border);
    label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    c.fill = GridBagConstraints.HORIZONTAL;   
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    c.gridx = 0;
    c.weightx = 1;
    c.weighty = 1;        
    c.gridy = 0;
    c.gridheight = 1;

    label.setFont(font);
    panel.add(label, c);
    //c.gridheight = 8;
    //c.fill = GridBagConstraints.BOTH;

    for (int i = 1; i <= numSystems; i++) {
        label = new JLabel(sg.getSystemNameIndex((i - 1)) + "      ");
        label.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        c.fill = GridBagConstraints.HORIZONTAL;
        //c.anchor = GridBagConstraints.LINE_START;
        c.gridx = 0;
        c.gridy = i;
        label.setFont(font);
        label.setBorder(border);
        panel.add(label, c);
    }
    panel.setBorder(border);
    return panel;
}
publicJPanel面板冷凝器(int numCond,int nummsg){
//冷凝器面板将列出冷凝器
JLabel标签;
GridBagLayout gbl=新的GridBagLayout();
GridBagConstraints c=新的GridBagConstraints();
//返回包含冷凝器标签的面板
JPanel面板=新JPanel(gbl);
int numCols=numSg+1;
double numPerCol=Math.ceil((double)numCond/numCols);
//System.out.println(“单元数为“+numCond+”/“+numCols+”=“+numPerCol”);
int numAdded=0;
//===========================
//机架式冷凝器
//===========================
//标签=新JLabel(“冷凝器”);
//label.setFont(字体);
//label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
c、 填充=GridBagConstraints.HORIZONTAL;
//c、 gridx=0;
c、 权重x=1;
c、 权重=1;
//c、 gridy=0;
//c、 网格高度=1;
//面板。添加(标签,c);
int-colIndex=0;
int rowIndex=2;

对于(int i=1;i)您不必对整个表使用单个GridBagLayout。嵌套布局既常见又推荐。我不确定自己是否确切理解您的目标,但我怀疑您可以简单地将列的每个项目列表表示为单个GridBagLayout单元格中的,锚定值为PAGE_START或FIRST_LINE_START。1) 这个问题与其他问题有何不同你似乎忽略了camickr在这个问题上提出的关于使用多个布局的建议,我猜你会忽略@VGR在这个问题上提出的相同建议,所以在我们告诉你你想听到什么之前,你是不是一直在问同一个问题?请不要编辑并删除这个问题。我们是来回答这个问题的不仅是为了你,也是为了每一个将来访问此页面的人。
public void updateView() {

    // Vars used        
    int[] rackGridWidth = new int[5];
    int gridXPos, gridYPos, gridWidth, gridHeight;
    Rack r;
    SuctionGroup sg;
    JLabel label;
    JPanel panel = new JPanel();
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();

    _Panel_MainPanel.setLayout(gbl);
    _Panel_MainPanel.removeAll();

    // First initial cell
    label = new JLabel("Outside Air Temp");
    label.setFont(font);
    panel.add(label);
    panel.setBorder(border);
    //===========================================================
    // Positioning
    gridXPos = 0;
    gridYPos = 0;
    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;        
    // Setup next position
    gridXPos += gridWidth;
    //gridYPos += gridHeight;

    // End of Constraints
    //===========================================================        
    _Panel_MainPanel.add(panel, c);

    // rack names + SEI, they have their own panels, incase we want borders
    for (int i = 0; i < this.numRacks; i++) {

        r = racks[i];
        // Number of suction groups for the rack          

        panel = panelRackName(r.getName());
        // For each new rack panel, we must assign the grid width
        // to be 3 * num Suctiongroups
        rackGridWidth[i] = 3 * r.numSuctionGroups; // 1 cell per compressor
        // 2 cells per system
        //===========================================================            
        // 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 = rackGridWidth[i];
        //c.gridheight = gridHeight;
        //c.ipady = 100;
        //c.ipady = 0;                  
        // Setup next position
        gridXPos += rackGridWidth[i];
        // End of Constraints
        //===========================================================
        _Panel_MainPanel.add(panel, c);

    }

    //===========================
    // RACKS STATUS Condenser Fans
    //===========================
    //===========================================================
    // Positioning
    gridXPos = 0;
    gridYPos += gridHeight;
    gridWidth = 2;
    gridHeight = 10;
    // 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 * 2;
    c.ipady = 0;
    //c.ipady = 0;        
    // Setup next position
    gridXPos += gridWidth;
    //gridYPos += gridHeight;
    // End of Constraints
    //=========================================================== 

    //===========================================================
    GridBagLayout gblStatus = new GridBagLayout();
    GridBagConstraints c1 = new GridBagConstraints();
    //===========================================================            
    // Constraints         for c2
    c1.fill = GridBagConstraints.BOTH;        
    c1.weightx = 1;
    c1.weighty = 0; // No space between bottom and below row?        
    c1.gridx = 0;
    c1.gridy = 0;
    c1.gridwidth = 2;
    c1.gridheight = 5; // 2 spots per row
    c1.ipady = 0;
    //c1.ipadx = 0;                  
    // We dont setup next position because we are adding suction groups still
    // End of Constraints
    //===========================================================
    // Rack status
    panel = new JPanel(gblStatus);
    label = new JLabel("Condenser Fans");
    label.setFont(font);
    label.setBorder(border);
    panel.add(label, c1);
    //===========================================================            
    // Constraints         for c1
    c1.fill = GridBagConstraints.HORIZONTAL;        
    //c1.weightx = 1;
    c1.weighty = 1; // No space between bottom and below row?        
    //c1.gridx = 0;
    c1.gridy = 5;
    //c1.gridwidth = 2;
    c1.gridheight = 14; // 2 spots per row
    c1.ipady = 0;
    //c1.ipadx = 0;                  
    // We dont setup next position because we are adding suction groups still
    // End of Constraints
    //===========================================================
    label = new JLabel("Suction Groups");
    label.setFont(font);
    label.setBorder(border);
    panel.add(label, c1);

    label = new JLabel("blank");
    label.setBorder(border);
    c1.gridy = 19;
    c1.gridheight = 1;
    //c1.ipady = 75;
    //panel.add(label, c1);

    panel.setBorder(border);
    _Panel_MainPanel.add(panel, c);        

    // Add condensers
    for (int i = 0; i < this.numRacks; i++) {
        r = racks[i];
        // Number of suction groups for the rack
        //===========================
        // RACK CONDENSER
        //===========================
        panel = panelCondenser(r.getNumCondenserFans(), r.getNumSuctionGroups());

        //===========================================================            
        // 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 = rackGridWidth[i];
        c.gridheight = gridHeight;            
        c.ipady = 0;                  
        // We dont setup next position because we are adding suction groups still
        //gridXPos += rackGridWidth[i];
        //gridYPos += gridHeight;
        // End of Constraints
        //===========================================================
        _Panel_MainPanel.add(panel, c);

        // Suction Groups - Compressors then Systems   
        for (int j = 0; j < r.numSuctionGroups; j++) {

            sg = r.getSuctionGroupIndex(j);

            // Compressor
            panel = panelCompressor(sg.getNumCompressors(), (j + 1));
            //===========================================================            
            // Constraints        
            //c.fill = GridBagConstraints.BOTH;        
            //c.weightx = 1;
            c.weighty = 0; // No space between bottom and below row?        
            c.gridx = gridXPos;
            c.gridy = gridYPos + gridHeight;
            c.gridwidth = 1;
            //c.gridheight = gridHeight;
            //c.ipady = 100;
            //c.ipady = 0;                  
            // We dont setup next position because we are adding suction groups still
            gridXPos += 1;
            //gridYPos += gridHeight;
            // End of Constraints
            //===========================================================

            _Panel_MainPanel.add(panel, c);
            //c.gridx = gridXPos;
            //c.fill = GridBagConstraints.HORIZONTAL;

            //gridWidth = 1;
            //c.gridwidth = gridWidth;
            //gridXPos += gridWidth;
            // System
            panel = panelSystems(sg.getNumSystems(), sg, (j + 1));
            //===========================================================            
            // 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 = 2;
            //c.gridheight = gridHeight;
            //c.ipady = 100;
            //c.ipady = 0;                  
            // We dont setup next position because we are adding suction groups still
            gridXPos += 2;
            //gridYPos += gridHeight;
            // End of Constraints
            //===========================================================
            _Panel_MainPanel.add(panel, c);
        }
    } .... more code after like validate/repaint