Next: Interfaces. Up: Graphical user interfaces. Previous: None.


Components and containers

Textbook: Section 12.2

Many common programs involve graphical user interfaces. We're going to look at Java's libraries for building GUIs, called Swing. (There's an older library Java used for this, called AWT. This is what the textbook covers. We're going to look at Swing in this class, though.)

The JFrame class

The first class we'll look at is the JFrame, which represents a window. Consider the following simple program.

import java.awt.*;       // we'll include these three import lines in
import java.awt.event.*; // all our programs using Swing.
import javax.swing.*;

public class EmptyWindow extends JFrame {
    public EmptyWindow() {
        pack();
    }

    public static void main(String[] args) {
        EmptyWindow window = new EmptyWindow();
        window.show();
    }
}
This particular program isn't very useful - it just creates a frame and displays it, effectively showing an empty window.

A JFrame has just a few methods worth talking about for now.

JFrame()
(Constructor method) Creates a JFrame to represent a window.

void pack()
Resizes the window to fit what it contains.

void show()
Displays the window on the screen.

Container getContentPane()
Returns an object into which you can place the graphical user interfaces on the screen.

Containers

A Container is an object used for holding components. (Examples of components include buttons and text fields.) For the moment, we're just going to pay attention to one of its methods.

void add(Component what, Object info)
Inserts what into the container, using info as information about where the object should be placed.

The info parameter to add(), saying where to place the object, can have one of several possibilities.
BorderLayout.NORTHBorderLayout.EAST
BorderLayout.SOUTHBorderLayout.WEST
BorderLayout.CENTER
These say where to place the component into the window. (You'll notice that this is pretty restricted. There are other ways to lay out components in the window, but play along for the moment.)

Components

Finally, there are the components. The first one of these we'll see is the JButton, which represents a button on the screen. For the moment, we'll just worry about the constructor method.

JButton(String label)
Creates a button holding the string label.

Another type of component is the text field, where the user can type something. The JTextField class is

JTextField()
Creates a text field.

String getText()
Returns the current text inside the field.

void setText(String text)
Changes the current text of the field to text.

Here's a program that creates a window containing two buttons and a text field. We're going to work on making the buttons do something soon. For the moment, all the program does is display the components.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonExample extends JFrame {
    JButton incr_button;
    JButton quit_button;
    JTextField number_field;

    public ButtonExample() {
        incr_button = new JButton("Increment");
        quit_button = new JButton("Quit");
        number_field = new JTextField();

        Container contents = getContentPane();
        contents.add(incr_button, BorderLayout.NORTH);
        contents.add(number_field, BorderLayout.CENTER);
        contents.add(quit_button, BorderLayout.SOUTH);
        pack();
    }

    public static void main(String[] args) {
        (new ButtonExample()).show();
    }
}
When executed, this makes a window containing a text field sandwiched by a button labeled ``Increment'' at the top and a button labeled ``Quit'' below.


Next: Interfaces. Up: Graphical user interfaces. Previous: None.