1 import java.io.*;
2 import java.awt.*;
3 import java.awt.event.*;
4
5 public class ReadingHead extends Frame implements MouseListener {
6 private Button quit, read;
7 private TextArea text;
8
9 public static void main(String args[]) {
10 new ReadingHead();
11 }
12
13 public ReadingHead () {
14 super("ReadingHead");
15 this.setLayout(new BorderLayout());
16
17 Panel p = new Panel();
18 p.setLayout(new GridLayout(1,2));
19 this.add(p, BorderLayout.SOUTH);
20
21
22 quit = new Button("Quit");
23 quit.addMouseListener(this);
24 p.add(quit);
25
26
27 read = new Button("Read File");
28 read.addMouseListener(this);
29 p.add(read);
30
31
32 text = new TextArea();
33 this.add(text, BorderLayout.CENTER);
34
35
36 this.setSize(320,240);
37 this.setVisible(true);
38 }
39
40 public void mouseClicked(MouseEvent mouseEvent) {
41 if(mouseEvent.getSource() instanceof Button) {
42 Button s = (Button) mouseEvent.getSource();
43
44 if(s == quit) {
45 System.exit(0);
46 } else if(s == read) {
47 try {
48 File f = new File("/etc/hostconfig");
49 FileReader fr = new FileReader(f);
50 BufferedReader br = new BufferedReader(fr);
51
52 String total = "";
53 String l = br.readLine();
54
55 while(l != null) {
56 total = total + l + '\n';
57 l = br.readLine();
58 }
59
60 text.setText(total);
61 } catch (FileNotFoundException fnf) {
62 text.setText("File not found");
63 } catch(IOException io) {
64 text.setText("Couldn't read file");
65 }
66 }
67 }
68 }
69
70 public void mousePressed(MouseEvent mouseEvent) {
71 }
72
73 public void mouseReleased(MouseEvent mouseEvent) {
74 }
75
76 public void mouseEntered(MouseEvent mouseEvent) {
77 }
78
79 public void mouseExited(MouseEvent mouseEvent) {
80 }
81 }