Java JPanel和JLabel';s

Java JPanel和JLabel';s,java,swing,jlabel,layout-manager,grid-layout,Java,Swing,Jlabel,Layout Manager,Grid Layout,我试图创建两组JLabel,一组包含名称,第二组包含值。这些值从数据库中提取并显示到第二组JLabel 我似乎无法将2组JLabel对齐,以便具有名称的集合位于面板的左侧,而具有值的集合直接位于右侧。我知道我的网格布局有一个因素,我只是不知道它应该是什么 package Frames; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayou

我试图创建两组JLabel,一组包含名称,第二组包含值。这些值从数据库中提取并显示到第二组JLabel

我似乎无法将2组JLabel对齐,以便具有名称的集合位于面板的左侧,而具有值的集合直接位于右侧。我知道我的网格布局有一个因素,我只是不知道它应该是什么

package Frames;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import Application.Main;

public class AccountFrame extends JPanel implements PropertyChangeListener, ActionListener {

    private static JFrame accountFrame;

    private JLabel firstNameLabel;
    private JLabel lastNameLabel;
    private JLabel dobLabel;
    private JLabel emailLabel;
    private JLabel usernameLabel;
    private JLabel passwordLabel;

    private static String firstNameLabelText = "First Name: ";
    private static String lastNameLabelText = "Last Name: ";
    private static String dobLabelText = "Date Of Birth: ";
    private static String emailLabelText = "Email: ";
    private static String usernameLabelText = "Username: ";
    private static String passwordLabelText = "Password: ";

    private static JButton editButton;
    private static JButton closeButton;

    public AccountFrame() {
        super(new BorderLayout());

        firstNameLabel = new JLabel(firstNameLabelText);
        firstNameLabel.setForeground(Color.WHITE);
        firstNameLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        lastNameLabel = new JLabel(lastNameLabelText);
        lastNameLabel.setForeground(Color.WHITE);
        lastNameLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        dobLabel = new JLabel(dobLabelText);
        dobLabel.setForeground(Color.WHITE);
        dobLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        emailLabel = new JLabel(emailLabelText);
        emailLabel.setForeground(Color.WHITE);
        emailLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        usernameLabel = new JLabel(usernameLabelText);
        usernameLabel.setForeground(Color.WHITE);
        usernameLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        passwordLabel = new JLabel(passwordLabelText);
        passwordLabel.setForeground(Color.WHITE);
        passwordLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        editButton = new JButton("Edit");
        editButton.setBackground(new Color(129,13,13));
        editButton.setForeground(Color.WHITE);
        editButton.setFocusPainted(false);
        editButton.setFont(new Font("Andalus", Font.BOLD, 18));
        editButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
//EDIT ACCOUNT INFORMATION.
            }
        });

        closeButton = new JButton("Close");
        closeButton.setBackground(new Color(129,13,13));
        closeButton.setForeground(Color.WHITE);
        closeButton.setFocusPainted(false);
        closeButton.setFont(new Font("Andalus", Font.BOLD, 18));
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                accountFrame.dispose();
            }
        });

        TitledBorder accountPanelBorder = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.WHITE), "Account", TitledBorder.CENTER , TitledBorder.TOP, new Font("Andalus", Font.BOLD, 18));
        accountPanelBorder.setTitleColor(Color.WHITE);

        //this is where the labels need to have values 
        //added on to the string to get values from the current character.
        JPanel accountPanel = new JPanel(new GridLayout(0, 1));
        accountPanel.add(firstNameLabel, BorderLayout.WEST);
        accountPanel.add(lastNameLabel, BorderLayout.WEST);
        accountPanel.add(dobLabel, BorderLayout.WEST);
        accountPanel.add(emailLabel, BorderLayout.WEST);
        accountPanel.add(usernameLabel, BorderLayout.WEST);
        accountPanel.add(passwordLabel, BorderLayout.WEST);
        accountPanel.setBackground(new Color(82,80,80));
        accountPanel.setBorder(accountPanelBorder);
        accountPanel.setPreferredSize(new Dimension(400,200));

//      JPanel accountValuesPanel = new JPanel(new GridLayout(0, 1));
//      accountValuesPanel.add(firstNameValue);
//      accountValuesPanel.setBackground(new Color(82,80,80));

        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttons.add(editButton);
        buttons.add(closeButton);
        buttons.setBackground(new Color(82,80,80));

        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        setBackground(new Color(82,80,80));
        add(accountPanel, BorderLayout.WEST);
        add(buttons, BorderLayout.SOUTH);
