Java Swing Gridbaglayout看起来

时间:2015-10-06 20:47:06

标签: java swing gridbaglayout

我一直致力于创建动态大小的表的程序,该表依赖于来自另一个窗口的用户输入。代码与使用gridbaglayout的显示器直接相关,所以我不会发布它。在我的图片中你可以看到" SG"列" Comp#"和"系统#",每个新项目都会添加一个位置,即列中项目的数量在列中的项目之间被赋予相等的空间。我希望这些项目只是添加并直接在标题下。

Pic

这是重要内容的源代码。无法执行。

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;
}

调用功能

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

0 个答案:

没有答案