java 如何推动 GridbagLayout 不将组件放置在 JPanel 的中心

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1277154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:55:03  来源:igfitidea点击:

How to push GridbagLayout not to lay components in the center of JPanel

javauser-interfaceswinglayout

提问by ante.sabo

the problem is in centered layout of components, GridBagLayout always 'sits' in center of JPanel, so I don't care how it will layout components inside, my problem is where these components will start laying out on a panel.

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

I tried with:

我试过:

panel.setAlignmentX( JPanel.LEFT_ALIGNMENT );

but it did not helped.

但它没有帮助。

Any idea?

任何的想法?

回答by Nick Holt

You need to add at least one component that will fill the horizontal space. If you don't have such a component you can try this:

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

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

回答by Courtney Christensen

In addition to setting the anchorand fillfields, you will likely need to set the weightxfield. This helps specify resizing behavior.

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

Quote:

报价

Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.

除非您为 weightx 或 weighty 指定至少一个非零值,否则所有组件都会聚集在其容器的中心。这是因为当权重为 0.0(默认值)时,GridBagLayout 在其单元格网格和容器边缘之间放置任何额外空间。

The following will keep myComponentanchored to the NORTHWESTcorner. Assuming thisis JPanelor similar:

以下将保持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);

To keep child components left-aligned yet vertically-centered, just set anchor = WESTand remove weighty = 1;.

要保持子组件左对齐但垂直居中,只需设置anchor = WEST和删除weighty = 1;.

回答by Tri.Bao

You can done it by simply use this utility jar painless-gridbag. It also make your code with GridBagLayout much prettier, like following

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

回答by Bombe

If you want to change where a component is located in a cell created by a GridBagLayoutuse the parameter anchorfrom GridBagConstraints.

如果要更改组件在由GridBagConstraints 中GridBagLayout的参数创建的单元格中的位置。

回答by Bombe

I had the same problem as you. Solved it by adding that Panel into another one with BorderLayout and NORTH constraint.

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

Ondrej

翁德雷

回答by ttn

You can set main layout as flowlayout and set alignment as left. in this panel (flowlayout), add a panel which is gridbaglayout. also this is worked in netbeans

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

回答by ttn

Another solution is that you add two dummy panel (container) to the most right, the most bottom. then you adjust weightx and weighty to distribute extra space. if you set 1 to the dummy then all extra space assign to this dummy.

另一种解决方案是在最右侧,最底部添加两个虚拟面板(容器)。然后你调整 weightx 和 weighty 来分配额外的空间。如果您将 1 设置为虚拟对象,则所有额外空间都分配给该虚拟对象。

this is an example forming in netbeans.

这是在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                   
}