Java GUI中没有任何内容

时间:2015-01-22 15:14:08

标签: java swing user-interface

所以我从大学开始进行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);
    }
}

感谢您的帮助。我希望我能很快完成这项工作。

3 个答案:

答案 0 :(得分:1)

变化:

    panel.add(centerPanel, BorderLayout.CENTER);

要看到这个:

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

答案 1 :(得分:1)

您似乎需要将自己创建的根组件(panel)添加到this

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

答案 2 :(得分:1)

在RegistrationForm构造函数中添加this.add(面板)。它对我有用。