【问题标题】:How to push GridbagLayout not to lay components in the center of JPanel如何推动 GridbagLayout 不在 JPanel 的中心放置组件
【发布时间】:2010-11-19 14:44:53
【问题描述】:

问题在于组件的居中布局,GridBagLayout 总是“位于”JPanel 的中心,所以我不在乎它如何在内部布局组件,我的问题是这些组件将开始在面板上布局的位置。

我试过了:

panel.setAlignmentX( JPanel.LEFT_ALIGNMENT );

但它没有帮助。

有什么想法吗?

【问题讨论】:

    标签: java user-interface swing layout


    【解决方案1】:

    您需要添加至少一个将填充水平空间的组件。如果你没有这样的组件,你可以试试这个:

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

    【讨论】:

      【解决方案2】:

      除了设置anchorfill 字段外,您可能还需要设置weightx 字段。这有助于指定调整大小的行为。

      Quote:

      除非您为 weightx 或 weighty 指定至少一个非零值,否则所有组件都会在其容器的中心聚集在一起。这是因为当权重为 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;

      【讨论】:

        【解决方案3】:

        您只需使用此实用程序 jar 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();
        

        【讨论】:

        • -1 以一种痛苦代替另一种痛苦。只学习 GridBagLayout 会更有用。
        【解决方案4】:

        如果您想更改组件在由GridBagLayout 创建的单元格中的位置,请使用GridBagConstraints 中的参数anchor

        【讨论】:

        • 这还不够,你必须有一个组件来填充剩余的水平空间。
        • 是的,我知道;这不是问题,问题在于将整个“网格”放在持有元素的 JPanel 的左上角,而不干扰 GridBags 布置组件的内部工作。只是告诉GridBag:好吧,伙计,你做你喜欢的工作,坐在左上角而不是坐在中间
        • @as:Bombe 是正确的,您需要使用 GridBagConstraints.anchor,只是错过了有关填充组件的部分。我相信我说 GridBagLayout 不尊重 JComponent.setAlignmentX 和 JComponent.setAlignmentY 是对的,这两者都适用于组件本身而不是容器的内容。
        【解决方案5】:

        我和你有同样的问题。通过将该面板添加到另一个具有 BorderLayout 和 NORTH 约束的面板来解决它。

        翁德瑞

        【讨论】:

          【解决方案6】:

          您可以将主布局设置为flowlayout,并将对齐设置为left。在这个面板(flowlayout)中,添加一个gridbaglayout面板。这也适用于netbeans

          【讨论】:

            【解决方案7】:

            另一种解决方案是在最右侧、最底部添加两个虚拟面板(容器)。然后你调整 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                   
            }
            

            【讨论】:

              猜你喜欢
              • 2013-06-10
              • 2015-08-01
              • 1970-01-01
              • 2013-07-24
              • 1970-01-01
              • 1970-01-01
              • 2021-02-14
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多