Java: GUI Overview

A GUI (Graphical User Interface) is easy to create after you have a few basic elements: containers, layouts, components, listeners, etc. There are simple ways to use each of these, yet there is no limit on how sophisticated the interface can get. Good guidance on the appearance can be found in Sun's Java Look and Feel Design Guidelines, which is available on-line for free (java.sun.com/products/jlf/ed2/book/).

Containers

Containers are windows (eg, JFrame) or panels (eg, JPanel) that contain components. The most common top-level containers are JFrame (window) and JApplet. To organize components within a window or applet, it is common to group components in panels (JPanel).

Layouts

Layouts specify how to arrange components in a container. Every container starts with a default layout manager. It's possible to use absolute positioning, but it's better to use one of Java's layout managers: FlowLayout, BorderLayout, GridLayout, GridBagLayout, BoxLayout, etc.

Components

Components are the user interface controls like buttons (JButton, ...), text boxes (JTextField JTextArea, ...), menus, etc. These are added to a container with the add() method, and the place they appear on a container is determined by the layout which is used for that container.

Listeners

Listeners are attached to components and contain the code that is called when the component is used (eg, a button is clicked). This is how a user action on a component (like a click on a JButton or moving a JSlider) is connected to a Java method.

Other

The above are essential elements for user interfaces, but others are also important: drawing graphics, animation, mouse, keyboard, sound, ...

Program Organization

There are some simple patterns using constructors, anonymous listeners, etc to produce a reasonable program.

Tools to help build a GUI

Some IDEs have GUI editors. There are also separate GUI editors, eg XUI and UICompiler.