📄 mainframe.java~1~
字号:
// call.
JOptionPane.showMessageDialog(null,
message,
"Request Denied",
JOptionPane.WARNING_MESSAGE);
}
else {
// Attempt to enroll the student, noting
// the status code that is returned.
int success =
selected.enroll(currentUser);
// Report the status to the user.
if (success == Section.SECTION_FULL) {
JOptionPane.showMessageDialog(
null,
"Sorry - that section is full.",
"Request Denied",
JOptionPane.WARNING_MESSAGE);
}
else if (success ==
Section.PREREQ_NOT_SATISFIED) {
JOptionPane.showMessageDialog(
null,
"You haven't satisfied all " +
"of the prerequisites for " +
"this course.",
"Request Denied",
JOptionPane.WARNING_MESSAGE);
}
else if (success ==
Section.PREVIOUSLY_ENROLLED) {
String[] message =
{ "You are already enrolled in " +
"(or have already",
"successfully completed) a " +
"section of this course.",
" " };
JOptionPane.showMessageDialog(
null,
message,
"Request Denied",
JOptionPane.WARNING_MESSAGE);
}
else { // success!
// Display a confirmation message.
JOptionPane.showMessageDialog(
null,
"Seat confirmed in " +
selected.
getRepresentedCourse().
getCourseNo() + ".",
"Request Successful",
JOptionPane.INFORMATION_MESSAGE);
// Update the list of sections
// that this student is
// registered for.
studentCourseList.setListData(
currentUser.
getSectionsEnrolled());
// Update the field representing
// student's course total.
int total =
currentUser.getCourseTotal();
totalCoursesLabel.setText("" +
total);
// Clear the selection in the
// schedule of classes list.
scheduleOfClassesList.
clearSelection();
}
}
// Check states of the various buttons.
resetButtons();
}
};
addButton.addActionListener(aListener);
// dropButton
aListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Determine which section is selected
// (note that we must cast it, as it
// is returned as an Object reference).
Section selected = (Section)
studentCourseList.getSelectedValue();
// Drop the course.
selected.drop(currentUser);
// Display a confirmation message.
JOptionPane.showMessageDialog(null,
"Course " + selected.
getRepresentedCourse().
getCourseNo() + " dropped.",
"Request Successful",
JOptionPane.INFORMATION_MESSAGE);
// Update the list of sections that
// this student is registered for.
studentCourseList.setListData(
currentUser.
getSectionsEnrolled());
// Update the field representing
// student's course total.
int total = currentUser.getCourseTotal();
totalCoursesLabel.setText("" + total);
// Check states of the various buttons.
resetButtons();
}
};
dropButton.addActionListener(aListener);
// saveScheduleButton
aListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean success = currentUser.persist();
if (success) {
// Let the user know that his/her
// schedule was successfully saved.
JOptionPane.showMessageDialog(null,
"Schedule saved.",
"Schedule Saved",
JOptionPane.INFORMATION_MESSAGE);
}
else {
// Let the user know that there
// was a problem.
JOptionPane.showMessageDialog(null,
"Problem saving your " +
"schedule; please contact " +
"the SRS Support Staff for " +
"assistance.",
"Problem Saving Schedule",
JOptionPane.WARNING_MESSAGE);
}
}
};
saveScheduleButton.addActionListener(aListener);
// logoffButton
aListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearFields();
ssnField.setText("");
currentUser = null;
// Clear the selection in the
// schedule of classes list.
scheduleOfClassesList.clearSelection();
// Check states of the various buttons.
resetButtons();
}
};
logoffButton.addActionListener(aListener);
// studentCourseList
lListener = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// When an item is selected in this list,
// we clear the selection in the other list.
if (!(studentCourseList.isSelectionEmpty()))
scheduleOfClassesList.clearSelection();
// Check states of the various buttons.
resetButtons();
}
};
studentCourseList.addListSelectionListener(lListener);
// scheduleOfClassesList
lListener = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// When an item is selected in this list,
// we clear the selection in the other list.
if (!(scheduleOfClassesList.isSelectionEmpty()))
studentCourseList.clearSelection();
// Check states of the various buttons.
resetButtons();
}
};
scheduleOfClassesList.addListSelectionListener(lListener);
wListener = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
this.addWindowListener(wListener);
this.setVisible(true);
}
// Because there are so many different situations in which one or
// more buttons need to be (de)activated, and because the logic is
// so complex, we centralize it here and then just call this method
// whenever we need to check the state of one or more of the buttons.
// It is a tradeoff of code elegance for execution efficiency:
// we are doing a bit more work each time (because we don't need to
// reset all four buttons every time), but since the execution time
// is minimal, this seems like a reasonable tradeoff.
private void resetButtons() {
// There are four conditions which collectively govern the
// state of each button:
//
// o Whether a user is logged on or not.
boolean isLoggedOn;
if (currentUser != null) isLoggedOn = true;
else isLoggedOn = false;
// o Whether the user is registered for at least one course.
boolean atLeastOne;
if (currentUser != null && currentUser.getCourseTotal() > 0)
atLeastOne = true;
else atLeastOne = false;
// o Whether a registered course has been selected.
boolean courseSelected;
if (studentCourseList.isSelectionEmpty())
courseSelected = false;
else courseSelected = true;
// o Whether an item is selected in the Schedule of Classes.
boolean catalogSelected;
if (scheduleOfClassesList.isSelectionEmpty())
catalogSelected = false;
else catalogSelected = true;
// Now, verify the conditions on a button-by-button basis.
// Drop button:
if (isLoggedOn && atLeastOne && courseSelected)
dropButton.setEnabled(true);
else dropButton.setEnabled(false);
// Add button:
if (isLoggedOn && catalogSelected)
addButton.setEnabled(true);
else addButton.setEnabled(false);
// Save My Schedule button:
if (isLoggedOn) {
saveScheduleButton.setEnabled(true);
// Because of the way that we created the latter two
// buttons, we have do a bit of extra work to make them
// APPEAR to be turned on or off.
l1.setEnabled(true);
l2.setEnabled(true);
}
else {
saveScheduleButton.setEnabled(false);
l1.setEnabled(false);
l2.setEnabled(false);
}
// Log Off button:
if (isLoggedOn) logoffButton.setEnabled(true);
else logoffButton.setEnabled(false);
}
// Called whenever a user is logged off.
private void clearFields() {
nameField.setText("");
totalCoursesLabel.setText("");
studentCourseList.setListData(new Vector());
}
// Set the various fields, lists, etc. to reflect the information
// associated with a particular student. (Used when logging in.)
private void setFields(Student theStudent) {
nameField.setText(theStudent.getName());
int total = theStudent.getCourseTotal();
totalCoursesLabel.setText("" + total);
// If the student is registered for any courses, list these, too.
if (total > 0) {
// Because we already have a vector containing the
// sections that the student is registered for,
// and because these objects have defined a toString()
// method, we can merely hand the vector to the JList.
studentCourseList.setListData(theStudent.
getSectionsEnrolled());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -