Java 如何在JFrame中定位组件?

Java 如何在JFrame中定位组件?,java,swing,layout-manager,Java,Swing,Layout Manager,我试图为我的程序创建一个整洁的登录区域,但我似乎无法使两个标签和文本字段与下面的按钮对齐。是否有更好的方法在此JFrame中定位组件?使用JOptionPane显示登录对话框。布局组件的一种方法是使用GridBagLayout 生成以下内容的代码: jframe = new JFrame("Admin"); jpanel = new JPanel(); jpanel.setLayout(new FlowLayout()); jf

我试图为我的程序创建一个整洁的登录区域,但我似乎无法使两个标签和文本字段与下面的按钮对齐。是否有更好的方法在此
JFrame
中定位组件?

使用
JOptionPane
显示登录对话框。布局组件的一种方法是使用
GridBagLayout

生成以下内容的代码:

        jframe = new JFrame("Admin");
        jpanel = new JPanel();

        jpanel.setLayout(new FlowLayout());
        jframe.add(jpanel);

        userLabel = new JLabel("Username:");
        jpanel.add(userLabel);
        userLabel.setBounds(100, 100, 30, 30);

        userTxtfield = new JTextField(15);
        jpanel.add(userTxtfield);

        passwordTxtfield = new JTextField(15);
        jpanel.add(userTxtfield);
        jpanel.add(passwordTxtfield);

        passwordLabel = new JLabel("Password:");
        jpanel.add(passwordLabel);
        userLabel.setBounds(100, 100, 30, 30);

        loginButton= new JButton("Login");
        jpanel.add(loginButton);

        jframe.pack();
        jframe.setLocationRelativeTo(null);
        jframe.setSize(350,350);
        jframe.setVisible(true);
下面是完整的示例(如上所述的MCVE)


搜索Java布局,你会发现很多不错的教程。要创建预期布局,您可能必须在框架的子组件上定义布局。-1) 以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则提供更大的宽度和高度,以显示应如何使用额外的空间。2) 为了更快地获得更好的帮助,请发布一个or。3) 在源代码中只需要一行空白就可以了。
{
之后或
}
之前的空行通常也是多余的。
public void login() {
    JPanel loginPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints(
            0, 0, 1, 1, 0, 0,
            GridBagConstraints.BASELINE_TRAILING,
            GridBagConstraints.NONE, 
            new Insets(5, 5, 5, 5), 4, 6);
    loginPanel.add(new JLabel("ID"), gbc);
    gbc.gridy = 1;
    loginPanel.add(new JLabel("Password"), gbc);
    gbc.anchor = GridBagConstraints.BASELINE_LEADING;
    gbc.gridx = 1;
    gbc.gridy = 0;
    loginPanel.add(new JTextField("enter ID", 10), gbc);
    gbc.gridy = 1;
    loginPanel.add(new JPasswordField(6), gbc);

    int result = JOptionPane.showConfirmDialog(
            ui, loginPanel, "LogIn", JOptionPane.OK_CANCEL_OPTION);
    if (result == JOptionPane.OK_OPTION) {
        // here a real app would check the results of the ID/password
        cardLayout.show(ui, "loggedin");
    }
}
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class BlockTheFrame {

    private JComponent ui = null;
    private CardLayout cardLayout;

    BlockTheFrame() {
        initUI();
    }

    public final void initUI() {
        if (ui != null) {
            return;
        }

        cardLayout = new CardLayout();
        ui = new JPanel(cardLayout);
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        JLabel login = new JLabel("Log in");
        login.setFont(login.getFont().deriveFont(200f));
        ui.add(login, "login");

        ui.add(new JLabel("logged in"), "loggedin");
    }

    public void login() {
        JPanel loginPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints(
                0, 0, 1, 1, 0, 0,
                GridBagConstraints.BASELINE_TRAILING,
                GridBagConstraints.NONE, 
                new Insets(5, 5, 5, 5), 4, 6);
        loginPanel.add(new JLabel("ID"), gbc);
        gbc.gridy = 1;
        loginPanel.add(new JLabel("Password"), gbc);
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.gridx = 1;
        gbc.gridy = 0;
        loginPanel.add(new JTextField("enter ID", 10), gbc);
        gbc.gridy = 1;
        loginPanel.add(new JPasswordField(6), gbc);

        int result = JOptionPane.showConfirmDialog(
                ui, loginPanel, "LogIn", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            // here a real app would check the results of the ID/password
            cardLayout.show(ui, "loggedin");
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                BlockTheFrame o = new BlockTheFrame();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
                o.login();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}