Java 布局对齐混乱

The alignment of Java Layout is messed up

尝试在 Java Swing 和 AWT 中构建一个简单的注册表单,但无法完成我真正想要的。

这是我想要的结果

这是代码

import java.awt.*;
import javax.swing.*;


public class MainFrame {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    // Main Frame
    JFrame mainFrame = new JFrame("New Account Registration");

    JPanel borderPanel = new JPanel(new BorderLayout());
    JPanel gridPanel = new JPanel(new GridLayout(9,2));

    JPanel gridGenderPanel = new JPanel(new GridLayout(1,2));
    JPanel flowButton = new JPanel(new FlowLayout());

    //JLabels
    JLabel title = new JLabel("New Account Registration");
    JLabel name = new JLabel("Name");
    JLabel email = new JLabel("Email Address:");
    JLabel createPassword = new JLabel("Create Password:");
    JLabel confirmPassword = new JLabel("Confirm Password:");
    JLabel gender = new JLabel("Gender:");
    JLabel address = new JLabel("Address:");
    JLabel state = new JLabel("State:");
    JLabel country = new JLabel("Country:");
    JLabel phoneNo = new JLabel("Phone No:");


    String[] coutriesStrings = { "America", "Japan", "India", "Korea", "Sweden" };

    // JTextFields, JRadioButton, JComboBox
    JTextField nameField = new JTextField();
    JTextField emailField = new JTextField();
    JPasswordField passField = new JPasswordField();
    JPasswordField confirmPassField = new JPasswordField();
    JRadioButton male = new JRadioButton("Male");
    JRadioButton female = new JRadioButton("Female");
    ButtonGroup group = new ButtonGroup();
    group.add(male);
    group.add(female);
    JTextField addressField = new JTextField();
    JComboBox stateBox = new JComboBox(coutriesStrings);
    stateBox.setSelectedIndex(1);
    JTextField countryField = new JTextField();
    JTextField phoneField = new JTextField();

    JButton submitButton = new JButton("Submit");
    JButton clearButton = new JButton("Clear");


//      borderPanel.add(title, BorderLayout.NORTH);
//      gridPanel.add(title);

    // Name
    gridPanel.add(name);
    gridPanel.add(nameField);

    //Email
    gridPanel.add(email);
    gridPanel.add(emailField);

    // CreatePassword
    gridPanel.add(createPassword);
    gridPanel.add(passField);

    // Confirm Password
    gridPanel.add(confirmPassword);
    gridPanel.add(confirmPassField);

    // Gender
    gridGenderPanel.add(gender);
    gridGenderPanel.add(male);
    gridGenderPanel.add(female);
    gridPanel.add(gridGenderPanel);

    // Address
    gridPanel.add(address);
    gridPanel.add(addressField);

    // State
    gridPanel.add(state);
    gridPanel.add(stateBox);

    // Country
    gridPanel.add(country);
    gridPanel.add(countryField);

    //Button
    flowButton.add(submitButton);
    flowButton.add(clearButton);
    gridPanel.add(flowButton);

    mainFrame.add(gridPanel);   

    mainFrame.setSize(600, 700);
    mainFrame.setVisible(true);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

}

这是代码的结果

我不知道我哪里做错了,请指导。

您必须将 gender 标签添加到 gridPanel 而不是 gridGenderPanel

    // Gender
    //gridGenderPanel.add(gender);//The mistake
    gridPanel.add(gender);//add to main panel 
    gridGenderPanel.add(male);
    gridGenderPanel.add(female);
    gridPanel.add(gridGenderPanel);

网格布局有一个坏习惯,就是让网格的每个部分都与网格的最大部分大小相同。使 UI 要求所有内容都具有相同的大小 - 这很有效。尝试使用 GridBagLayout - 它不是很漂亮,但绝对有效。

public class MainFrame {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    // Main Frame
    JFrame mainFrame = new JFrame("New Account Registration");

