Java Java intro
  1. Java intro
  2. Java basics
Java core
  1. Java variables
  2. Java conditionals
  3. Java loops
  4. Java arrays
  5. Java strings
Object-oriented Java
  1. Java OOP 1
  2. Java OOP 2
  3. Java packages
  4. Java interfaces
Java interactive
  1. Java user input
  2. Java exceptions
  3. Java events
Graphical Java
  1. Java GUI
  2. Java GUI layout
  3. Java graphics
Java extras
  1. Java applets
  2. Java sounds
  3. Java random numbers
Java wrap-up
  1. Java summary

Java GUI (Graphical User Interface) programming

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

This tutorial focues on:

Graphical programs package

Import the java.awt package to create graphical programs. This package contains classes for displaying several graphical components such as frames, textboxes, labels, and buttons, as well as drawing graphics and displaying images.

Example:
import java.awt.*; class GraphicalProgram{ }

Creating a frame

In Java, a frame is a standard graphical window. 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

Create a frame by extending the Frame class:
import java.awt.*; class AFrame extends Frame{ public static void main(String[] args){ AFrame frame = new AFrame(); frame.setSize(200, 200); frame.setVisible(true); } }
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); } }

Both sets of code from above produce the same frame:

Basic frame

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 including labels, buttons, textboxes, and textareas. Each component is created through a class and each of these classes has methods to work with the component.

In the example below you will find usage of a few different classes used to create graphical components as well as usage of some of their methods.

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 Java GUI layout tutorial).

import java.awt.*; class FrameWithComponents{ public static void main(String[] args){ Frame AFrame = new Frame("Frame with components"); Label lblOne = new Label("This is a label"); Button btn1 = new Button("This is a button"); TextField tf1 = new TextField(); TextArea ta1 = new TextArea(12, 40); tf1.setText("This is a textbox"); ta1.append("Number of columns in this textarea: " + ta1.getColumns()); //the add() method of the Frame class is //used to add components to the frame AFrame.add(lblOne); AFrame.add(btn1); AFrame.add(tf1); AFrame.add(ta1); AFrame.setSize(450, 300); AFrame.setLayout(new FlowLayout()); AFrame.setVisible(true); } }

What it will look like:

Frame with components
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information