Chapter 9. Java™ Graphical User Interface(GUI) Components

Table of Contents

Introduction
AWT Components
Button (java.awt.Button)
Checkboxes (java.awt.Checkbox)
Radio Buttons (java.awt.CheckboxGroup)
Choice Buttons (java.awt.Choice)
Labels (java.awt.Label)
TextFields (java.awt.TextField)
An Example Component Application
Layout Policies
FlowLayout (java.awt.FlowLayout)
GridLayout (java.awt.GridLayout)
BorderLayout (java.awt.BorderLayout)
The Panel Container (java.awt.Panel)
A Layout Exercise
More AWT GUI Components
List (java.awt.List)
TextArea (java.awt.TextArea)
Scrollbar (java.awt.Scrollbar)
Using these Components
Introduction
ActionEvent (java.awt.event.ActionEvent)
Other Event Types
Using Other Components
Introduction
A Scrollbar Example
Exercise - The Font Chooser Applet
Mixing Components and Graphics
The Canvas class
A Functional Canvas Example
An Exercise For You. The Colour Chooser Applet

Introduction

Graphical User Interfaces (GUIs) are mechanisms for allowing users to enter data in the most economical and straightforward manner possible. Figure 9.1, “Example GUI: Font Dialog” shows an example GUI that is designed to allow a user to choose a font type, style and size. There are different controls such as buttons, lists, choice items and check box items. This dialog box allows the user to choose the options easily, while it also allows the programmer to carefully control the way that the user can enter the data, preventing the user from entering invalid options.

Figure 9.1. Example GUI: Font Dialog

Example GUI: Font Dialog

Java provides two mechanisms for developing user interface applications in Java - AWT and Swing. AWT (Abstract Windowing Toolkit) is the mechanism we will use here, but we will discuss Swing later. The AWT is tied directly to the operating system (Figure 9.1, “Example GUI: Font Dialog” is on the Windows OS) and so AWT applications will have a different "look-and-feel" on different operating systems.