/* TestGUI.java: test for ClassOfStudents2 example of using arrays * * Author: David Aspinall * Copyright: University of Edinburgh * Version: TestGUI.java,v 1.7 2002/11/19 12:29:20 pbj Exp * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; class TestGUI extends JFrame { ClassOfStudents2 testclass = new ClassOfStudents2(); // Two helper methods static String inputString(String message) { return JOptionPane.showInputDialog(null, message); } static void showMessage(String message, String title) { JOptionPane.showMessageDialog( null, message, title, JOptionPane.PLAIN_MESSAGE); } void addNewStudent() { // add a student to testclass Student newstud = new Student(); newstud.surname = inputString("Surname of new student:"); newstud.forename = inputString("Forename of new student:"); newstud.matricno = inputString("Matriculation number of new student:"); testclass.insert(newstud); } void findStudent() { // search for a student in testclass String surname = inputString("Search for student with surname:"); Student s = testclass.find(surname); if (s != null) { showMessage(s.toString(), "Found student"); } else { showMessage("No student with surname " + surname, "Could not find student"); } } // Constructor for GUI public TestGUI() { this.setTitle("Using Arrays Example"); this.setSize(new Dimension(250, 100)); JPanel buttonsPanel = (JPanel) this.getContentPane(); buttonsPanel.setLayout( new GridLayout(2,1) ); JButton addStudent = new JButton("Add new student"); addStudent.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { addNewStudent(); } }); buttonsPanel.add(addStudent); JButton findStudent = new JButton("Find student"); findStudent.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { findStudent(); } }); buttonsPanel.add(findStudent); // Exit app when frame is closed this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { System.exit(0); } }); } // end of constructor public static void main(String[] args) { TestGUI gui = new TestGUI(); gui.setVisible(true); } }