Java GUI 中没有任何内容

Nothing appears in the Java GUI

所以我有一个 Java 从大学开始的 GUI 练习,关于制作注册表。

我 [错误地] 在没有测试的情况下从头到尾对所有内容进行了编码,结果发现我的 GUI 上实际上没有显示任何内容。该程序只是一个普通的 JFrame,内部没有任何内容,如上图所示。

我试图找出发生这种情况的原因,但我还没有找到任何线索。感谢您的帮助。

到目前为止,我还没有发现这个问题的任何重复。我发现我的问题与其他用户有些不同。所以,我想道歉,好像有一个重复。

下面是代码:(你可能想仔细看看这个)

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 javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class RegistrationForm extends JFrame {

    private JPanel panel;    
    private JPanel northPanel;
    private JPanel eastPanel;
    private JPanel westPanel;
    private JPanel southPanel;
    private JPanel centerPanel;

    private JPanel formPanel;
    private JPanel basicFormPanel;
    private JPanel addressPanel;
    private JPanel typePanel;
    private JPanel cityPanel;
    private JPanel agreementPanel;
    private JPanel buttonPanel;

    private JLabel formTitle;
    private JLabel lblName;
    private JLabel lblEmail;
    private JLabel lblPhone;
    private JLabel lblAddress;
    private JLabel lblType;
    private JLabel lblCity;

    private JTextField name;
    private JTextField email;
    private JTextField phone;
    private JScrollPane addressScroll;
    private JTextArea address;
    private ButtonGroup type;
    private JRadioButton silver;
    private JRadioButton gold;
    private JRadioButton platinum;
    private JComboBox<String> city;
    private JCheckBox agreement;

    private JButton submit;
    private JButton reset;

    public RegistrationForm() {
        initElements();
        initView();
        initFormPanels();
    }

    public void initElements() {
        panel = new JPanel();
        northPanel = new JPanel();
        eastPanel = new JPanel();
        westPanel = new JPanel();
        southPanel = new JPanel();
        centerPanel = new JPanel();

        formPanel = new JPanel();
        basicFormPanel = new JPanel();
        addressPanel = new JPanel();
        typePanel = new JPanel();
        cityPanel = new JPanel();
        agreementPanel = new JPanel();
        buttonPanel = new JPanel();

        formTitle = new JLabel("Registration Form");
        formTitle.setFont(new Font("Arial", Font.PLAIN, 32));

        lblName = new JLabel("Name");
        lblEmail = new JLabel("Email");
        lblPhone = new JLabel("Phone");
        lblAddress = new JLabel("Address");
        lblType = new JLabel("Type");
        lblCity = new JLabel("City");

        name = new JTextField();
        email = new JTextField();
        phone = new JTextField();
        address = new JTextArea();
        addressScroll = new JScrollPane(address);
        type = new ButtonGroup();
        silver = new JRadioButton("Silver");
        gold = new JRadioButton("Gold");
        platinum = new JRadioButton("Platinum");
        String[] cities = {"Jakarta", "Bandung", "Bogor"};
        city = new JComboBox<String>(cities);
        agreement = new JCheckBox("I agree with the terms and conditions");

        submit = new JButton("Submit");
        reset = new JButton("Reset");
    }

    public void initView() {
        panel.setLayout(new BorderLayout());
        panel.add(northPanel, BorderLayout.NORTH);
        panel.add(eastPanel, BorderLayout.EAST);
        panel.add(westPanel, BorderLayout.WEST);
        panel.add(southPanel, BorderLayout.SOUTH);
        panel.add(centerPanel, BorderLayout.CENTER);

        northPanel.setBackground(Color.black);
        northPanel.setPreferredSize(new Dimension(50, 50));
        formTitle.setForeground(Color.white);

        eastPanel.setBackground(Color.black);
        westPanel.setBackground(Color.black);
        southPanel.setBackground(Color.black);      
        centerPanel.setBackground(Color.gray);

        formPanel.setBackground(Color.gray);
        basicFormPanel.setBackground(Color.gray);
        addressPanel.setBackground(Color.gray);
        typePanel.setBackground(Color.gray);
        cityPanel.setBackground(Color.gray);
        agreementPanel.setBackground(Color.gray);

        formPanel.add(basicFormPanel);
        formPanel.add(addressPanel);
        formPanel.add(typePanel);
        formPanel.add(cityPanel);
        formPanel.add(agreementPanel);
        formPanel.add(buttonPanel);
        centerPanel.add(formPanel);

        buttonPanel.setBackground(Color.black);
        buttonPanel.add(submit);
        buttonPanel.add(reset);
        southPanel.add(buttonPanel);
    }

    public void initFormPanels() {
        // basic form panel
        basicFormPanel.setLayout(new GridLayout(3, 3, 5, 5));

        basicFormPanel.add(lblName);
        basicFormPanel.add(name);

        basicFormPanel.add(lblEmail);
        basicFormPanel.add(email);

        basicFormPanel.add(lblPhone);
        basicFormPanel.add(phone);

        // address panel
        addressPanel.setLayout(new GridLayout(1, 2, 5, 5));

        addressPanel.add(lblAddress);
        addressPanel.add(addressScroll);

        // type panel
        typePanel.setLayout(new GridLayout(1, 2, 5, 5));

        typePanel.add(lblType);

        type.add(silver);
        type.add(gold);
        type.add(platinum);

        buttonPanel.add(silver);
        buttonPanel.add(gold);
        buttonPanel.add(platinum);

        typePanel.add(buttonPanel);

        // city panel
        cityPanel.setLayout(new GridLayout(1, 2, 5, 5));

        cityPanel.add(lblCity);
        cityPanel.add(city);

        // agreement checkbox panel
        agreementPanel.setLayout(new FlowLayout());
        agreementPanel.add(agreement);

        // button panel
        buttonPanel.add(submit);
        buttonPanel.add(reset);

        // set preferred sizes
        basicFormPanel.setPreferredSize(new Dimension(400, 100));
        addressPanel.setPreferredSize(new Dimension(400, 90));
        cityPanel.setPreferredSize(new Dimension(400, 30));
        typePanel.setPreferredSize(new Dimension(400, 50));
        agreementPanel.setPreferredSize(new Dimension(400, 30));
        buttonPanel.setPreferredSize(new Dimension(400, 50));
    }

    public static void main(String[] args) {
        RegistrationForm gui = new RegistrationForm();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(450, 450);
        gui.setTitle("Registration");
        gui.setLocationRelativeTo(null);
        gui.setVisible(true);
    }
}

感谢您的帮助。我希望我能尽快完成这件事。

变化:

    panel.add(centerPanel, BorderLayout.CENTER);

给这个看东西:

    panel.add(centerPanel, BorderLayout.CENTER);
    setContentPane(panel);

看起来您需要将您制作的根组件 (panel) 添加到 this

public RegistrationForm() {
    initElements();
    initView();
    initFormPanels();
    this.add(panel);
}

在注册表中添加 this.add(panel) constructor.It 对我有用。