该程序的目标是用 Java 创建您自己的图形用户界面 (GUI)。使用至少 4 个不同的 GUI 组件(按钮、组合框、支票簿、文本字段……)。我正在尝试制作 GUI,但在尝试为我的 GUI 制作子面板时不断收到错误消息?具体来说,它从我的代码中的 panel2 开始。谢谢你。
import javax.swing.*;
public class PTtestGUI {
public static void main(String[] args) {
//Declare labels
JLabel ranklabel = new JLabel("Rank");
JLabel firstLabel = new JLabel("First Name");
JLabel middleInitialLabel = new JLabel("Middle Initial");
JLabel lastLabel = new JLabel("Last Name");
JLabel pushUp = new JLabel("Number of Push-ups");
JLabel sitUp = new JLabel("Number of Sit-ups");
JLabel run = new JLabel("Run time");
//Declare TextFields
JTextField lastName = new JTextField("Enter Last Name");
JTextField firstName = new JTextField("Enter First Name");
JTextField middleInitial = new JTextField("Enter Middle Initial");
//Declare radio button
JRadioButton army = new JRadioButton("ARMY");
JRadioButton navy = new JRadioButton("NAVY");
JRadioButton marine = new JRadioButton("MARINE");
JRadioButton airForce = new JRadioButton("AIR FORCE");
//Declare Combo box
JComboBox rank = new JComboBox(new String[] {"PVT", "PV2", "PFC",
"SPC", "SGT", "SSG", "SFC", "MSG", "CSM"});
//Declare buttons
JButton jbtOK = new JButton("Calculate APFT");
JButton jbtCancel = new JButton("Cancel");
// Create a primary panel that's arranged vertically
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
Jpanel panel2 = new Jpanel();
panel2.add(ranklabel);//Add the label rank to the panel
panel2.add(rank);//Add the label rank to the panel
panel2.add(lastLabel);//Add the label rank to the panel
panel2.add(lastName);//Add the label rank to the panel
panel2.add(firstLabel);//Add the label rank to the panel
panel2.add(firstName);//Add the label rank to the panel
panel2.add(middleInitialLabel);//Add the label rank to the panel
panel2.add(middleInitial);//Add the label rank to the panel
Jpanel panel3 = new Jpanel();//What branch of the military the user is in
panel3.add(army);//Add the label rank to the panel
panel3.add(navy);//Add the label rank to the panel
panel3.add(marine);//Add the label rank to the panel
panel3.add(airForce);//Add the label rank to the panel
Jpanel panel4 = new Jpanel();//How many repititions did the user get on the PT test
panel4.add(pushUp);//Add the label rank to the panel
panel4.add(sitUp);//Add the label rank to the panel
panel4.add(run);//Add the label rank to the panel
Jpanel panel5 = new Jpanel();//offers the user the ability to calculate or cancel the program
panel5.add(jbtOK);//Add the label rank to the panel
panel5.add(jbtCancel);//Add the label rank to the panel
// Add the sub-panels to the primary panel
panel1.add(panel2);
panel1.add(panel3);
panel1.add(panel4);
panel1.add(panel5);
//Displays main frame according to attributes set
JFrame displayFrame = new JFrame();
displayFrame.setTitle("PT Test Calculator");
displayFrame.setSize(1000, 300);
displayFrame.setLocation(300, 150);
displayFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
displayFrame.setVisible(true);
displayFrame.add(panel1);
}//end main
}//end class