Last updated 10/3/05
The standard Java packages for GUI development is the Abstract Windowing Toolkit (AWT) but is very complicated to use. AWT was part of Java 1.0. It was significantly revised in 1.1.
Another IO package for GUI development is the Swing package. Swing works for Java 1.1 and then became part of the standard Java 2 (1.2 and current 1.3) release.
Once familiar with the Swing package, having to use purely AWT will be easier to learn.
import java.awt.*; //core AWT import java.awt.event.*; //core event handling for AWT import javax.swing.*; //extensions to AWT
The proper GUI design is outside the realm of this course (Software Models and User Interfaces deal with more of the issues), but it is desirable to separate the model of the solution from the view of the solution.
Production applications with a GUI interface can quickly be complex. This separation of the model and view help keep the development under control.
The View
|
The Model
|
The third component, the Controller
To build an application that uses a GUI, you need to create a class that inherits from one of the Swing classes to handle the user interface.
Event-driven programming is a different approach to application development than the standard input-process-output model. In event-driven programming, you have:
Different types of "widgets" set up mechanisms for input and output
Many of these widgets require layout: the positioning of the widgets
import javax.swing;
The following components are needed for a GUI application.
To lay out the objects in a Swing window, the organization is based on a grid, similar to the table in a web page. The grid need not be predefined. Each GUI object, as it is added, is placed on the grid by row and column coordinates. Also, each object can span across multiple rows or columns. The row and columns are measured in simple units of equal size.
Typically these objects are "added" to a panel
JLabel label1 = new JLabel ("Text for label");
JButton button1 = new JButton("ButtonLabel")
JTextField text1 = new JTextField("initialContents", length);
JLabel
JTextField
Be sure you are not mixing the GUI layout and event handling with the data structures and their supporting routines.
import java.awt.*; import java.awt.event.*; import javax.swing; import java.io.*;
public class programClassName {
declarations of instance variables
declarations of widgets that need to be shared/accessed by multiple methods
...e.g. textFields
private methods (internal use) that manipulate the widget
inner class(es) eventHandlers, one each for different classes of events
public static void main (String args[]){
declarations of minor widgets
declarations of local variables
declaration of listeners
declare/instantiate/initialize frame
layout contents of frame
instantiate buttons and register their listeners
set up and show frame
rest of execution occurs via events and the event handling code elsewhere
}
}
Review of the RealEstate.java example