import javax.swing.*; import java.awt.*; import java.awt.event.*; /** Graphics interface for Student class. Makes it possible to add grades, see averages, and see grade lists. There is no file-handling mechanism. @author Melvin Fitting @version October, 2003, revised October, 2004 */ public class StudentGui extends JFrame implements ActionListener { private Student s; private JTextField instructions; private JComboBox choiceList; private JTextField average; private JTextField newGrade; private JTextArea gradeList; private JScrollPane gradeListScroll; /** @param inS Student object for which this is the GUI */ public StudentGui(Student inS) { s = inS; setSize(400,480); setTitle(s.getFirst() + " " + s.getLast()); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setLayout(null); setBackground(Color.YELLOW); instructions = new JTextField("Select an item below."); instructions.setFont(new Font("SansSerif", Font.BOLD, 20)); instructions.setSize(280,30); instructions.setLocation(30,50); instructions.setBackground(Color.YELLOW); instructions.setEditable(false); add(instructions); String[] choices = {"Select an item here", "Add a grade", "See the average", "See the list of grades", "Quit"}; choiceList = new JComboBox(choices); choiceList.setSize(280,30); choiceList.setLocation(30,100); choiceList.addActionListener(this); add(choiceList); average = new JTextField("No grades entered yet"); average.setSize(280,30); average.setLocation(30,150); average.setEditable(false); gradeList = new JTextArea("No grades entered yet"); gradeList.setEditable(false); gradeList.setLineWrap(true); gradeListScroll = new JScrollPane(gradeList); gradeListScroll.setSize(280,200); gradeListScroll.setLocation(30,150); newGrade = new JTextField("Enter grade here"); newGrade.setSize(280,30); newGrade.setLocation(30,150); newGrade.setEditable(true); newGrade.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == choiceList) { int choice = choiceList.getSelectedIndex(); switch(choice) { case 1: addGrade(); break; case 2: seeAverage(); break; case 3: seeList(); break; case 4: dispose(); } } else if (e.getSource() == newGrade) { String input = newGrade.getText(); input = input.trim(); try { int grade = Integer.parseInt(input); s.addGrade(grade); newGrade.setText("Enter a grade here"); remove(newGrade); repaint(); } catch(NumberFormatException ex) { newGrade.setText("Please re-enter"); } } } private void refresh() { remove(average); remove(gradeListScroll); remove(newGrade); choiceList.setSelectedIndex(0); } private void addGrade() { refresh(); add(newGrade); repaint(); } private void seeAverage() { refresh(); if (!s.listEmpty()) average.setText("Average is " + s.getAverage()); add(average); repaint(); } private void seeList() { refresh(); if (!s.listEmpty()) { Integer[] grades = s.getGradeList(); StringBuffer textBuffer = new StringBuffer("" + grades[0]); for(int n = 1; n < grades.length; n++) textBuffer.append(", " + grades[n]); gradeList.setText(textBuffer.toString()); } add(gradeListScroll); gradeListScroll.validate(); repaint(); } }