2009-08-14 44 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     
}