|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Listing 38.5 OKDialogPop Up a Simple Dialog Box with an OK Button import java.awt.*; public class OKDialog extends Dialog { protected Button fOKButton; protected static Frame fFrame; public OKDialog( Frame theParent, String theMessage ) { super( theParent, true ); // Call the parents constructor // This Dialog box uses the GridBagLayout. GridBagLayout theGridBagLayout = new GridBagLayout(); GridBagConstraints theConstraints = new GridBagConstraints(); // Make the OK button and the message to display fOKButton = new Button( OK ); Label aMessageLabel = new Label( theMessage ); setLayout( theGridBagLayout ); // The message should not fill, it should be centered within this area, with // some extra padding. The gridwidth of REMAINDER means this is the only // thing on its row, and the gridheight of RELATIVE means there should only // be one thing below it. theConstraints.fill = GridBagConstraints.NONE; theConstraints.anchor = GridBagConstraints.CENTER; theConstraints.ipadx = 20; theConstraints.ipady = 20; theConstraints.weightx = 1.0; theConstraints.weighty = 1.0; theConstraints.gridwidth = GridBagConstraints.REMAINDER; theConstraints.gridheight = GridBagConstraints.RELATIVE; theGridBagLayout.setConstraints( aMessageLabel, theConstraints ); add( aMessageLabel ); // The button has no padding, no weight, takes up minimal width, and // is the last thing in its column. theConstraints.ipadx = 0; theConstraints.ipady = 0; theConstraints.weightx = 0.0; theConstraints.weighty = 0.0; theConstraints.gridwidth = 1; theConstraints.gridheight = GridBagConstraints.REMAINDER; theGridBagLayout.setConstraints( fOKButton, theConstraints ); add( fOKButton ); // Pack is a special window method that makes the window take up the minimum // space necessary to contain its components. pack(); } // The action method just waits for the OK button to be clicked; // when it is it hides the dialog, causing the show() method to return // back to whoever activated this dialog. public boolean action( Event theEvent, Object whichAction ) { if ( theEvent.target == fOKButton ) { setVisible( false ); if ( fFrame != null ) fFrame.setVisible( false ); } return( true ); } // Shortcut to make a frame automatically public static void makeOKDialog( String theDialogString ) { if ( fFrame == null ) fFrame = new Frame( Dialog ); OKDialog theOKDialog = new OKDialog( fFrame, theDialogString ); // Shrink the frame to just fit the dialog fFrame.setSize( theOKDialog.getSize().width, theOKDialog.getSize().height ); theOKDialog.show(); } } The DialogApplet in Listing 38.6 pops up an OKDialog whenever a button is pressed. Listing 38.6 DialogApplet.javaTest Your New OKDialog with This Applet import java.awt.*; import java.awt.event.*; import java.applet.*; public class DialogApplet extends Applet { protected Button fLaunchButton; public void init() { fLaunchButton = new Button( Show OK Dialog ); add( fLaunchButton ); fLaunchButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { doLaunchClicked(); } } ); } public void doLaunchClicked() { OKDialog.makeOKDialog( Press OK to dismiss this dialog ); } } Figure 38.14 shows the DialogApplet with the OK dialog popped up.
The Swing ArchitectureWhen Java was first introduced, the only graphical user interface (GUI) available was the Abstract Windowing Toolkit (AWT). Recall from the first section of this chapter, Interacting with People by Using Java, that AWT components are considered to be heavyweight in that each Java component has a peer object from the native GUI. Sun also offers lightweight components that dont have a peer object. Instead, they offer pluggable look and feel. If the end user is running on a Windows 95 machine but prefers the look and feel of Suns Motif interface, for example, the user can select that interface in the running application. When Sun developed these lightweight components, it did so as part of a project called Swing, so the lightweight user interface components are usually called Swing components. Swing components are one part of the Java Foundation Classes (JFC). Figure 38.15 illustrates the relationship of the AWT and the Swing components to the JFC.
Understanding the JFCAlthough the JFC was released well before JDK 1.2, Sun didnt make a big deal about it. Most of Suns description of the new JFC has centered on Swing, so many people have the idea that Swing is all there is to the JFC and that the JFC came out as part of JDK 1.2. The link is so strong that Sun unofficially calls JFC 1.1 Swing 1.0. In fact, the JFC includes
A Short Tour of SwingSun supplies Swing in 14 packages:
|
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. |