Java Reference
In-Depth Information
Fig. 2.5 Example output from the UDPEchoServer program
2.3 Network Programming with GUIs
Now that the basics of socket programming in Java have been covered, we can add
some sophistication to our programs by providing them with graphical user inter-
faces (GUIs), which users have come to expect most software nowadays to provide.
In order to concentrate upon the interface to each program, rather than upon the
details of that program's processing, the examples used will simply provide access
to some of the standard services, available via 'well known' ports. Some of these
standard services were listed in Fig. 1.1 .
Example
The following program uses the Daytime protocol to obtain the date and time from
port 13 of user-specifi ed host(s). It provides a text fi eld for input of the host name
by the user and a text area for output of the host's response. There are also two but-
tons, one that the user presses after entry of the host name and the other that closes
down the program. The text area is 'wrapped' in a JScrollPane , to cater for long
lines of output, while the buttons are laid out on a separate panel. The application
frame itself will handle the processing of button presses, and so implements the
ActionListener interface. The window-closing code (encapsulated in an anonymous
WindowAdapter object) ensures that any socket that has been opened is closed
before exit from the program.
import java.awt.*;
import java.awt.event.*;
Search WWH ::




Custom Search