public class GetRemoteTimeGUIApp extends JFrame
1: //GetRemoteTimeGUIApp.java
2: //Retrieves the time from a "remote" server.
4: import java.awt.*;
5: import java.awt.event.*;
6: import java.net.*;
7: import java.io.*;
8: import javax.swing.*;
10: public class GetRemoteTimeGUIApp extends JFrame
11: implements ActionListener
12: {
13: private JTextField hostInput, display;
14: private JButton button;
15: private JButton exit;
17: public static void main(String [] args)
18: {
19: GetRemoteTimeGUIApp app = new GetRemoteTimeGUIApp();
20: app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21: app.makeGUI();
22: app.setSize(500, 140);
23: app.setVisible(true);
24: }
26: public void makeGUI()
27: {
28: getContentPane().setLayout(new FlowLayout());
30: hostInput = new JTextField(40);
31: getContentPane().add(hostInput);
32: button = new JButton("Enter host name above and click here " +
33: "to see its time below:");
34: getContentPane().add(button);
35: display = new JTextField(40);
36: getContentPane().add(display);
37: button.addActionListener(this);
39: exit = new JButton("Exit");
40: getContentPane().add(exit);
41: exit.addActionListener(this);
42: }
43:
44: public void actionPerformed(ActionEvent event)
45: {
46: if (event.getSource() == exit)
47: System.exit(0);
49: String theTime;
50: String host = hostInput.getText();
52: try
53: {
54: Socket socket = new Socket(host, 7013);
55: BufferedReader input = new
56: BufferedReader(new
57: InputStreamReader(socket.getInputStream()));
58: theTime = input.readLine();
59: display.setText("The time at " + host +
60: " is " + theTime + ". ");
61: socket.close();
62: }
63: catch (UnknownHostException e)
64: {
65: display.setText("No such host. ");
66: }
67: catch (IOException io)
68: {
69: display.setText(io.toString());
70: }
71: catch (Exception e)
72: {
73: System.out.println("Error: " + e.toString());
74: }
75: }
76: }