简体   繁体   English

为什么GridBagConstraints不起作用?

[英]Why GridBagConstraints doesn't work?

I have JFrame form which must contain TextField somewhere around center of the form, I am using GridBagLayout to set TextField position on the frame. 我有JFrame表单,该表单必须在表单中心附近的某个地方包含TextField,我正在使用GridBagLayout设置TextField在框架上的位置。

The code like this below: 如下代码:

public static JFrame enterFrameDraw(JFrame frame){
    JButton btnEnter = new JButton("Sign in!");
    JLabel loginLabel = new JLabel("Login!");
    GridBagLayout gbl = new GridBagLayout();
    JTextField textFieldLogin = new JTextField();
    textFieldLogin.setBackground(Color.GRAY);
    btnEnter.addActionListener(e -> {
        try{
            Chat.btnEnterHandler(frame);
        } catch (Exception e2){
            e2.printStackTrace();
        }
    });


    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setTitle(FRM_TITLE);
    frame.setLocation(FRM_LOC_X, FRM_LOC_Y);
    frame.setSize(FRM_WIDTH, FRM_HEIGHT);
    frame.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.NORTH;
    c.fill = GridBagConstraints.NONE;
    c.gridheight = 1;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.gridx = GridBagConstraints.RELATIVE;
    c.gridy = GridBagConstraints.RELATIVE;
    c.insets = new Insets(40, 0, 0, 0);
    c.ipadx = 0;
    c.ipady = 0;
    c.weightx = 0.0;
    c.weighty = 0.0;
    gbl.setConstraints(textFieldLogin, c);
    frame.add(textFieldLogin, c);
    frame.setResizable(false);
    frame.setVisible(true);

    return frame;
}

And result seems like: Image 结果似乎是: 图片

Could the problem lies in the "frame.add()" instead of simple "add()"? 问题可能出在“ frame.add()”而不是简单的“ add()”上吗?

UPDATE: 更新:

Method enterFrameDraw() after modification. 修改后的方法enterFrameDraw()。 Still doesn't work. 仍然不起作用。

public static JFrame enterFrameDraw(JFrame frame){
    JButton btnEnter = new JButton("Sign in!");
    JLabel loginLabel = new JLabel("Login!");
    GridBagLayout gbl = new GridBagLayout();
    JTextField textFieldLogin = new JTextField();
    textFieldLogin.setBackground(Color.GRAY);
    btnEnter.addActionListener(e -> {
        try{
            Chat.btnEnterHandler(frame);
        } catch (Exception e2){
            e2.printStackTrace();
        }
    });


    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setTitle(FRM_TITLE);
    frame.setLocation(FRM_LOC_X, FRM_LOC_Y);
    frame.setSize(FRM_WIDTH, FRM_HEIGHT);
    frame.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.NORTH;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridheight = 1;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.gridx = GridBagConstraints.RELATIVE;
    c.gridy = GridBagConstraints.RELATIVE;
    c.insets = new Insets(0, 40, 0, 40);
    c.ipadx = 0;
    c.ipady = 0;
    c.weightx = 0.5;
    c.weighty = 0.0;
    gbl.setConstraints(textFieldLogin, c);
    frame.add(textFieldLogin);
    frame.setResizable(false);
    frame.setVisible(true);
    return frame;
}

UPDATE 2: What i want to have on the form. 更新2:我想在窗体上。 Image: This one 图片: 这一个

As far as I can tell there are two problems. 据我所知有两个问题。

  1. You need to set your constraints' fill to horizontal: 您需要将约束的填充设置为水平:

    c.fill = GridBagConstraints.HORIZONTAL;

  2. You need to set the constraints' weightx to a non-zero value: 您需要将约束的weightx设置为非零值:

    c.weightx = 0.5;

Edit 1: 编辑1:

There's 2 ways you can centralize the content: 有两种方法可以集中内容:

  1. Define the constraints' insets to push it in from the left and right, like so: 定义约束的插图以从左侧和右侧将其推入,如下所示:

    c.insets = new Insets(0, 40, 0, 40);

The value 40 here is just an example. 这里的值40只是一个示例。 You could change the parameters to whatever suits. 您可以将参数更改为任何合适的值。

  1. You can use javax.swing.Box to create empty grid cells. 您可以使用javax.swing.Box创建空的网格单元格。 To keep your content in the center, you can create an empty 'strut' on either side of the middle components: 为了使内容居中,您可以在中间组件的任一侧创建一个空的“ strut”:

     c.gridx = 0; c.gridy = 0; c.weightx = 0.5; frame.add(Box.createHorizontalStrut(100), c); c.gridx = 1; frame.add(textFieldLogin, c); c.gridx = 2; frame.add(Box.createHorizontalStrut(100), c); 

Again, the value of 100 here is just an example, you should tweak the parameters to your needs. 同样,这里的值100仅是一个示例,您应该根据需要调整参数。

It would be worthwhile reading up on things like Struts and Glues in the context of gridbaglayouts, they're really useful in organizing your content the way you want it. 值得在gridbaglayouts的背景下阅读诸如Struts和Glues之类的东西,它们对于按您想要的方式组织内容确实很有用。

Edit 2: 编辑2:

To add more components below the text field that's in the center, you should use the second approach of adding horizontal struts to the cells in the first row. 要在中间的文本字段下方添加更多组件,应使用第二种方法向第一行的单元格中添加水平支撑。

The idea is that putting a strut in a cell fills up space that would be used by a component, if there was a component in that cell. 这个想法是,如果在单元格中有组件,则在单元格中放置撑杆会填充该组件将使用的空间。 So by specifying the width of a horizontal strut in the first row, any following components with the same x value as the strut will share the same width as the strut. 因此,通过在第一行中指定水平撑杆的宽度,与撑杆具有相同x值的所有后续组件都将与撑杆共享相同的宽度。

So in this case, your method might look something like this: 因此,在这种情况下,您的方法可能如下所示:

public static JFrame enterFrameDraw(JFrame frame){

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setSize(500, 500);
    frame.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;

    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 1;
    frame.add(Box.createHorizontalStrut(80), c);

    JTextField textFieldLogin = new JTextField();
    textFieldLogin.setBackground(Color.GRAY);
    c.gridx = 1;
    c.gridy = 0;
    frame.add(textFieldLogin, c);

    c.gridx = 2;
    c.gridy = 0;
    frame.add(Box.createHorizontalStrut(80), c);

    JLabel label = new JLabel("Label");
    c.gridx = 0;
    c.gridy = 1;
    frame.add(label, c);

    frame.setVisible(true);

    return frame;
}

Here a label is added to the second row, and it does not appear in the center. 在这里,标签会添加到第二行,并且不会出现在中间。

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

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