Fundamental GUI Classes

All Java GUI classes are based on the fundamental classes in the Application Windowing Toolkit, or AWT. These classes are also used as the base classes of an older set of components (which are usually regarded as part of the AWT). Swing components come with recent java Java releases. If you download the most recent release of Java, Swing will be part of it.

The older AWT components are similar to Swing components, but Swing components are more versatile. Also, Swing integrates smoothly with the Java Foundation Classes. The Java Foundation Classes provide many classes needed for modern software development. Sun Microsystems recommends that you use Swing for all new application code.

Container Classes

A GUI program consists of a collection of graphical components that are all placed inside one or more windows. The components are contained by the window. A container is an object that can hold other GUI components. Visually, a container is an area of the screen and the objects it contains are shown as smaller areas within it.

In Java terminology, a window (such as the browser window) is a container. The buttons, sliders, icons and other GUI components are contained (by the container).

Swing JFrames

You create a GUI by extending class JFrame. This class holds the basic GUI functions. Extend this class by adding the components and methods that your application needs. This chapter introduces these ideas.

Let's first examine the graphical components. These must be placed inside a container. Visually, a container is a rectangular area of the monitor screen and its components are smaller areas within it.

In Java, a frame is a window that has nice borders, various buttons along the top border, and other features. What you usually call a "window" Java calls a "frame". A frame is a container object, so GUI components can be placed in it.

Like all software objects, a frame object is actually a section of main memory that holds information and methods. With the help of the operating system and the graphics board, Java paints a picture on the computer monitor that represents the frame. People often speak as if the frame were the actual picture on the monitor. In fact, what you see on the monitor is just a graphical representation of the frame.

GUI application programs are usually organized around one or more frames.

Small GUI Program

Our first GUI program does not have a listener nor any application code, and is not a useful program. But it will get us started. Here is the complete program:

import java.awt.*; // 1. import the application windowing toolkit

import javax.swing.*; // 2. import the Swing classes

public class TestFrame1{ // usual main() method

public static void main ( String[] args ) {

JFrame frame = new JFrame("Test Frame 1"); // 3. construct a JFrame object

frame.setSize(200,100); // 4. set it to 200 pixels wide by 100 high

frame.setVisible( true ); // 5. ask it to become visible on the screen

frame.setDefaultCloseOperation ( WindowConstants.EXIT_ON_CLOSE );

// 6. say what the close button does

} // end main

} // end TestFrame1

First a JFrame is created by invoking its constuctor. The argument to the constructor sets the title of the frame. The setSize(200,100) method call makes the rectangular area 200 pixels wide by 100 pixels high.

The setVisible(true) method call makes the frame appear on the screen. If you forget to do this, the frame object will exist as an object in memory, but no picture will appear on the screen. A call to setVisible(false) makes the frame invisible, but does not destroy the software object.

It is easy to forget to include setVisible(true). If you run your GUI program, and nothing happens, check that you have called this method.

The setDefaultCloseOperation() method picks the action to perform when the "close" button of the frame is clicked. This is the little button usually at the top right of the frame. If you forget to call this method with the appropriate constant, clicks on the close button will be ignored.

The program displays a frame like the one here. While the program is running, you can click on the frame and drag it around, you can minimize it, you can grab a border and resize it, and so on. All of this is built into the JFrame class. Your program gets all of these features when it constructs a JFrame object.

To stop the program, click on the "close button" (the little square that contains an X in the top right of the frame).

Extending the JFrame Class

The usual way of writing a GUI is to define your own class by extending the JFrame class. Here is a program that does that. It also includes several other new features.

import java.awt.*;

import javax.swing.*;

/**********************************************************

*class MyFrame

********************************************************/

class MyFrame extends JFrame{

// paint() is called automatically by the system

// to display your customizations to the frame.

public void paint ( Graphics g ) {

super.paint(g); // call the paint method of the class JFrame

g.drawString("A MyFrame object", 10, 50 ); // draw a String at location x=10 y=50

} // end paint

} // end class

/**********************************************************

* class TestFrame2

********************************************************/

public class TestFrame2{

public static void main ( String[] args ) {

MyFrame frame = new MyFrame(); // construct a MyFrame object

frame.setSize( 150, 100 ); // set it to 150 wide by 100 high

frame.setVisible( true ); // ask it to become visible

frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );

} // end main

} // end class

A JFrame is painted on the screen by the Java system whenever needed to keep the screen up to date. The Java system does this automatically. Your program does not need to keep track of what has been happening on the screen.

The class MyFrame extends the class JFrame. MyFrame objects, also, will be automatically re-painted when needed. The standard parts of the frame—the background, the borders, the controls—are drawn automatically by the Java system. The paint() method of your frame is called by the Java system (not by you) to draw your customizations to the frame. You write a paint() method to customize what is painted.

Example Program with a Button

Here is an example program that adds a button to a frame.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class ButtonFrame extends JFrame{

JButton bChange ; // declare the button object

ButtonFrame() { // constructor for ButtonFrame

bChange = new JButton("Click Me!"); // create a new button

Container c = getContentPane(); // create a container that will hold buttons

c.add( bChange ); // add the button to the JFrame

setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );

} // end constructor

} //end class

public class ButtonDemo{

public static void main ( String[] args ) {

ButtonFrame frm = new ButtonFrame();

frm.setSize( 200, 150 );

frm.setVisible( true );

} // end main

} //end class

To construct a JButton object, use new. Then add the button to the frame's content pane. The content pane is a container that represents the main rectangle of the frame. To get a reference to the content pane, use the getContentPane() method of the frame. The add method of a content pane puts a JButton (or other component) into the frame.

The picture shows what the program displays. The entire frame is filled with the button! (We will fix this shortly.) You can move the mouse pointer over the button and click on it, and the button will appear to move in and out. But the program does nothing in response to that event.

Layout Manager

When you add() buttons (and other components) to a content pane, a layout manager utomatically decides what size they will be and where they will go. This is convenient, because you can add components without worrying about the details. The layout manager is like a little artist inside the computer. You say what components you want and the layout manager lays them out in the picture.

The layout manager sometimes makes odd choices. However, there are several kinds of layout managers. Each layout manager has a different style of positioning components. Usually there is a layout manager that will do the job you want. If you don't specify otherwise, a default layout manager is used.

The FlowLayout Manager

Our program will look better if it uses the FlowLayout manager. The FlowLayout manager puts components into the frame row by row in the order they are added. (Later on we will add more than one component). It also picks reasonable sizes for components.

The FlowLayout manager is part of the java.awt package.

Setting the Layout Manager

Here is how to set the layout manager for our JFrame:

import java.awt.*;

. . .

public class ButtonFrame extends JFrame{

JButton bChange;

ButtonFrame() {

bChange = new Button("Click Me!");

Container c = getContentPane();

c.setLayout( new FlowLayout() ); // choose the layout manager

c.add( bChange );

setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );

}

}

The layout manager is set using the setLayout() method of the content pane.

Exercise:

View examples ButtonTest.java and LabelTest.java. Create your own application with buttons and labels.

Page1