GUIs in Java

The Java classes for graphical user interfaces are arranged in two packages, known as AWT (java.awt) and Swimg (javax.swing). The model is complicated and powerful and we don't try to explain it in detail here. Instead, we provide boiler-plate examples and common idioms.

Basic graphics

The exercise is to draw a bar chart, looking like this:

from a data file which the user selects using the Open item of the File menu. We will do this in stages.

Frames and Panels

Look at the program BarChartAxes.java, which simply draws the axes and a single block:

Points to note:

You can do the first exercise now if you want to.

Events and menus

Look at the program BarChartEvents.java. It adds a File menu to our Frame, and an item Exit.

Points to note:

The event model:

Another example

Look at the program MyClickExample.java. It displays a frame consisting of some tiles. The program detects where the user clicks, and reports which tile it corresponds to.

Points to note:

© 2001 Mark Ryan and Alan Sexton