|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Swing was first released as a separate set of classes that worked with JDK 1.1. Swing was not actually part of JDK 1.1, but had to be downloaded and installed separately. Swing remained a separate set of classes through much of the JDK 1.2 beta process. Now, however, Swing is fully integrated into JDK 1.2. Be sure to get the most up-to-date version of Swing by downloading the final version of Java 2/JDK 1.2 at http://java.sun.com/products/jdk/1.2/.
Swing Component APIsWith the AWT you can choose buttons, labels, lists, and a few other user interface components. With the Swing components, Sun provides a richer interface, more akin to the full range available in Microsoft Foundation Classes or the Macintosh Toolbox. More than twice as many Swing components are available as AWT components. The Swing components include
All these components are lightweightinstead of building a peer component from the native operating system, they look for a library of pluggable look and feel classes. Three such libraries come with Swing 1.0:
Most of Suns demo applets enable you to change the look and feel from a control at runtime. Figure 38.16 shows Simple, one of the examples, in the default look and feel (called Metal).
Compare Figure 38.16 with Figure 38.17. Figure 38.16 shows Simple with the Metal look and feel. By choosing the Windows radio button, you can switch Simple to the Windows look and feel, shown in Figure 38.17. Similarly, you can use the Motif look and feel, shown in Figure 38.18.
Using the TPanelTester ApplicationThe examples in this section are based on subclasses of the Swing panel component, JPanel. To run the various panels, you need an application to display the panel. Listing 38.7 shows a generic panel tester. Listing 38.7 TPanelTester.javaChange the Name in This Panel Tester to Match the Panel Under Test import javax.swing.*; public class TPanelTester extends JFrame { public TPanelTester() { super(Panel Tester); // change TLabelPanel to match the name of the panel under test JPanel thePanelUnderTest = new TLabelPanel(); setContentPane(thePanelUnderTest); } public static void main(String[] args) { JFrame theFrame = new TPanelTester(); theFrame.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) {System.exit(0);} }); theFrame.pack(); theFrame.setVisible(true); } } Using JPanelWe first looked at panels earlier in this chapter in our discussion of the AWT. A JPanel is a lightweight version of panel; it is used in most of the examples in this section.
Working with IconsIn our discussion of the AWT, we noted that subclasses of java.awt.Container can contain java.awt.Components, and that Containers themselves are Components. It is this hierarchy that enables us to add Panels to Frames, for example. All the Swing components are derived from JComponent, which is a java.awt.Container. This design means that every JComponent can contain other components, either AWT or Swing. Therefore, you can add a graphical icon to a JButton, a JLabel, or other Swing component. Swing provides Icon as an interface; to implement it you must provide a paintIcon() method, a getIconWidth() method, and a getIconHeight() method. The paintIcon() method is paintIcon(Component c, Graphics g, int x, int y);
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |