CHAPTER 16 SRS NOTES
====================
TWO CLASSES HAVE BEEN *ADDED* AS OF CHAPTER 16:
MainFrame
PasswordPopup
AND TWO HAVE *CHANGED* SINCE CHAPTER 15:
Student
SRS
TOTAL OF FOURTEEN CLASSES *UNCHANGED* SINCE CHAPTER 15:
Course
CourseCatalog
EnrollmentStatus
Faculty
InvalidStudentException
Person
Professor
ScheduleOfClasses
Section
SRSDataAccess
SRSInitializationException
StudentPersistenceException
Transcript
TranscriptEntry
FOR A GRAND TOTAL OF EIGHTEEN CLASSES.
===
OTHER FILES IN THIS DIRECTORY ARE AS FOLLOWS:
*.dat - miscellaneous data files read by the SRS application
*.dat.orig - original versions of selected data files, to be used in
"resetting" file contents if the programs are to be rerun multiple times
reset.bat - a DOS script for reinstating the contents of various *.dat
files to their original state if the programs are to be rerun multiple times
FileNames.properties - a Java properties file that maps symbolic file names
to physical file names so that the latter needn't be hardcoded into the
SRS application
===
THE REMAINING FOUR FILES, IF PRESENT, ARE TO BE *DELETED*:
code.bat
copyCode.bat
diffCode.bat
separator.txt
===============
// MainFrame.java - Chapter 16, Java 5 version.
// Copyright 2005 by Jacquie Barker - all rights reserved.
// A GUI class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class MainFrame extends JFrame {
// Define all of the components here as attributes of this class.
private JPanel leftPanel;
private JPanel topLeftPanel;
private JPanel labelPanel;
private JPanel fieldPanel;
private JPanel bottomLeftPanel;
private JPanel rightPanel;
private JPanel buttonPanel;
private JTextField ssnField;
//? private JTextField nameField;
private JLabel nameLabel;
private JLabel totalCoursesLabel;
private JButton dropButton;
private JButton addButton;
private JButton logoffButton;
private JButton saveScheduleButton;
private JLabel l1;
private JLabel l2;
private JLabel l3;
private JLabel l4;
private JList studentCourseList;
private JList scheduleOfClassesList;
// Maintain a handle on the Student who is logged in.
// (Whenever this is set to null, nobody is officially logged on.)
private Student currentUser;
// Constructor.
public MainFrame() {
// Initialize attributes.
currentUser = null;
// Note that using "this." as a prefix is unnecessary -
// any method calls that stand alone (without a dot notation
// prefix) are UNDERSTOOD to be invoked on THIS object.
this.setTitle("Student Registration System");
this.setSize(500, 300);
Container contentPane = this.getContentPane( );
// Technique for centering a frame on the screen.
Dimension frameSize = this.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screenSize.width - frameSize.width)/2,
(screenSize.height - frameSize.height)/2);
// Create a few panels.
leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(2, 1));
topLeftPanel = new JPanel();
topLeftPanel.setLayout(new GridLayout(1, 2));
labelPanel = new JPanel();
labelPanel.setLayout(new GridLayout(4, 1));
fieldPanel = new JPanel();
fieldPanel.setLayout(new GridLayout(4, 1));
bottomLeftPanel = new JPanel();
bottomLeftPanel.setLayout(new BorderLayout());
rightPanel = new JPanel();
rightPanel.setLayout(new BorderLayout());
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 4));
// We'll allow the main frame's layout to remain the
// default BorderLayout.
// Note that we are adding labels without maintaining
// handles on them! This is because we don't need to
// manipulate them programmatically after the fact.
// Thus, we use the same reference variable "l" repeatedly.
JLabel l = new JLabel("SSN: ");
l.setForeground(Color.black);
labelPanel.add(l);
l = new JLabel("Name: ");
l.setForeground(Color.black);
labelPanel.add(l);
l = new JLabel("Total Courses: ");
l.setForeground(Color.black);
labelPanel.add(l);
// Add an empty label for padding/white space.
l = new JLabel("");
labelPanel.add(l);
// We DO maintain handles on modifiable components, however, so
// that we can later go back and read/write their contents
// by name. Note our choice of descriptive names for these.
ssnField = new JTextField(10);
//? nameField = new JTextField(10);
nameLabel = new JLabel();
nameLabel.setForeground(Color.black);
totalCoursesLabel = new JLabel();
totalCoursesLabel.setForeground(Color.black);
fieldPanel.add(ssnField);
//? fieldPanel.add(nameField);
fieldPanel.add(nameLabel);
fieldPanel.add(totalCoursesLabel);
// Add an empty label for padding/white space.
l = new JLabel("");
fieldPanel.add(l);
// Create the buttons and add them to their panel. Again,
// note use of descriptive names.
dropButton = new JButton("Drop");
addButton = new JButton("Add");
logoffButton = new JButton("Log Off");
// Technique for creating a multi-line button label.
saveScheduleButton = new JButton();
saveScheduleButton.setLayout(new GridLayout(2, 1));
l1 = new JLabel("Save My", JLabel.CENTER);
l1.setForeground(Color.black);
l2 = new JLabel("Schedule", JLabel.CENTER);
l2.setForeground(Color.black);
saveScheduleButton.add(l1);
saveScheduleButton.add(l2);
buttonPanel.add(dropButton);
buttonPanel.add(saveScheduleButton);
buttonPanel.add(new JLabel("")); // white space padding
buttonPanel.add(addButton);
buttonPanel.add(logoffButton);
studentCourseList = new JList();
studentCourseList.setFixedCellWidth(200);
bottomLeftPanel.add(studentCourseList, BorderLayout.CENTER);
l = new JLabel("Registered For:");
l.setForeground(Color.black);
bottomLeftPanel.add(l, BorderLayout.NORTH);
l = new JLabel("--- Schedule of Classes ---", JLabel.CENTER);
l.setForeground(Color.black);
rightPanel.add(l, BorderLayout.NORTH);
// Retrieve a Collection of Sections from the main ScheduleOfClasses,
// then sort it via a private "housekeeping" method,
// sortSections, before displaying it.
scheduleOfClassesList = new JList(sortSections(SRS.scheduleOfClasses.
getSectionsOffered()));
scheduleOfClassesList.setFixedCellWidth(250);
rightPanel.add(scheduleOfClassesList, BorderLayout.EAST);
// Initialize the buttons to their proper enabled/disabled
// state.
resetButtons();
// Finally, attach all of the panels to one another
// and to the frame.
// Add in ascending row, then column, order.
topLeftPanel.add(labelPanel);
topLeftPanel.add(fieldPanel);
leftPanel.add(topLeftPanel);
leftPanel.add(bottomLeftPanel);
contentPane.add(leftPanel, BorderLayout.WEST);
contentPane.add(rightPanel, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
// ------------------
// Add all behaviors.
// ------------------
// Different types of components require different types
// of listeners:
//
// o Text fields respond to an ActionListener
// whenever the Enter key is pressed.
//
// o Buttons respond to an ActionListener
// whenever the button is clicked.
//
// o JLists respond to a ListSelectionListener
// whenever an item is selected.
ActionListener aListener;
ListSelectionListener lListener;
WindowAdapter wListener;
// ssnField
aListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// First, clear the fields reflecting the
// previous student's information.
clearFields();
SRS.rar_学生注册功能
版权申诉
146 浏览量
2022-09-21
08:46:31
上传
评论
收藏 37KB RAR 举报
御道御小黑
- 粉丝: 61
- 资源: 1万+
评论0