Java
Java intro
Java basics
Java variables
Java conditionals
Java loops
Java arrays
Java OOP 1
Java OOP 2
Java interfaces
Java Strings
Java user input
Java exceptions
Java packages
Java rand numbers
Java GUI's
Java GUI layout
Java events
Java applets
Java graphics
Java sounds
Java summary

Programming

Programming intro

Markup

First webpage guide HTML
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

Java GUI (Graphical User Interface) programming

Up to this point we have focused only on Java code that runs in a command prompt. In this lesson, we go beyond text and enter the realm of graphical programming.

This lesson focues on:

Importing the necessary package to create graphical programs

To create graphical programs, you have to import the java.awt package. This package contains classes for several graphical components such as frames, windows, textboxes, buttons, and more.

Example:

import java.awt.*;

class AClass{

}

Creating a frame

In Java, a frame is a standard window used for graphical applications. It has minimize, maximize, and close buttons in its top right corner and can be moved and resized (by default). Frames are created using the Frame class.

Frame class constructors:

Frame class methods:

You can create a frame by extending the Frame class:

Example:

import java.awt.*;

class AFrame extends Frame{

    public static void main(String[] args){

        AFrame frame = new AFrame();
        frame.setSize(200, 200); 
        frame.setVisible(true);

    }

}

Output:

Basic frame

In the above example, a class named AFrame that extends the Frame class is created. Within the main() method of this class, a new instance of AFrame is created. It's size is set as 200 by 200, and it is set to be initially visible.

You can also create a frame by creating an instance of the Frame class:

import java.awt.*;

class AFrame{

    public static void main(String[] args){

        Frame aFrame = new Frame();
        aFrame.setSize(200, 200); 
        aFrame.setVisible(true);
    
    }

}

Output:

Basic frame

In the above example, a class named AFrame is created. Within the main() method of this class, a new instance of Frame is created. It's size is set as 200 by 200, and it is set to be initially visible.

NOTE: Initially, a frame will not be visible. You have to set it to be visible using the setVisible() method.

GUI components

There are various graphical components you can add to frames. Some of these components include labels, buttons, textboxes, and textareas.

A Frame with several graphical components:

import java.awt.*;

class FrameWithComponents{

    public static void main(String[] args){

    //create a new frame whose 
    //title is "Frame with components"
    Frame AFrame = new Frame("Frame with components");

    //create a label, a button, a textbox, and a textarea
    Label lblOne = new Label("This is a label");
    Button btn1 = new Button("This is a button");
    TextField tf1 = new TextField();
    TextArea ta1 = new TextArea();

    //set the text of the
    //textfield to "This is a textbox
    tf1.setText("This is a textbox");

    //append to the textarea the text
    //"Number of columns in this textarea: "
    //+ the number of columns in the textarea
    ta1.append("Number of columns in this textarea: " 
                + ta1.getColumns());

    //add the components to the frame
    AFrame.add(lblOne);
    AFrame.add(btn1);
    AFrame.add(tf1);
    AFrame.add(ta1);

    //set the size of
    //the frame to 450 by 300
    AFrame.setSize(450, 300);

    //set the layout of
    //the frame to FlowLayout
    AFrame.setLayout(new FlowLayout());

    //set the frame to be initially visible
    AFrame.setVisible(true);

    }

}

Output:

Frame with components

In the above example, a class named FrameWithComponents is created. Within the main() method of this class, a frame is created, as well as a label, a button, a textbox, and a textarea. The text that will appear on the label and the button is set in their initialization. The text that will appear in the textbox is set through the setText() method. The text that will appear in the textarea is set through the append() method. Another method used with the textarea is getColumns, which is used to return the number of columns in the textarea.

The size of the frame is set to 450 by 300 with the setSize method. The layout of the frame is set to FlowLayout which means that the components in the frame will appear from left to right in the order in which they are added (GUI layout is covered in detail in the next lesson. The frame is set to be initially visible with the setVisible() method.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments