Java Tutorial for Beginners

 

Creating a Graphic User Interface (GUI) for your program

Java provides a hierarchy of classes useful in creating GUIís. Itís useful to have an idea of how this hierarchy fits together, since classes inherit objects and methods from the classes above them in the hierarchy.

The diagram below illustrates this hierarchy:

As you can see, everything in the hierarchy descends from the class Component. Component has lots of useful methods, such as setBackgroundColor, setFont etc. Container, the next class in the hierarchy, defines methods that are useful for maintaining a list of components that the container stores. For example, a panel can contain text boxes, labels etc. The Container class has methods such as add and remove.

The Window branch of the hierarchy contains classes that are useful in creating windows (known as Frames), and the JComponent branch contains classes that can be used to create the GUI components used in the window. These include buttons, labels, text boxes, scroll bars etc.

The original version of the Java language used the package java.awt to work with GUIís. However, this was revised later, and newer programs make use of the javax.swing package. All of the classes beginning with J (Eg JFrame, JButton etc) belong to javax.swing. Many of them extend classes defined under java.awt. Gui programs often have to import both packages, as they may use classes from each.

There are many GUI classes which do not fit into this hierarchy, but which are used in conjunction with screen components. These include:

Event objects e.g. ActionEvent. These are used to store information about a GUI event that has occurred, for example, the user has clicked a button.

Clipboard is an object used to hold information that the user has copied to the clipboard.

Color stores the definition of a color.

Font stores the definition of a font.

Graphics stores the graphics context of a component, and has methods that can be used for drawing on the component.

Image can be used to store an image in JPG or GIF format

Toolkit is used to interface with the userís environment. It can be used for many things, including loading images from a file.

Letís have a look at a simple GUI program that displays an empty window.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//
//* This is the public class, where program execution begins
//* ************************************************
public class GuiExample
{

public static void main(String[] args)
{
JFrame win = new EgWindow(); //Create a window
win.show(); //Display it
}
} 

// //* This is the definition of the window, which extends JFrame
//* **************************************************
class EgWindow extends JFrame
{
//
//* The constructor for EgWindow
//* ***************************
public EgWindow()
{
setTitle("Example1");
setSize(400,300);
//
//* An inner class is defined as the window Listener
//* It is only interested in the windowClosing event,
//* which will shut down the program
//* *****************************************
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
} // Terminate the program
});
}
}

Notes: