gpt4 book ai didi

java - Java GUI 中什么也没有出现

转载 作者:行者123 更新时间:2023-12-01 22:29:36 26 4
gpt4 key购买 nike

所以我有一个大学时的 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)添加到this

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

关于java - Java GUI 中什么也没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28092239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com