简体   繁体   English

如何在 Java GUI 中居中对齐组件?

[英]How do I center align the components in a Java GUI?

I am a beginner programmer and I'm currently working on a project by making a simple Login screen layout using Java.我是一名初学者程序员,我目前正在通过使用 Java 制作简单的登录屏幕布局来开发一个项目。 So far I'm doing pretty good, but is there any way to center-align all the objects I created rather than setting them individually?到目前为止,我做得很好,但是有没有办法让我创建的所有对象居中对齐,而不是单独设置它们?

在此处输入图像描述

So far this is my current code:到目前为止,这是我当前的代码:

package com.main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginScreen extends JFrame {

JLabel lblIntro, lblregister, lblAsGuest; 
JTextField txtUsername;
JPasswordField jpfPassword;
JButton btnLogin, btnRegister, btnAsGuest;
public LoginScreen(){
    super("List n\' Go");
    setLayout(null);

    lblIntro = new JLabel("Welcome to List n\' Go");
    lblregister = new JLabel("Dont have an account yet?");
    lblAsGuest = new JLabel("or continue as Guest");
    txtUsername = new JTextField("Username");
    jpfPassword = new JPasswordField("Password");
    btnLogin = new JButton("Login");
    btnRegister =new JButton("Register");

    lblIntro.setBounds(100,50, 250,40);
    txtUsername.setBounds(50, 150, 300, 40);
    jpfPassword.setBounds(50, 200, 300,  40);
    btnLogin.setBounds(125, 250, 150, 40);
    lblregister.setBounds(125, 350,150, 12);
    btnRegister.setBounds(125, 365, 150, 40);
    lblAsGuest.setBounds(135, 415, 150, 12);

    lblIntro.setFont(new Font("Serif",Font.BOLD, 20));

    add(lblIntro);
    add(txtUsername);
    add(jpfPassword);
    add(btnLogin);
    add(lblregister);
    add(btnRegister);
    add(lblAsGuest);
    addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent exit){
            System.exit(0);
        }
    });

    setSize(400,600);
    setVisible(true);
    setResizable(false);
}
public static void main(String [] args){
    LoginScreen login = new LoginScreen();
}
}

You need to look into Layouts.您需要查看布局。 You should never manually set a components bounds or size.您永远不应该手动设置组件边界或大小。 Sometimes you need to nest layouts to get the effect your looking for:有时您需要嵌套布局以获得您想要的效果:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    
    Insets insets = new Insets(4, 4, 4, 4);
    JPanel inputPanel = new JPanel(new GridBagLayout());
    inputPanel.add(new JLabel("Username: "), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, insets, 0, 0));
    inputPanel.add(new JTextField(), new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insets, 0, 0));
    inputPanel.add(new JLabel("Password: "), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, insets, 0, 0));
    inputPanel.add(new JPasswordField(), new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insets, 0, 0));
    
    JPanel buttonPanel = new JPanel(new GridBagLayout());
    buttonPanel.add(new JButton("Register"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, insets, 0, 0));
    buttonPanel.add(new JButton("Log In"), new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, insets, 0, 0));
    
    frame.setLayout(new GridBagLayout());
    frame.add(new JLabel("Welcome to List"), new GridBagConstraints(0, 0, 2, 1, 0, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insets, 0, 0));
    frame.add(inputPanel, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insets, 0, 0));
    frame.add(buttonPanel, new GridBagConstraints(0, 2, 1, 1, 0, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

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

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