我正在尝试制作一个用于计算用户输入类的GPA的GUI。但是,当我尝试通过点击&#34添加更多文本字段到框架时,我遇到了问题。添加了一个新类"按钮。当我点击它没有任何反应。我使用面板来包含文本字段并设置单击操作以将面板添加到我的框架,但这似乎不起作用。我的代码有什么问题?任何答案都非常感谢!
public class gpaCalculator extends JFrame{
private JFrame frame;
private JPanel panel1;
/**private JLabel creditHourLabel;
private JLabel nameLabel;
private JLabel calculatedGPALabel;
private JLabel gradeLabel;
private JComboBox courseCreditHour;
private JTextField courseName;
private JComboBox courseGrade;
private JComboBox courseType;
private JTextField calculatedGPA;**/
private JButton addNewCourse;
private JButton finishedCalculate;
/**Courses**/
public gpaCalculator()
{
frame = new JFrame();
JPanel panel1 = new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 600);
this.setLayout(new FlowLayout());
addNewCourse = new JButton("Add a new course!");
addNewCourse.addActionListener(new ButtonListener());
finishedCalculate = new JButton("Finished, Please tell me my GPA!");
this.add(addNewCourse);
this.add(finishedCalculate);
this.add(panel1);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private class ButtonListener implements ActionListener
{
@Override
public void actionPerformed (ActionEvent args){
String[] courseCreditHourCombo = {"1", "2", "3", "4"};
String[] courseGradeCombo = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "E", "F", "U"};
String[] courseTypeCombo = {"Have taken", "Currently taking", "Will be taking"};
JLabel creditHourLabel = new JLabel ("Course credit houe");
JLabel nameLabel = new JLabel("Course name: ");
JLabel calculatedGPALabel = new JLabel("Calculated GPA");
JLabel gradeLabel = new JLabel("Course grade ");
JComboBox courseCreditHour = new JComboBox(courseCreditHourCombo);
JTextField courseName = new JTextField(10);
JComboBox courseGrade = new JComboBox(courseGradeCombo);
JComboBox courseType = new JComboBox(courseTypeCombo);
JTextField calculatedGPA = new JTextField(10);
panel1.setSize(100, 100);
panel1.add(nameLabel);
panel1.add(courseName);
panel1.add(creditHourLabel);
panel1.add(courseCreditHour);
panel1.add(gradeLabel);
panel1.add(courseGrade);
panel1.setLocation(0, 0);
panel1.setVisible(true);
}
}
}