我遇到了一些严重的麻烦。
我必须编写的程序是一个基本上要求的GUI:
我对ActionListener
不是很了解,因为我不知道我在做什么。我必须为ArrayList
提供信息
ArrayList<Course>
(实例变量),例如courseList,用于存储您添加的课程; ActionListener
类,每个类实现其方法
为actionPerformed。 Specically,这些都是我需要的ActionListener
。到目前为止,我将向您展示我的代码。编译时会显示所有Button
和TextArea
s,ActionListener
是我唯一需要帮助的内容。
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class DegreeWorksFrame extends JFrame {
private JLabel courseCodeLabel;
private JTextField courseCodeField;
private JLabel courseNameLabel;
private JTextField courseNameField;
private JLabel courseCreditLabel;
private JTextField courseCreditField;
private JLabel courseGradeLabel;
private JTextField courseGradeField;
private JTextArea resultArea;
private double sum =0;
private double totalCredits=0;
private String code = "";
private String name = "";
private String credit = "";
private String grade = "";
private String heading = ("Code\tName\tCredit\tGrade" + "\n");
private ArrayList<Course> courseList;
private JButton AddCourse;
private JButton CalculateGPA;
private JButton ResetInput;
private JButton ResetOutput;
private static final int AREA_ROWS = 15;
private static final int AREA_COLUMNS = 35;
private final int FRAME_HEIGHT =400;
private final int FRAME_WIDTH = 500;
final int FIELD_WIDTH = 30;
public DegreeWorksFrame() {
resultArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
resultArea.setText(heading);
resultArea.setEditable(false);
createTextField1();
createTextField2();
createTextField3();
createTextField4();
createButtonAddCourse();
createButtonCalculateGPA();
createButtonResetInput();
createButtonResetOutput();
createPanel();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
private void createTextField1() {
courseCodeLabel = new JLabel("Enter Course Code: ");
courseCodeField = new JTextField(FIELD_WIDTH);
courseCodeField.setText(code);
}
private void createTextField2() {
courseNameLabel = new JLabel("Enter Course Name: ");
courseNameField = new JTextField(FIELD_WIDTH);
courseNameField.setText(name);
}
private void createTextField3() {
courseCreditLabel = new JLabel("Enter Course Credit: ");
courseCreditField = new JTextField(FIELD_WIDTH);
courseCreditField.setText(credit);
}
private void createTextField4() {
courseGradeLabel = new JLabel("Enter Course Grade: ");
courseGradeField = new JTextField(FIELD_WIDTH);
courseGradeField.setText(grade);
}
private void createButtonAddCourse() {
AddCourse = new JButton ("Add Courses: ");
ActionListener listener = new addCourseListener();
AddCourse.addActionListener(listener);
}
class addCourseListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
courseList = new ArrayList();
String receiveList = userList.get
}
}
private void createButtonCalculateGPA() {
CalculateGPA = new JButton ("Calculate GPA");
ActionListener listener = new addCourseListener();
AddCourse.addActionListener(listener);
}
class calculateGPAListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
}
}
private void createButtonResetInput() {
ResetInput = new JButton ("Reset Input");
ActionListener listener = new addCourseListener();
AddCourse.addActionListener(listener);
}
class resetInputListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
}
}
private void createButtonResetOutput() {
ResetOutput = new JButton ("Reset Output");
ActionListener listener = new addCourseListener();
AddCourse.addActionListener(listener);
}
class resetOutputListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
}
}
private void createPanel() {
JPanel panel = new JPanel();
panel.add(courseCodeLabel);
panel.add(courseCodeField);
panel.add(courseNameLabel);
panel.add(courseNameField);
panel.add(courseCreditLabel);
panel.add(courseCreditField);
panel.add(courseGradeLabel);
panel.add(courseGradeField);
//buttons
panel.add(AddCourse);
panel.add(CalculateGPA);
panel.add(ResetInput);
panel.add(ResetOutput);
//Scroll bar
JScrollPane scrollPane = new JScrollPane(resultArea);
panel.add(scrollPane);
add(panel);
}
}
答案 0 :(得分:2)
旁注:请注意,您要在AddCourse
按钮上添加多个不正确的监听器。您需要将侦听器添加到正确的按钮。
正如this answer中所述,实现侦听器的一个好方法是使用Anonymous Inner类(有关原因的更好解释,请参阅链接的答案)。
例如:
private void createButtonAddCourse() {
AddCourse = new JButton ("Add Courses: ");
AddCourse.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Create a new Course object and add it to the ArrayList here
}
});
}
...
private void createButtonCalculateGPA() {
CalculateGPA = new JButton ("Calculate GPA");
CalculateGPA.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Iterate over the ArrayList and calculate the overall GPA for each course
}
});
}