To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98
Day 5 Windows
Java has a rich set of classes that help you build graphical user interfaces. You can use these various classes, such as Frames, Panels, Labels, Buttons, TextFields, TextAreas, List boxes, Choice menus, and menus, to construct user interfaces. These classes are grouped into packages, and these packages are all part of whats known as the Abstract Windowing Toolkit (AWT).
The AWT classes provide a platform-independent interface to develop visual programs and graphical user interfaces. For each platform on which Java runs, the AWT components are automatically mapped to the platforms specific components. This mapping enables the user applications to be consistent with other applications running on the specific platform. For instance, a Java user interface on Windows 95 looks the same as other Windows 95 applications. But the same program might look slightly different when its running on a Macintosh. Today, well learn the AWT concepts. Specifically, well be talking mostly about the Frame class. Well cover these topics:
- The Frame class
- A Java classs tutorial
- Class inheritance
- Extending classes
- The super() method
- Frame class events
- Programs that use the Frame class
- The this keyword
- Garbage collection in Java
Creating Windows with the Frame Class
A Frame is a top-level window with a title and a border. Normally, Frame windows appear initially above the main applet window. To create and use a frame requires very little. The following source code shows how to create and show a Frame window:
// Declared global to the applet.
Frame m_MyFrame;
// Created in the init() method.
m_MyFrame = new Frame( "MyFrame Window" );
m_MyFrame.setVisible( true );
The Frame class actually has two constructors. The one Ive used in the preceding example takes one argumenta string. This string becomes the title of the Frame window. The other kind of Frame constructor doesnt take any arguments, and the Frame window wont have a title. However, you can add a title later if you want with the setTitle() method.
One of the problems with the source-code example I just gave is that the size of the Frame window will be a width of 0 and a height of 0. For that reason, its always a good idea somewhere before the window is made visible to use the setSize() method. The setSize() method lets you specify the width and height of the Frame window that will appear. The following example shows how to use the setSize() method to set a Frame window to have a width of 200 and a height of 200:
m_MyFrame.setSize( 200, 200 );
Okay, now its time to build an entire applet that creates a Frame window and displays it. Listing 5.1 shows a simple applet that creates and displays a Frame window thats titled MyFrame Window.
Listing 5.1 The MyFrameWindow Applet
1 import java.awt.*;
2 import java.applet.*;
3
4 public class Applet1 extends Applet
5 {
6 Frame m_MyFrame;
7
8 public void init()
9 {
10 m_MyFrame = new Frame( "MyFrame Window" );
11 m_MyFrame.setSize( 200, 200 );
12 m_MyFrame.setVisible( true );
13 }
14
15 }
When you compile and run the program, youll see a Frame window appear as shown in Figure 5.1. Now suppose you want to close the window. Normally, you would click the upper-right corner of the menu or use the system menu to select Close, but the window isnt closed in either case because you havent yet added the capability to your program to close the window. Well show you how to do that later today when we talk about frame events.
Figure 5.1 This program creates and displays a Frame window.
Note: You might look through the MSDN Help and see methods you might want to use, but theyre marked deprecated. Deprecated means that these are older Java API methods. In Java 1.1, new methods were introduced to fix interface or implementation bugs. Old deprecated methods will be supported in the short term for Java 1.0, but your programs wont compile. When the browsers find these methods in older applets, they interpret them just fine.
There are two methods that belong to the Frame class that you might find useful. The first one is the setResizable() method. This lets you set the resizable flag, which determines whether the frame is resizable. It takes a single argument. The following line of code makes a Frame window nonresizable:
m_MyFrame.setResizable( false );
Another method you might find useful is the setTitle() method. This changes the title text of the Frame window. One reason why this might be especially useful is that at different times during your programs execution, you might want to change the title on the Frame window to give users different information. The following example shows how to use the setTitle() method to set the title of the Frame window to new text:
m_MyFrame.setTitle( "This is a new title!" );
If you look through the MSDN online help at the Frame class, youll notice that it doesnt really have many methods of its own. There are still methods, though, that the Frame class has at its disposal. The reason it has additional methods it can use is that Frame is an extension of Window, Window is an extension of Container, Container is an extension of Component, and Component is an extension of Object. Therefore, the Frame class inherits all methods from these classes above. Well talk more about inheritance shortly, but for now, well go ahead and show you how to set a background color for a Frame class. You can use the following line of code to set the Frame class to red:
m_MyFrame.setBackground( Color.red );
Note: Sometimes its confusing to know the difference between a Components setForeground() method and a Graphics classs setColor() method. First of all, these methods do the same thing. They set the foreground color to the value of a parameter. The difference is in where you use them. If youre trying to set the color of a Component, use the setForeground() method. If youre in a paint() method and you want to draw in a certain color with one of the Graphics classs methods, use the setColor() method.
Well talk a lot about the Graphics class and the Color class during Day 10, Fonts and Text, and Day 11, Drawing. For now, its important to note that for a Component (or in this case the extended Frame class), youll use setBackground().
|