如何将一组组件集中在JPanel中,无论其大小如何

时间:2013-11-10 22:04:05

标签: java swing jframe jpanel size

我是java新手,我正在编写一个登录框架,在成功登录后(到mysql数据库(使用xampp的localhost))关闭自己并打开主框架。

我的登录屏幕有一个欢迎标签,一个用户名标签,一个密码标签,一个文本字段和一个密码字段以及一个登录按钮。我希望这些组件在窗口中居中并保持居中并且不改变它们的大小,无论jpanel变得多大(通过在屏幕上将其拖动得更大或更小)。

我的问题是,如果它发生变化,我如何让它们保持居中并使其位置适应面板尺寸? 让我们说面板尺寸为100 * 100,按钮为50/50,面板变为200/200,但按钮​​保持在50/50,如果我将其设置为水平或垂直可调整,它只会变大,但我希望它保持相同的大小,只需将其位置调整为100/100。

我无法张贴我的画面的图像,因为它似乎需要声誉,我没有因为我刚创建了我的帐户。我希望你能想象我的意思和现在的情况。

我使用GroupLayout的LoginFrame代码如下(我离开了actionlistener,因为它不相关):

public LoginFrame() {
     setTitle("LoginFrame");
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setBounds(100, 100, 363, 270);
     contentPane = new JPanel();
     contentPane.setBackground(Color.ORANGE);
     contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
     setContentPane(contentPane);

     JLabel lblWelcome = new JLabel("Welcome");

     JLabel lblNewLabel = new JLabel("Username: ");

     textField = new JTextField();
     textField.setColumns(10);

     JLabel lblNewLabel_1 = new JLabel("Password: ");

     passwordField = new JPasswordField();

     JButton btnLogin = new JButton("Login");

        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addGap(107)
                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                    .addComponent(lblNewLabel)
                    .addComponent(lblNewLabel_1))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                    .addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                    .addComponent(textField, GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE))
                .addGap(75))
            .addGroup(gl_contentPane.createSequentialGroup()
                .addGap(145)
                .addComponent(btnLogin)
                .addContainerGap(145, Short.MAX_VALUE))
            .addGroup(gl_contentPane.createSequentialGroup()
                .addGap(149)
                .addComponent(lblWelcome)
                .addContainerGap(155, Short.MAX_VALUE))
    );
    gl_contentPane.setVerticalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addGap(56)
                .addComponent(lblWelcome)
                .addGap(18)
                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                    .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNewLabel))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                    .addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNewLabel_1))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addComponent(btnLogin)
                .addContainerGap(64, Short.MAX_VALUE))
    );
    contentPane.setLayout(gl_contentPane);
 }

我希望我能得到一些有用的答案,我试着具体,告诉我是否有什么遗漏。感谢

1 个答案:

答案 0 :(得分:0)

出于这个目的,我建议您使用GridBagLayouttutorial)而不是GroupLayout,因为它更简单,更灵活。它适用于GridBagConstraintsread about it parametrs)。

下一个代码在GridBagLayout的帮助下执行您想要的操作。

public class LoginFrame extends JFrame {

private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;

public LoginFrame() {
    setTitle("LoginFrame");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 363, 270);
    contentPane = new JPanel();
    contentPane.setBackground(Color.ORANGE);
    contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
    setContentPane(contentPane);

    JLabel lblWelcome = new JLabel("Welcome");

    JLabel lblNewLabel = new JLabel("Username: ");

    textField = new JTextField();
    textField.setColumns(10);

    JLabel lblNewLabel_1 = new JLabel("Password: ");

    passwordField = new JPasswordField();

    JButton btnLogin = new JButton("Login");

    contentPane.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(5, 5, 5, 5);

    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 2;
    c.anchor = GridBagConstraints.CENTER;
    contentPane.add(lblWelcome, c);

    c.gridwidth = 1;
    c.gridy = 1;
    contentPane.add(lblNewLabel, c);

    c.gridx = 1;
    contentPane.add(textField, c);

    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridy = 2;
    contentPane.add(passwordField, c);

    c.fill = GridBagConstraints.NONE;
    c.gridx = 0;
    contentPane.add(lblNewLabel_1, c);

    c.gridy = 3;
    c.gridwidth = 2;
    contentPane.add(btnLogin, c);
}

public static void main(String... s) {
    LoginFrame loginFrame = new LoginFrame();
    loginFrame.setVisible(true);
}
}