Sams Teach Yourself C in 21 Days

(singke) #1
More Java Techniques 749

BD6


Finally, lines 31 to 35 display one of two farewell messages depending on whether the
fileErrorflag is trueorfalse.

Doing Graphics and Windows ............................................................................


Any modern programming language must have support for graphics. Using Java for con-
sole applications, as we have been doing, is fine for a learner just starting out, but almost
any real-world program is going to require windows, menus, dialog boxes, and other
screen elements. The developers of Java approached this problem by creating the
Abstract Window Toolkit (AWT). The AWT is perhaps the most complex part of Java,
and entire books have been devoted just to the AWT. Only a brief look will be provided
here. You can explore other sources of information if you want to go further.

There are other libraries within Java as well, libraries such as Swing.
Note Covering all the libraries is beyond the scope of this book.

Creating Windowing Applications ................................................................


Screen windows in a Java program are based on the Frameclass. Any application that
will use screen windows will have a declaration like this:
public class MyWindowingApplication extends Frame {
}
TheFrameclass provides most of the basic window functionality that you need, such as a
title bar, a border, the capability to be resized, and so forth. You can add the additional
elements that are specific to your program. To see how easy it is, the program in Listing
B6.3 creates a window and displays a message in it.

LISTINGB6.3 window.java. A Java program to display a message in a window
1: import java.awt.*;
2:
3: public class AWT_Test1 extends Frame {
4:
5: // Constructor.
6: public AWT_Test1(String title) {
7: super(title);
8: Label lbl = new Label(“Hello from Java”, Label.CENTER);
9: lbl.setFont(new Font(“Helvetica”, Font.PLAIN, 16));
10: add(“Center”, lbl);
11: }

41 448201x-Bonus6 8/13/02 11:23 AM Page 749

Free download pdf