【发布时间】:2021-07-30 22:10:46
【问题描述】:
我正在尝试使用 JFrame 和动作侦听器来创建(应该是什么)一个简单的程序,供学生添加和删除课程。我的降级课程按钮不起作用(而且我不相信我的添加课程按钮也可以正常工作)。我认为我的问题是我正在为每个动作侦听器创建一个新的 Course 对象,因此 dropCourse 方法没有在列表中找到该对象。但是,我无法弄清楚如何让侦听器使用我的 Jframe 中的信息来确定该项目是否已经存在于列表中......我对 Java 很陌生,我确信这不是唯一的我的代码有问题...这只是我过去 2 天试图修复的问题。
一门课程只有一个主题 (courseType) 和一个编号 (courseNumType)。
我已经粘贴了下面两个类的完整代码,但我认为问题在于我的动作侦听器创建了一个新对象,然后调用了一个 dropClass() 方法(实际上是一个 list.remove())。但我不确定如何在不创建新对象的情况下传递正确的值。
JButton dropButton = new JButton("Drop Course");
dropButton.setBounds(120, 210, 160, 20);
dropButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Student studentobj = new Student(fNametxt.getText(), lNametxt.getText(),stuEmailtxt.getText(), dobtxt.getText(), stuList);
Course courseobj = new Course (courseTypeBox.getSelectedItem().toString(), (int)courseNumTypeBox.getSelectedItem());
studentobj.dropCourse(courseobj);
}
});
这是我创建 GUI 和实现动作监听器的主要方法和类:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class SIS_GUI {
public SIS_GUI() {
createStudentGUI();
}
private void createStudentGUI() {
// TODO Auto-generated method stub
List<Course> stuList = new ArrayList<>();
JFrame jframe = new JFrame("Tom's Wonderful Student Information System");
jframe.setLayout(new GridLayout(8, 4));
JLabel fName = new JLabel("Enter Student First Name");
fName.setBounds(10, 50, 150, 20);
JTextField fNametxt = new JTextField(" ");
fNametxt.setBounds(150, 50, 150, 20);
JLabel lName = new JLabel("Enter Student Last Name");
lName.setBounds(10, 150, 150, 20);
JTextField lNametxt = new JTextField(" ");
lNametxt.setBounds(350, 150, 150, 20);
JLabel stuEmail = new JLabel("Enter Student Email");
stuEmail.setBounds(10, 90, 150, 20);
JTextField stuEmailtxt = new JTextField(" ");
stuEmailtxt.setBounds(200, 90, 150, 20);
JLabel dob = new JLabel("Enter Date of Birth");
dob.setBounds(10, 130, 150, 20);
JTextField dobtxt = new JTextField(" ");
dobtxt.setBounds(200, 130, 150, 20);
JLabel course = new JLabel("Select Course Name");
course.setBounds(10, 170, 150, 20);
String[] courseType = { "Biology", "Computer Science", "Philosophy" };
JComboBox courseTypeBox = new JComboBox(courseType);
//courseTypeBox.setBounds(200, 170, 150, 20);
JLabel courseNum = new JLabel("Select Course Number");
course.setBounds(10, 170, 150, 20);
Integer[] courseNumType = { 101, 202, 303 };
JComboBox courseNumTypeBox = new JComboBox(courseNumType);
//courseNumTypeBox.setBounds(200, 170, 150, 20);
JButton addButton = new JButton("Add Course");
addButton.setBounds(120, 210, 160, 20);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//String selectedCourseName = courseTypeBox.getSelectedItem().toString();
//int selectedCourseNum = (int) courseNumTypeBox.getSelectedItem();
Student studentobj = new Student(fNametxt.getText(), lNametxt.getText(),stuEmailtxt.getText(), dobtxt.getText(), stuList);
Course courseobj = new Course(courseTypeBox.getSelectedItem().toString(), (int)courseNumTypeBox.getSelectedItem());
studentobj.addCourse(courseobj);
}
});
JButton dropButton = new JButton("Drop Course");
dropButton.setBounds(120, 210, 160, 20);
dropButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//String selectedCourseName = courseTypeBox.getSelectedItem().toString();
//int selectedCourseNum = (int)courseNumTypeBox.getSelectedItem();
Student studentobj = new Student(fNametxt.getText(), lNametxt.getText(),stuEmailtxt.getText(), dobtxt.getText(), stuList);
Course courseobj = new Course (courseTypeBox.getSelectedItem().toString(), (int)courseNumTypeBox.getSelectedItem());
studentobj.dropCourse(courseobj);
}
});
JButton viewButton = new JButton("View Courses");
viewButton.setBounds(120, 210, 160, 20);
viewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Student studentobj = new Student(fNametxt.getText(), lNametxt.getText(),stuEmailtxt.getText(), dobtxt.getText(), stuList);
System.out.println(studentobj.toString());
}
});
jframe.add(fName);
jframe.add(fNametxt);
jframe.add(lName);
jframe.add(lNametxt);
jframe.add(stuEmail);
jframe.add(stuEmailtxt);
jframe.add(dob);
jframe.add(dobtxt);
jframe.add(course);
jframe.add(courseTypeBox);
jframe.add(courseNum);
jframe.add(courseNumTypeBox);
jframe.add(addButton);
jframe.add(dropButton);
jframe.add(viewButton);
jframe.setSize(1000, 800);
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SIS_GUI();
}
}
这是我的带有 addCourse() 和 dropCourse() 方法的学生类代码:
import java.util.List;
public class Student {
private String fName;
private String lName;
private String email;
private String DOB;
private List<Course> courses;
public Student() {
}
public Student(String fName, String lName, String email, String dOB, List<Course> courses)
{
super();
this.fName = fName;
this.lName = lName;
this.email = email;
DOB = dOB;
this.courses = courses;
}
public void addCourse(Course course) {
courses.add(course);
}
public void dropCourse(Course course) {
int index = courses.indexOf(course);
courses.remove(index);
}
public List<Course> viewCourses() {
return courses;
}
public String getfName() {
return fName;
}
public String getlName() {
return lName;
}
public String getEmail() {
return email;
}
public String getDOB() {
return DOB;
}
public String toString() {
String str;
if(courses.size()>0) {
str = fName + " " + lName + "\nEmail address: " + email + "\nDate of birth: " + DOB
+ "\n";
for(int i=0; i < courses.size(); i++) {
str += courses.get(i).getCourseName() + " " + courses.get(i).getCourseID() +"\n";
}}
else
str = fName + " " + lName + "\nEmail address: " + email + "\nDate of birth: " + DOB
+ "\nis not registered for any classes";
return str;
}
}
课程类别:
public class Course {
private String courseName;
private int courseID;
//private int capacity;
public Course() {
}
public Course(String courseName, int courseID /*,int capacity*/) {
this.courseName = courseName;
this.courseID = courseID;
//this.capacity = capacity;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public int getCourseID() {
return courseID;
}
public void setCourseID(int courseID) {
this.courseID = courseID;
}
public boolean contains(Course course) {
// TODO Auto-generated method stub
return false;
}
public int indexOF(Course course) {
// TODO Auto-generated method stub
return 0;
}
// public int getCapacity() {
// return capacity;
// }
//
// public void setCapacity(int capacity) {
// this.capacity = capacity;
// }
}
【问题讨论】:
-
所以,如果没有看到
Course的来源,我会认为Student studentobj = new Student(fNametxt.getText(), lNametxt.getText(), stuEmailtxt.getText(), dobtxt.getText(), stuList);和Course courseobj = new Course(courseTypeBox.getSelectedItem().toString(), (int) courseNumTypeBox.getSelectedItem());通常是错误的方法。您需要一些方法来查找Student和Course的原始实例,以便其中任何一个工作 -
不要分解对象以适应 UI。
JComboBox将很容易让您将原始对象添加到它的模型中。然后,您可以使用ListCellRenderer自定义 UI 如何显示这些对象。更多详情请见How to Use Combo Boxes -
感谢您的帮助!我添加了我的
Course课程。至少我知道我已经发现了错误。我不确定如何通过 actionlistener 查找原始实例,但至少我已经缩小了我需要弄清楚的范围。 -
我更新了我的代码,它似乎可以工作:
List<Course> courseList = studentobj. getCourses(); Course courseobj = new Course (course stuff); if (courseList.contains(courseobj)) { studentobj.dropCourse(courseobj); } else System.out.println("You are not enrolled in this course"); } }); -
不知道为什么你有一个 ArrayList。通常,您会显示学生的课程将显示在某种组件中,例如 JList、JTable 或 JComboBox。因此,数据将存储在组件的“模型”中。然后要删除课程,您只需在组件中选择课程,然后从模型中删除课程。看看How to Use Lists 中的
ListDemo示例。 Hire/Fire 按钮展示了如何使用按钮来更新列表的 ListModel。
标签: java swing arraylist actionlistener