12.14. Programming Exercises

 
[Page 401 ( continued )]

12.3. The Java GUI API

The design of the Java API for GUI programming is an excellent example of the use of classes, inheritance, and interfaces. The API contains the essential classes listed below. Their hierarchical relationships are shown in Figures 12.2 and 12.3.

Figure 12.2. Java GUI programming utilizes the classes shown in this hierarchical diagram.


[Page 402]
Figure 12.3. JComponent and its subclasses are the basic elements for building graphical user interfaces.

The GUI classes can be classified into three groups: container classes , helper classes , and component classes . The container classes, such as JFrame , JPanel , and JApplet , are used to contain other components . The helper classes, such as Graphics , Color , Font , FontMetrics , and Dimension , are used by components and containers to draw and place objects. The GUI component classes, such as JButton , JTextField , JTextArea , JComboBox , JList , JRadioButton , and JMenu , are subclasses of JComponent .

Note

The JFrame , JApplet , JDialog , and JComponent classes and their subclasses are grouped in the javax.swing package. All the other classes in Figure 12.2 are grouped in the java.awt package.


12.3.1. Swing GUI Components

Component is a superclass of all the user-interface classes, and JComponent is a superclass of all the lightweight Swing components, Since JComponent is an abstract class, you cannot use new JComponent() to create an instance of JComponent . However, you can use the constructors of concrete subclasses of JComponent to create JComponent instances. It is important to become familiar with the class inheritance hierarchy. For example, the following statements all display true:

 JButton jbtOK =   new   JButton(   "OK"   ); System.out.println(jbtOK   instanceof   JButton); System.out.println(jbtOK   instanceof   AbstractButton); System.out.println(jbtOK   instanceof   JComponent); System.out.println(jbtOK   instanceof   Container); System.out.println(jbtOK   instanceof   Component); System.out.println(jbtOK   instanceof   Object); 


[Page 403]

12.3.2. Container Classes

Container classes are GUI components that are used as containers to contain other GUI components. Window , Panel , Applet , Frame , and Dialog are the container classes for AWT components. To work with Swing components, use Component , Container , JFrame , JDialog , JApplet , and JPanel .

  • Container is used to group components. Frames, panels, and applets are examples of containers.

  • JFrame is a window not contained inside another window. It is the container that holds other Swing user-interface components in Java GUI applications.

  • JDialog is a popup window or message box generally used as a temporary window to receive additional information from the user or to provide notification that an event has occurred.

  • JApplet is a subclass of Applet . You must extend JApplet to create a Swing-based Java applet.

  • JPanel is an invisible container that holds user-interface components. Panels can be nested. You can place panels inside a container that includes a panel. JPanel can also be used as a canvas to draw graphics.

12.3.3. GUI Helper Classes

The helper classes, such as Graphics , Color , Font , FontMetrics , Dimension , and LayoutManager , are not subclasses of Component . They are used to describe the properties of GUI components, such as graphics context, colors, fonts, and dimension.

  • Graphics is an abstract class that provides a graphical context for drawing strings, lines, and simple shapes .

  • Color deals with the colors of GUI components. For example, you can specify background or foreground colors in components like JFrame and JPanel , or you can specify colors of lines, shapes, and strings in drawings.

  • Font specifies fonts for the text and drawings on GUI components. For example, you can specify the font type (e.g., SansSerif), style (e.g., bold), and size (e.g., 24 points) for the text on a button.

  • FontMetrics is an abstract class used to get the properties of the fonts.

  • Dimension encapsulates the width and height of a component (in integer precision) in a single object.

  • LayoutManager is an interface whose instances specify how components are arranged in a container.

Note

The helper classes are in the java.awt package. The Swing components do not replace all the classes in AWT, only the AWT GUI component classes (e.g., Button , TextField , TextArea ). The AWT helper classes remain unchanged.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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