//      add(accountValuesPanel, BorderLayout.LINE_END);
    }

    /**
     * Create the GUI and show it. For thread safety,
     * this method should be invoked from the 
     * event dispatch thread.
     */
    public static void createAndShowGUI() {
        accountFrame = new JFrame("OVERRATED");
        accountFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        accountFrame.setBackground(Color.red);
        accountFrame.add(new AccountFrame());
        accountFrame.setVisible(true);
        accountFrame.pack();
        accountFrame.setLocationRelativeTo(null);   
        accountFrame.setTitle("OVERRATED");
        accountFrame.setResizable(false);
        //startupFrame.setIconImage(new ImageIcon().getImage());
        JFrame.setDefaultLookAndFeelDecorated(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        // TODO Auto-generated method stub

    }

    public static void main(String[] args) {
        AccountFrame a = new AccountFrame();
        a.createAndShowGUI();
    }
}

您的问题是您的
GridLayout
只有一列。您需要像这样创建
GridLayout
新的GridLayout(0,2)
。下面是一个小的可运行示例,它将成对的jlabel放在一起

public static void main(String[] args) {
    JFrame f = new JFrame("Good day");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(400, 250);

    JPanel panel = new JPanel(new GridLayout(0, 2));
    JLabel left = new JLabel("Foo");
    JLabel right = new JLabel(("Bar"));
    JLabel hello = new JLabel("Hello");
    JLabel world = new JLabel("World");
    panel.add(left);
    panel.add(right);
    panel.add(hello);
    panel.add(world);
    f.add(panel);

    f.setVisible(true);
}

您的问题是您的
GridLayout
只有一列。您需要像这样创建
GridLayout
新的GridLayout(0,2)
。下面是一个小的可运行示例,它将成对的jlabel放在一起

public static void main(String[] args) {
    JFrame f = new JFrame("Good day");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(400, 250);

    JPanel panel = new JPanel(new GridLayout(0, 2));
    JLabel left = new JLabel("Foo");
    JLabel right = new JLabel(("Bar"));
    JLabel hello = new JLabel("Hello");
    JLabel world = new JLabel("World");
    panel.add(left);
    panel.add(right);
    panel.add(hello);
    panel.add(world);
    f.add(panel);

    f.setVisible(true);
}

首先,即使accountPanel布局是gridLayout,您也使用了BorderLayout:

JPanel accountPanel = new JPanel(new GridLayout(0, 1));
accountPanel.add(firstNameLabel, BorderLayout.WEST); // BorderLayout.WEST wrong
我的建议是,你应该使用gridbaglayout。GridbagLayout看起来很难学,但实际上它很有逻辑性

