Java GridBagLayout在一行中聚集在一起

时间:2014-10-12 02:49:43

标签: java swing alignment layout-manager gridbaglayout

我正在使用GridBagLayoutJButtonsJLabelsJTextFields对齐到登录表单中。但有时候所有的物体都挤在一条线上。 (不是总是,它是随机发生的,但经常被忽略)。

这就是我对齐对象的方式

package test;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

public class ManagerMain {
    public static JFrame frame = new JFrame("this is a frame");
    private JLabel loginNameLB = new JLabel("username:");
    private JLabel loginPasswordLB = new JLabel("password:");
    private JTextField loginName = new JTextField();
    private JPasswordField loginPassword = new JPasswordField();
    private JButton loginBTN = new JButton("Login");
    private JButton loginCreateBTN = new JButton("Create An Account");
    private Container login = new Container();
    //login is a container
    public ManagerMain() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


        frame.setLayout(new BorderLayout());
        login.setLayout(new GridBagLayout());
        GridBagConstraints gbc1 = new GridBagConstraints();
        gbc1.insets = new Insets(2, 2, 15, 15);
        gbc1.weightx = 1.0;
        gbc1.weightx = 1.0;
        //user name label
        gbc1.gridx = 0;
        gbc1.gridy = 0;
        gbc1.gridwidth = 1;
        gbc1.gridheight = 1;
        gbc1.fill = GridBagConstraints.VERTICAL;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        login.add(loginNameLB, gbc1);
        //user name text field
        gbc1.gridx = 1;
        gbc1.gridy = 0;
        gbc1.gridwidth = 2;
        gbc1.gridheight = 1;
        gbc1.fill = GridBagConstraints.VERTICAL;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        login.add(loginName, gbc1);
        //password label
        gbc1.gridx = 0;
        gbc1.gridy = 1;
        gbc1.gridwidth = 1;
        gbc1.gridheight = 1;
        gbc1.fill = GridBagConstraints.VERTICAL;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        login.add(loginPasswordLB, gbc1);
        //password text field
        gbc1.gridx = 1;
        gbc1.gridy = 1;
        gbc1.gridwidth = 2;
        gbc1.gridheight = 1;
        gbc1.fill = GridBagConstraints.VERTICAL;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        login.add(loginPassword, gbc1);
        //login button
        gbc1.gridx = 1;
        gbc1.gridy = 2;
        gbc1.gridwidth = 1;
        gbc1.gridheight = 1;
        gbc1.fill = GridBagConstraints.VERTICAL;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        login.add(loginBTN, gbc1);
        //create account button
        gbc1.gridx = 2;
        gbc1.gridy = 2;
        gbc1.gridwidth = 1;
        gbc1.gridheight = 1;
        gbc1.fill = GridBagConstraints.VERTICAL;
        gbc1.fill = GridBagConstraints.HORIZONTAL;
        login.add(loginCreateBTN, gbc1);
        frame.add(login, BorderLayout.CENTER);//add container to frame
    }
}

有什么建议吗?它看起来像/它应该是什么样的图像是吼叫

Screenshot 1

有时也会这样做:

Screenshot 2

1 个答案:

答案 0 :(得分:3)

  

它随机发生但经常被忽略。有什么建议吗?

  1. 您应该在将所有组件添加到框架后调用setVisible(...)
  2. 您应该在setVisible()..
  3. 之前调用pack()
  4. 确保在事件调度线程(EDT)上创建GUI组件