home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


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 what’s 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 platform’s 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 it’s running on a Macintosh. Today, we’ll learn the AWT concepts. Specifically, we’ll be talking mostly about the Frame class. We’ll cover these topics:

  The Frame class
  A Java class’s 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 I’ve used in the preceding example takes one argument—a string. This string becomes the title of the Frame window. The other kind of Frame constructor doesn’t take any arguments, and the Frame window won’t 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, it’s 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 it’s 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 that’s 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, you’ll 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 isn’t closed in either case because you haven’t yet added the capability to your program to close the window. We’ll 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 they’re 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 won’t 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 program’s 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, you’ll notice that it doesn’t 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. We’ll talk more about inheritance shortly, but for now, we’ll 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 it’s confusing to know the difference between a Component’s setForeground() method and a Graphics class’s 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 you’re trying to set the color of a Component, use the setForeground() method. If you’re in a paint() method and you want to draw in a certain color with one of the Graphics class’s methods, use the setColor() method.

We’ll talk a lot about the Graphics class and the Color class during Day 10, “Fonts and Text,” and Day 11, “Drawing.” For now, it’s important to note that for a Component (or in this case the extended Frame class), you’ll use setBackground().



Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.