Chapter 11. Graphical User Interfaces

Graphical user interfaces, or GUIs, represent an excellent example of software modularity and reuse. GUIs are almost always assembled from libraries of predefined building blocks. To Motif programmers on Unix systems, these GUI building blocks are known as widgets. To Windows programmers, they are known as controls. In Java, they are known by the generic term components, because they are all subclasses of java.awt.Component.[1]

[1] Except for menu-related AWT components, which are all subclasses of java.awt.MenuComponent.

In Java 1.0 and 1.1, the standard library of GUI components was AWT the package java.awt and its subpackages. There is debate as to what the letter A stands for in this acronym, but "WT" stands for "windowing toolkit." In practice, it is always called AWT. In addition to GUI components, the AWT includes facilities for drawing graphics, performing cut-and-paste-style data transfer, and other related operations. On most platforms, AWT components are implemented using the operating-system's native GUI system. That is, AWT components are implemented on top of Windows controls on Windows operating systems, on top of Motif widgets on Unix systems, and so on. This implementation style led to a least-common-denominator toolkit, and, as a result, the AWT API is not as complete and full featured as it should be.

Java 1.2 introduced a new library of GUI components known as Swing. Swing consists of the javax.swing package and its subpackages. Unlike the AWT, Swing has a platform-independent implementation and a state-of-the-art set of features. Although Swing first became a core part of the Java platform in Java 1.2, a version of Swing is available for use with Java 1.1 platforms. Swing has largely replaced the AWT for the creation of GUIs, so we'll focus on Swing in this chapter. Note that Swing defines a new, more powerful set of GUI components but retains the same underlying GUI programming model used by the AWT. Thus, if you learn to create GUIs with Swing components, you can do the same with AWT components.

There are four basic steps to creating a GUI in Java:

  1. Create and configure the components

    You create a GUI component just like any other object in Java, by calling the constructor. You'll need to consult the documentation for individual components to determine what arguments a constructor expects. For example, to create a Swing JButton component that displays the label "Quit", simply write:

    JButton quit = new JButton("Quit");

    Once you have created a component, you may want to configure it by setting one or more properties. For example, to specify the font a JButton component should use, you can write:

    quit.setFont(new Font("sansserif", Font.BOLD, 18));

    Again, consult the documentation for the component you are using to determine what methods you can use to configure the component.

  2. Add the components to a container

    All components must be placed within a container. Containers in Java are all subclasses of java.awt.Container. Commonly used containers include JFrame and JDialog classes, which represent top-level windows and dialog boxes, respectively. The java.applet.Applet class, which is subclassed to create applets, is also a container and can therefore contain and display GUI components. A container is a type of component, so containers can be, and commonly are, nested within other containers. JPanel is a container that is often used in this manner. In developing a GUI, you are really creating a containment hierarchy: the top-level window or applet contains containers that may contain other containers, which in turn contain components. To add a component to a container, you simply pass the component to the add( ) method of the container. For example, you can add a quit button to a "button box" container with code like the following:

    buttonbox.add(quit);
  3. Arrange, or lay out, the components

    In addition to specifying which components are placed inside of which containers, you must also specify the position and size of each component within its container, so that the GUI has a pleasing appearance. While it is possible to hardcode the position and size of each component, it is more common to use a LayoutManager object to lay out the components of a container automatically according to certain layout rules defined by the particular LayoutManager you have chosen. We'll learn more about layout management later in this chapter.

  4. Handle the events generated by the components

    The steps described so far are sufficient to create a GUI that looks good on the screen, but our graphical "user interface" is not complete, because it does not yet respond to the user. As the user interacts with the components that make up your GUI using the keyboard and mouse, those components generate, or fire, events. An event is simply an object that contains information about a user interaction. The final step in GUI creation is the addition of event listeners objects that are notified when an event is generated and respond to the event in an appropriate way. For example, the Quit button needs an event listener that causes the application to exit.

We'll look at each of these topics in more detail in the first few sections of this chapter. Then we'll move on to more advanced GUI examples that highlight particular Swing components or specific GUI programming techniques. As usual, you'll want to have access to AWT and Swing reference material as you study the examples. One such reference is Java Foundation Classes in a Nutshell. You may also find it helpful to read this chapter in conjunction with Chapters 2 and 3 of that book.



Java Examples in a Nutshell
Java Examples in a Nutshell, 3rd Edition
ISBN: 0596006209
EAN: 2147483647
Year: 2003
Pages: 285

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net