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
{
{
win.show(); //Display it
Notes: Try it! The next example shows how you can add another component into
the window. Building on this example, you can write your own GUI programs that
use text boxes, buttons, scrollbars and other things. This one just uses a simple
JLabel to display some text. Components are never added directly to the window. The window
has a Content Pane that is used to store components. So this program first gets
a pointer to the content pane, then adds the label to it. import java.awt.*; public class example2 // We’ll finish off on GUI’s by having a quick look at how we
can use the JPanel class to play with drawing and graphics. In order to do this, it is necessary to write a new class which
extends JPanel, and overwrite its paintComponent method. This method
is called automatically by the system whenever the component is drawn on the
screen, either initially or after the window has been minimised or hidden by
the user. The new paintComponent method receives the graphics
context of the component as an argument. This is as an object of class Graphics.
This Graphics object can then be used to draw the graphics. The first step in
the method should be to call paintComponent from the superclass in order
to draw the rest of the object. This is demonstrated in the following sample program. import java.awt.*; The program output should look like this:
//* **************************************************
class EgWindow extends JFrame
{
//* The constructor for EgWindow
//* ***************************
public EgWindow()
{
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() {
{
addWindowListener(new WindowAdapter() {
{
import java.awt.event.*;
import javax.swing.*;
//* This is the public class, where program execution begins
//* ************************************************
{
{
Example2.show(); //Display it
//* This is the definition of the window, which extends JFrame
//* **************************************************
class EgWindow2 extends JFrame
{
//* The constructor for EgWindow2
//* ***************************
public EgWindow2()
{
setSize(150,120);
//
//* Get a pointer to the content pane, create a label
//* and add it to the content pane
//* *****************************************
Container MyContentFrame = getContentPane();
JLabel NiceDay=new JLabel(" Have a nice day!");
MyContentFrame.add(NiceDay);
//
//* 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() {
{
import java.awt.event.*;
import javax.swing.*;
//
//* This is the public class, where program execution begins
//* ************************************************
public class example3
{
{
JFrame Example3 = new EgWindow3(); //Create a window
Example3.show(); //Display it
}
//
//* This is the definition of the window, which extends JFrame
//* **************************************************
class EgWindow3 extends JFrame
{
//* The constructor for EgWindow
//* ***************************
public EgWindow()
{
setSize(150,100);
Container Contents=getContentPane();
JPanel Panel = new PicturePanel();
Contents.add(Panel);
addWindowListener(new WindowAdapter() {
{
//*
//* This class extends JPanel and includes some drawing
//* objects in the paintComponent method
// ***********************************************
class PicturePanel extends JPanel
{
{
Gr.setColor(Color.yellow);
Gr.fillOval(55,10,30,30);
Gr.setFont(new Font("Serif",Font.ITALIC,20));
Gr.drawString("Sunshine City",12,55);