2009-08-14 43 views

回答

19

您需要添加至少一個將填充水平空間的組件。如果你想改變其中一個組件位於由GridBagLayout創建一個單元使用參數anchorGridBagConstraints

GridBagConstraints noFill = new GridBagConstraints(); 
noFill.anchor = GridBagConstraints.WEST; 
noFill.fill = GridBagConstraints.NONE; 

GridBagConstraints horizontalFill = new GridBagConstraints(); 
horizontalFill.anchor = GridBagConstraints.WEST; 
horizontalFill.fill = GridBagConstraints.HORIZONTAL;  

panel.add(new JLabel("Left Aligned"), noFill); 
panel.add(Box.createHorizontalGlue(), horizontalFill); 
0

:如果你沒有這樣的組件,你可以試試這個。

+0

這還不夠,你必須有一個組件來填充剩餘的水平空間。 – 2009-08-14 10:53:03

+0

是的,我知道;這不是問題,問題在於將整個「網格」放在JPanel的左上角,它不會影響GridBags內部佈局組件的工作。只是告訴GridBag:好的,傢伙,你隨心所欲地做你的工作,坐在左上角而不是坐在中心 – 2009-08-14 11:37:38

+1

@as:Bombe是正確的,你需要使用GridBagConstraints.anchor,只是錯過了一點填充組件。我相信我是對的,說GridBagLayout不尊重JComponent.setAlignmentX和JComponent.setAlignmentY,它們都適用於組件本身而不是容器的內容。 – 2009-08-14 11:45:34

0

我和你有同樣的問題。通過將該面板添加到具有BorderLayout和NORTH約束的另一個面板來解決此問題。

的Ondrej

0

您可以通過簡單地使用這個工具罐子painless-gridbag做到了。這也使您的代碼使用GridBagLayout的更漂亮,像下面

PainlessGridBag gbl = new PainlessGridBag(getContentPane(), false); 

    gbl.row().cell(lblFirstName).cell(txtFirstName).fillX() 
      .cell(lblFamilyName).cell(txtFamilyName).fillX(); 
    gbl.row().cell(lblAddress).cellXRemainder(txtAddress).fillX(); 

    gbl.doneAndPushEverythingToTop(); 
+0

-1用另一種替代一種疼痛。學習GridBagLayout更有用。 – 2014-04-09 11:49:14

8

除了設置anchorfill領域,你可能會需要設置weightx領域。這有助於指定調整行爲。

Quote

除非你至少指定一個非零的weightx或沉重的值,所有組件在其容器的中心聚集在一起。這是因爲當權重爲0.0(默認值)時,GridBagLayout會在其網格單元格和容器邊緣之間放置任何額外的空間。

以下將保持myComponent固定在NORTHWEST一角。假設thisJPanel或類似:

setLayout(new GridBagLayout()); 
GridBagConstraints c = new GridBagConstraints(); 

// Specify horizontal fill, with top-left corner anchoring 
c.fill = GridBagConstraints.HORIZONTAL; 
c.anchor = GridBagConstraints.NORTHWEST; 

// Select x- and y-direction weight. Without a non-zero weight, 
// the component will still be centered in the given direction. 
c.weightx = 1; 
c.weighty = 1; 

// Add child component 
add(myComponent, c); 

要保持子組件左對齊但垂直居中,只需設置anchor = WEST和刪除weighty = 1;

0

您可以將主佈局設置爲流佈局,並將對齊方式設置爲左側。在這個面板(flowlayout)中,添加一個gridbaglayout面板。這也是在netbeans

0

另一種解決方案是,你添加兩個虛擬面板(容器)的最右邊,最底部。然後你調整weightx和weighty來分配額外的空間。如果您將虛擬設置爲1,則將所有額外空間分配給此虛擬設備。

這是在netbeans中形成的一個例子。

package tutorial; 

/** 
* 
* @author ttn 
*/ 
public class GridBag1 extends javax.swing.JPanel { 

    /** 
    * Creates new form GridBag1 
    */ 
    public GridBag1() { 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 
     java.awt.GridBagConstraints gridBagConstraints; 

     jLabel1 = new javax.swing.JLabel(); 
     jTextField1 = new javax.swing.JTextField(); 
     jPanel1 = new javax.swing.JPanel(); 
     jPanel2 = new javax.swing.JPanel(); 
     jLabel2 = new javax.swing.JLabel(); 
     jTextField2 = new javax.swing.JTextField(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextArea1 = new javax.swing.JTextArea(); 

     setLayout(new java.awt.GridBagLayout()); 

     jLabel1.setText("jLabel1"); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 0; 
     add(jLabel1, gridBagConstraints); 

     jTextField1.setText("jTextField1"); 
     jTextField1.setMinimumSize(new java.awt.Dimension(59, 20)); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 1; 
     gridBagConstraints.gridy = 0; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 
     gridBagConstraints.weightx = 0.3; 
     add(jTextField1, gridBagConstraints); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 227, Short.MAX_VALUE) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 104, Short.MAX_VALUE) 
     ); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 2; 
     gridBagConstraints.gridy = 0; 
     gridBagConstraints.gridheight = 3; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.weightx = 1.0; 
     add(jPanel1, gridBagConstraints); 

     javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 
     jPanel2.setLayout(jPanel2Layout); 
     jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 172, Short.MAX_VALUE) 
     ); 
     jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 196, Short.MAX_VALUE) 
     ); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 3; 
     gridBagConstraints.gridwidth = 2; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.weighty = 1.0; 
     add(jPanel2, gridBagConstraints); 

     jLabel2.setText("jLabel2"); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 1; 
     add(jLabel2, gridBagConstraints); 

     jTextField2.setText("jTextField2"); 
     jTextField2.setMinimumSize(new java.awt.Dimension(59, 20)); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 1; 
     gridBagConstraints.gridy = 1; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 
     gridBagConstraints.weightx = 0.3; 
     add(jTextField2, gridBagConstraints); 

     jScrollPane1.setMinimumSize(new java.awt.Dimension(104, 64)); 

     jTextArea1.setColumns(20); 
     jTextArea1.setRows(5); 
     jScrollPane1.setViewportView(jTextArea1); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 2; 
     gridBagConstraints.gridwidth = 2; 
     gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START; 
     add(jScrollPane1, gridBagConstraints); 
    }// </editor-fold>       


    // Variables declaration - do not modify      
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JPanel jPanel2; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextArea jTextArea1; 
    private javax.swing.JTextField jTextField1; 
    private javax.swing.JTextField jTextField2; 
    // End of variables declaration     
}