简体   繁体   English

如何使用GridBagLayout使所有组件出现在JPanel的中心?

[英]How make all components appear in center of the JPanel using GridBagLayout?

I have below code. 我有下面的代码。 But I want all the components to appear in the center of the JPanel. 但是我希望所有组件都出现在JPanel的中心。 I have already spent countless hours on this but failed every time. 我已经为此花费了无数小时,但每次都失败了。 Can someone please help? 有人可以帮忙吗?

package tg.com.bugtracker;

import java.awt.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class LoginPanel extends JPanel {    
    LoginPanel() {
        GridBagLayout layout = new GridBagLayout();
        setLayout(layout);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.LAST_LINE_END;
        add(new JLabel("Username"), constraints);
        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.LAST_LINE_START;
        JComboBox<String> combobox = new JComboBox<>();
        combobox.setPreferredSize(new Dimension(250, 20));
        add(combobox, constraints);
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.anchor = GridBagConstraints.FIRST_LINE_END;
        add(new JLabel("Password"), constraints);
        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.anchor = GridBagConstraints.FIRST_LINE_END;
        JTextField textfield = new JTextField();
        textfield.setPreferredSize(new Dimension(250, 20));
        add(textfield, constraints);
    }
}

One fairly easy way to center a group of components in an area of a GUI is to lay them out in a panel & create a (or another) panel with GridBagLayout . 在GUI区域中居中一组组件的一种相当简单的方法是将它们布置在面板中,并使用GridBagLayout创建一个(或另一个)面板。 Then place the panel with the components into the GBL as a single component with no constraint. 然后,将带有组件的面板作为单个组件放到GBL中,没有任何约束。 Job done! 任务完成!


As an aside, when I created an MCVE (Minimal Complete Verifiable Example) of that code, I dropped an instance of LoginPanel into the CENTER of a BorderLayout and it sure seems to be centered. LoginPanel ,当我创建该代码的MCVE(最小完整可验证示例)时,我将LoginPanel一个实例放到BorderLayoutCENTER ,并且它似乎居中。

General tip 一般提示

See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? 请参阅我应该避免在Java Swing中使用set(Preferred | Maximum | Minimum)Size方法吗? (Yes.) (是。)

The size of a text field can be suggested by the number of columns and the font size. 可以通过列数和字体大小来建议文本字段的大小。 A combo box will take the size of the content, which comes down to the widest combo item and the renderer used. 组合框将采用内容的大小,具体取决于最宽的组合项和所使用的渲染器。

Ok, so first of all I quite don't understand your question, I think you want to put them in the center, but I don't know why you're using anchors. 好的,首先我完全不理解您的问题,我想您想将它们放在中间,但是我不知道为什么要使用锚点。 The anchors you're using will make the objects appear in the Line End, or in the Line Start, but you want them in the center so you should use: 您正在使用的锚点将使对象出现在“线端”或“线起点”中,但是您希望它们在中心,因此应使用:

constraints.anchor = GridBagConstraints.CENTER; //BIG MOTHER OF GOD REDUNDANCE

Which is a redundance because the default value for anchor is Center, and you shouldn't use setBounds, and I don't know why you are setting the size, the grid will always be of the size of the largest object in the column. 这是一个冗余,因为锚点的默认值是Center,并且您不应该使用setBounds,而且我也不知道为什么要设置大小,因此网格将始终具有列中最大对象的大小。 If what you're trying to do there is fill the grid you should use: 如果您要尝试填写的是网格,则应使用:

constraints.fill = GridBagConstraints.HORIZONTAL;

Or vertical or Both depending the direction where you want to fill it. 或垂直或两者都取决于您要填充的方向。 So if you want to all be displayed at the Center here's your "corrected code": 因此,如果您想全部显示在中心,则这里是您的“更正代码”:

package tg.com.bugtracker;

import java.awt.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class LoginPanel extends JPanel {    
    LoginPanel() {
        GridBagLayout layout = new GridBagLayout();
        setLayout(layout);
        GridBagConstraints constraints = new GridBagConstraints(); 
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.CENTER; //Redundance but I leaved it for you to see the change.
        add(new JLabel("Username"), constraints);
        constraints.gridx = 1;
        constraints.gridy = 0;
        JComboBox<String> combobox = new JComboBox<>();
        combobox.setPreferredSize(new Dimension(250, 20));
        add(combobox, constraints);
        constraints.gridx = 0;
        constraints.gridy = 1;
        add(new JLabel("Password"), constraints);
        constraints.gridx = 1;
        constraints.gridy = 1;
        JTextField textfield = new JTextField();
        textfield.setPreferredSize(new Dimension(250, 20));
        add(textfield, constraints);

    }
}

Now all the objects should appear in the center, I recommend you this post here in Stack Overflow about GridBagLayout, is quite interesting and it has common errors and covers almost all about this awesome layout. 现在所有对象都应该出现在中间,我建议您在Stack Overflow上有关GridBagLayout的这篇文章非常有趣,它有常见的错误,几乎涵盖了所有有关该出色布局的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM