public class StudentMarks3 extends JFrame
class ButtonHandler implements ActionListener
1: //StudentMarks3.java
2: //Loads student names and marks from a textfile called "input.txt" and
3: //displays them in a JTextArea component on the screen.
4: //Extends StudentMarks2.java by using a JScrollPane for the display area
5: //and by introducing a "button handler", but just for the Quit button.
7: import java.awt.BorderLayout;
8: import java.awt.Font;
9: import java.io.File;
10: import java.io.FileNotFoundException;
11: import java.util.Scanner;
12: import javax.swing.JButton;
13: import javax.swing.JFrame;
14: import javax.swing.JPanel;
15: import javax.swing.JTextArea;
17: //New classes (and one new interface) used:
18: import java.awt.event.ActionEvent;
19: import java.awt.event.ActionListener; //an "interface"
20: import javax.swing.JScrollPane;
22: public class StudentMarks3 extends JFrame
23: {
24: private JTextArea display;
25: private JPanel buttonPanel;
26: private JButton fileButton;
27: private JButton byNameButton;
28: private JButton byMarkButton;
29: private JButton quitButton;
30: private Font font;
32: public StudentMarks3(String title)
33: {
34: super(title);
35: display = new JTextArea(15, 30);
36: getDataFromFile();
38: font = new Font("Monospaced", Font.PLAIN, 16);
39: display.setFont(font);
41: buttonPanel = new JPanel();
42: fileButton = new JButton("Load File");
43: byNameButton = new JButton("Sort by Name");
44: byMarkButton = new JButton("Sort by Mark");
45: quitButton = new JButton("Quit");
47: //New code to create a button handler for the Quit button:
48: ButtonHandler buttonHandler = new ButtonHandler();
49: quitButton.addActionListener(buttonHandler);
51: buttonPanel.add(fileButton);
52: buttonPanel.add(byNameButton);
53: buttonPanel.add(byMarkButton);
54: buttonPanel.add(quitButton);
56: //New code to insert a JScrollPane:
57: add(new JScrollPane(display), BorderLayout.CENTER);
59: add(buttonPanel, BorderLayout.SOUTH);
60: }
61:
62: public void getDataFromFile()
63: {
64: try
65: {
66: Scanner in = new Scanner(new File("input.txt"));
67: String line;
68: while (in.hasNext())
69: {
70: line = in.nextLine();
71: display.append(line + "\n");
72: }
73: in.close();
74: }
75: catch (FileNotFoundException e)
76: {
77: //This exception may or may not happen. It will not happen
78: //if a file called "input.txt" is available for reading.
79: System.out.println("Error: Required input file was not found.");
80: System.exit(0);
81: }
82: }
84: //New: A class for handling button clicks
85: class ButtonHandler implements ActionListener
86: {
87: public void actionPerformed(ActionEvent e)
88: {
89: //Later on there will be other buttons to check ...
90: if (e.getSource() == quitButton)
91: System.exit(0);
92: }
93: }
95: public static void main(String[] args)
96: {
97: StudentMarks3 app = new StudentMarks3("Record of Student Marks");
98: app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
99: app.pack();
100: app.setVisible(true);
101: }
102: }