    JPanel borderPanel = new JPanel(new BorderLayout());
    JPanel gridPanel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    JPanel gridGenderPanel = new JPanel(new GridLayout(1, 2));
    JPanel flowButton = new JPanel(new FlowLayout());

    // JLabels
    JLabel title = new JLabel("New Account Registration");
    setSize(title);
    JLabel name = new JLabel("Name");
    setSize(name);
    JLabel email = new JLabel("Email Address:");
    setSize(email);
    JLabel createPassword = new JLabel("Create Password:");
    setSize(createPassword);
    JLabel confirmPassword = new JLabel("Confirm Password:");
    setSize(confirmPassword);
    JLabel gender = new JLabel("Gender:");
    setSize(gender);
    JLabel address = new JLabel("Address:");
    setSize(address);
    JLabel state = new JLabel("State:");
    setSize(state);
    JLabel country = new JLabel("Country:");
    setSize(country);
    JLabel phoneNo = new JLabel("Phone No:");

    String[] coutriesStrings = { "America", "Japan", "India", "Korea",
        "Sweden" };

    // JTextFields, JRadioButton, JComboBox
    JTextField nameField = new JTextField(15);
    JTextField emailField = new JTextField(15);
    JPasswordField passField = new JPasswordField(15);
    JPasswordField confirmPassField = new JPasswordField(15);
    JRadioButton male = new JRadioButton("Male");
    JRadioButton female = new JRadioButton("Female");
    ButtonGroup group = new ButtonGroup();
    group.add(male);
    group.add(female);
    JTextField addressField = new JTextField(15);
    JComboBox stateBox = new JComboBox(coutriesStrings);
    stateBox.setPreferredSize(new Dimension(200, 25));
    stateBox.setMinimumSize(new Dimension(200, 25));
    stateBox.setSelectedIndex(1);
    JTextField countryField = new JTextField(15);
    JTextField phoneField = new JTextField(15);

    JButton submitButton = new JButton("Submit");
    JButton clearButton = new JButton("Clear");

    // Name
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(name, c);
    c.gridx = 2;
    c.gridy = 0;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(nameField, c);

    // Email
    c.gridx = 0;
    c.gridy = 1;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(email, c);
    c.gridx = 2;
    c.gridy = 1;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(emailField, c);

    // CreatePassword
    c.gridx = 0;
    c.gridy = 2;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(createPassword, c);
    c.gridx = 2;
    c.gridy = 2;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(passField, c);

    // Confirm Password
    c.gridx = 0;
    c.gridy = 3;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(confirmPassword, c);
    c.gridx = 2;
    c.gridy = 3;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(confirmPassField, c);

    // Gender
    c.gridx = 0;
    c.gridy = 4;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(gender,c );
    c.gridx = 2;
    c.gridy = 4;
    c.gridheight = 1;
    c.gridwidth = 1;
    gridPanel.add(male,c);
    c.gridx = 3;
    c.gridy = 4;
    c.gridheight = 1;
    c.gridwidth = 1;
    gridPanel.add(female,c);

    // Address
    c.gridx = 0;
    c.gridy = 5;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(address, c);
    c.gridx = 2;
    c.gridy = 5;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(addressField, c);

    // State
    c.gridx = 0;
    c.gridy = 6;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(state, c);
    c.gridx = 2;
    c.gridy = 6;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(stateBox, c);

    // Country
    c.gridx = 0;
    c.gridy = 7;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(country, c);
    c.gridx = 2;
    c.gridy = 7;
    c.gridheight = 1;
    c.gridwidth = 2;
    gridPanel.add(countryField, c);

    // Button
    flowButton.add(submitButton);
    flowButton.add(clearButton);
    c.gridx = 1;
    c.gridy = 8;
    c.gridheight = 1;
    c.gridwidth = 4;
    gridPanel.add(flowButton, c);

    mainFrame.add(gridPanel);

    mainFrame.setSize(350, 300);
    mainFrame.setVisible(true);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private static void setSize(Component label) {
    label.setMinimumSize(new Dimension(120, 15));
    label.setPreferredSize(new Dimension(120, 15));
    }
}