我对你的代码做了一点修改

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial")
public class AccountFrame extends JPanel implements PropertyChangeListener,
        ActionListener {
    private static JFrame accountFrame;
    private JTextField firstNameTextField, lastNameTextField, dobTextField,
            emailTextField, userNameTextField;
    private JPasswordField passwordPasswordField;

    private JLabel firstNameLabel;
    private JLabel lastNameLabel;
    private JLabel dobLabel;
    private JLabel emailLabel;
    private JLabel usernameLabel;
    private JLabel passwordLabel;

    private static String firstNameLabelText = "First Name: ";
    private static String lastNameLabelText = "Last Name: ";
    private static String dobLabelText = "Date Of Birth: ";
    private static String emailLabelText = "Email: ";
    private static String usernameLabelText = "Username: ";
    private static String passwordLabelText = "Password: ";

    private static JButton editButton;
    private static JButton closeButton;

    public AccountFrame() {
        super(new BorderLayout());

        firstNameLabel = new JLabel(firstNameLabelText);
        firstNameLabel.setForeground(Color.WHITE);
        firstNameLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        lastNameLabel = new JLabel(lastNameLabelText);
        lastNameLabel.setForeground(Color.WHITE);
        lastNameLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        dobLabel = new JLabel(dobLabelText);
        dobLabel.setForeground(Color.WHITE);
        dobLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        emailLabel = new JLabel(emailLabelText);
        emailLabel.setForeground(Color.WHITE);
        emailLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        usernameLabel = new JLabel(usernameLabelText);
        usernameLabel.setForeground(Color.WHITE);
        usernameLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        passwordLabel = new JLabel(passwordLabelText);
        passwordLabel.setForeground(Color.WHITE);
        passwordLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        // lets create JTextFields and a JPasswordField
        firstNameTextField = new JTextField(20);
        lastNameTextField = new JTextField(20);
        dobTextField = new JTextField(20);
        emailTextField = new JTextField(20);
        userNameTextField = new JTextField(20);
        passwordPasswordField = new JPasswordField(20);

        editButton = new JButton("Edit");
        editButton.setBackground(new Color(129, 13, 13));
        editButton.setForeground(Color.WHITE);
        editButton.setFocusPainted(false);
        editButton.setFont(new Font("Andalus", Font.BOLD, 18));
        editButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // EDIT ACCOUNT INFORMATION.
            }
        });

        closeButton = new JButton("Close");
        closeButton.setBackground(new Color(129, 13, 13));
        closeButton.setForeground(Color.WHITE);
        closeButton.setFocusPainted(false);
        closeButton.setFont(new Font("Andalus", Font.BOLD, 18));
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                accountFrame.dispose();
            }
        });

        TitledBorder accountPanelBorder = BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.WHITE), "Account",
                TitledBorder.CENTER, TitledBorder.TOP, new Font("Andalus",
                        Font.BOLD, 18));
        accountPanelBorder.setTitleColor(Color.WHITE);

        // this is where the labels need to have values
        // added on to the string to get values from the current character.
        JPanel accountPanel = new JPanel(new GridBagLayout());
        accountPanel.setBackground(new Color(82, 80, 80));
        accountPanel.setBorder(accountPanelBorder);
        accountPanel.setPreferredSize(new Dimension(400, 200));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(0, 0, 0, 0);

        // lets add labels and textfields

        // 1. row
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        accountPanel.add(firstNameLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        accountPanel.add(firstNameTextField, gbc);

        // 2. row
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        accountPanel.add(lastNameLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        accountPanel.add(lastNameTextField, gbc);

        // 3. row
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        accountPanel.add(dobLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        accountPanel.add(dobTextField, gbc);

        // 4. row
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        accountPanel.add(emailLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        accountPanel.add(emailTextField, gbc);

        // 5. row
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        accountPanel.add(usernameLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        accountPanel.add(userNameTextField, gbc);

        // 6. row
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        accountPanel.add(passwordLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        accountPanel.add(passwordPasswordField, gbc);

        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttons.add(editButton);
        buttons.add(closeButton);
        buttons.setBackground(new Color(82, 80, 80));

        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        setBackground(new Color(82, 80, 80));
        add(accountPanel, BorderLayout.WEST);
        add(buttons, BorderLayout.SOUTH);
        // add(accountValuesPanel, BorderLayout.LINE_END);
    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event dispatch thread.
     */
    public static void createAndShowGUI() {
        accountFrame = new JFrame("OVERRATED");
        accountFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        accountFrame.setBackground(Color.red);
        accountFrame.add(new AccountFrame());

        accountFrame.pack();
        accountFrame.setTitle("OVERRATED");
        accountFrame.setResizable(false);
        accountFrame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        // TODO Auto-generated method stub

    }

    public static void main(String[] args) {
        AccountFrame a = new AccountFrame();
        a.createAndShowGUI();
    }
}

首先,即使您的accountPanel布局是gridLayout,您也使用了BorderLayout:

JPanel accountPanel = new JPanel(new GridLayout(0, 1));
accountPanel.add(firstNameLabel, BorderLayout.WEST); // BorderLayout.WEST wrong
我的建议是,你应该使用gridbaglayout。GridbagLayout看起来很难学,但实际上它很有逻辑性

我对你的代码做了一点修改

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial")
public class AccountFrame extends JPanel implements PropertyChangeListener,
        ActionListener {
    private static JFrame accountFrame;
    private JTextField firstNameTextField, lastNameTextField, dobTextField,
            emailTextField, userNameTextField;
    private JPasswordField passwordPasswordField;

    private JLabel firstNameLabel;
    private JLabel lastNameLabel;
    private JLabel dobLabel;
    private JLabel emailLabel;
    private JLabel usernameLabel;
    private JLabel passwordLabel;

    private static String firstNameLabelText = "First Name: ";
    private static String lastNameLabelText = "Last Name: ";
    private static String dobLabelText = "Date Of Birth: ";
    private static String emailLabelText = "Email: ";
    private static String usernameLabelText = "Username: ";
    private static String passwordLabelText = "Password: ";

    private static JButton editButton;
    private static JButton closeButton;

    public AccountFrame() {
        super(new BorderLayout());

        firstNameLabel = new JLabel(firstNameLabelText);
        firstNameLabel.setForeground(Color.WHITE);
        firstNameLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        lastNameLabel = new JLabel(lastNameLabelText);
        lastNameLabel.setForeground(Color.WHITE);
        lastNameLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        dobLabel = new JLabel(dobLabelText);
        dobLabel.setForeground(Color.WHITE);
        dobLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        emailLabel = new JLabel(emailLabelText);
        emailLabel.setForeground(Color.WHITE);
        emailLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        usernameLabel = new JLabel(usernameLabelText);
        usernameLabel.setForeground(Color.WHITE);
        usernameLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        passwordLabel = new JLabel(passwordLabelText);
        passwordLabel.setForeground(Color.WHITE);
        passwordLabel.setFont(new Font("Andalus", Font.BOLD, 18));

        // lets create JTextFields and a JPasswordField
        firstNameTextField = new JTextField(20);
        lastNameTextField = new JTextField(20);
        dobTextField = new JTextField(20);
        emailTextField = new JTextField(20);
        userNameTextField = new JTextField(20);
        passwordPasswordField = new JPasswordField(20);

        editButton = new JButton("Edit");
        editButton.setBackground(new Color(129, 13, 13));
        editButton.setForeground(Color.WHITE);
        editButton.setFocusPainted(false);
        editButton.setFont(new Font("Andalus", Font.BOLD, 18));
        editButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // EDIT ACCOUNT INFORMATION.
            }
        });

        closeButton = new JButton("Close");
        closeButton.setBackground(new Color(129, 13, 13));
        closeButton.setForeground(Color.WHITE);
        closeButton.setFocusPainted(false);
        closeButton.setFont(new Font("Andalus", Font.BOLD, 18));
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                accountFrame.dispose();
            }
        });

        TitledBorder accountPanelBorder = BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.WHITE), "Account",
                TitledBorder.CENTER, TitledBorder.TOP, new Font("Andalus",
                        Font.BOLD, 18));
        accountPanelBorder.setTitleColor(Color.WHITE);

        // this is where the labels need to have values
        // added on to the string to get values from the current character.
        JPanel accountPanel = new JPanel(new GridBagLayout());
        accountPanel.setBackground(new Color(82, 80, 80));
        accountPanel.setBorder(accountPanelBorder);
        accountPanel.setPreferredSize(new Dimension(400, 200));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(0, 0, 0, 0);

        // lets add labels and textfields

        // 1. row
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        accountPanel.add(firstNameLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        accountPanel.add(firstNameTextField, gbc);

        // 2. row
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        accountPanel.add(lastNameLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        accountPanel.add(lastNameTextField, gbc);

        // 3. row
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        accountPanel.add(dobLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        accountPanel.add(dobTextField, gbc);

        // 4. row
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        accountPanel.add(emailLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        accountPanel.add(emailTextField, gbc);

        // 5. row
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        accountPanel.add(usernameLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        accountPanel.add(userNameTextField, gbc);

        // 6. row
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        accountPanel.add(passwordLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        accountPanel.add(passwordPasswordField, gbc);

        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttons.add(editButton);
        buttons.add(closeButton);
        buttons.setBackground(new Color(82, 80, 80));

        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        setBackground(new Color(82, 80, 80));
        add(accountPanel, BorderLayout.WEST);
        add(buttons, BorderLayout.SOUTH);
        // add(accountValuesPanel, BorderLayout.LINE_END);
    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event dispatch thread.
     */
    public static void createAndShowGUI() {
        accountFrame = new JFrame("OVERRATED");
        accountFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        accountFrame.setBackground(Color.red);
        accountFrame.add(new AccountFrame());

        accountFrame.pack();
        accountFrame.setTitle("OVERRATED");
        accountFrame.setResizable(false);
        accountFrame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        // TODO Auto-generated method stub

    }

    public static void main(String[] args) {
        AccountFrame a = new AccountFrame();
        a.createAndShowGUI();
    }
}

建议:研究Java Netbeans。它有一个拖放式IDE,允许您只需将元素拖放到
JFrame
上,而无需硬编码。这就是我学习如何使用
JFrame
s和
GroupLayout
s的方法。您可以创建一个名为accountPanel的JPanel,其中包含一列GridLayout。这不是Gridlayout的一个很好的用法。然后使用BorderLayout定位将组件添加到它?删除所有的
BorderLayout.WEST
s,将其切换到
GridLayout(0,2)
,并添加交替的名称和值。网格布局从左到右,然后从上到下填充。放置顺序取决于面板的ComponentOrientation属性。建议:查看Java Netbeans。它有一个拖放式IDE,允许您只需将元素拖放到
JFrame
上,而无需硬编码。这就是我学习如何使用
JFrame
s和
GroupLayout
s的方法。您可以创建一个名为accountPanel的JPanel,其中包含一列GridLayout。这不是Gridlayout的一个很好的用法。然后使用BorderLayout定位将组件添加到它?删除所有的
BorderLayout.WEST
s,将其切换到
GridLayout(0,2)
,并添加交替的名称和值。网格布局从左到右,然后从上到下填充。放置顺序取决于面板的ComponentOrientation属性。