|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
The first difference youll notice between HelloPlus and HelloApplication is that weve added several data members to the class: fButton, fTextField, and so forth. Often youll need to refer back to user-interface components during the life of the programits convenient to have all the components available as class members. (If you dont want to carry these references around in the class, you dont have totheChoice is an example of a user interface component that is not referenced as an instance variable.) The biggest area of change is in init(). Weve used init() to build the user interfacethat way, the components are added to the applet when were running as an applet, and the application frame when were running as an application. Understanding fTextField and fLabelForTextField We start init() by adding our ubiquitous Hello, World! label. Then we instantiate a new TextField and add it to the interface. We follow this with a new Label, likewise added to the interface. Now go down to lines 72 through 77: fTextField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fLabelForTextField.setText(Your text is + fTextField.getText()); } }); Heres a construct we havent seen before. Because we added a TextField, were able to get a semantic eventthe ActionEventwhich has only one method: actionPerformed(). Although the action differs from one component to the next, the method for listening to these events is the same. We dont need an adapter because only one method is in the event. Instead, we declare a new ActionListener and define it right here inside addActionListener(). In this case, the body of the method is quite simple: fLabelForTextField.setText(Your text is + fTextField.getText()); When you change the text in the text field and press Enter, the Java runtime sends an ActionEvent to your listening program, which changes the text in fLabelForTextField.
Understanding fButton This version of the program starts with a red background on the frame and a white button labeled White. When you click the button, it sends an ActionEvent to the listening program. The definition of the listener starts at line 43 of Listing 38.3: fButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if (fButton.getLabel() == White) { setBackground(java.awt.Color.white); fButton.setLabel(Red); } else { setBackground(java.awt.Color.red); fButton.setLabel(White); } Component theComponents[] = getComponents(); try { if (theComponents.length == 0) throw (new AWTException(Cannot find the components)); } catch (AWTException theException) { System.err.println(Exception: + theException.getMessage()); theException.printStackTrace(); System.exit(1); } for (short theIndex = 0; theIndex < theComponents.length; theIndex++) getComponent(theIndex).setBackground(getBackground()); if (fButton.getLabel() == White) fButton.setBackground(java.awt.Color.white); else fButton.setBackground(java.awt.Color.red); } }); We handle the button click in three steps:
Understanding fCheckbox and fDialog The Checkbox, named fCheckbox, is added in the same way as the other components weve seen, but then we do something differentwe make a new Dialog. Our plan is to have a modal dialog box come up whenever the end user checks the Checkbox, and we have to go through a couple of extra steps to make this work.
If you examine the documentation for class Dialog, youll see that all its constructors need an ownera Frame or another Dialog that serves as the parent of the new Dialog in the Window hierarchy. If youre writing a pure application, you can use the applications Frame as the owner of the Dialog. In this case, we can be called as either an applet or an application. An Applet is a Panel, not a Frame, so we have to start up our own Frame to serve as the parent. At the bottom of the HelloPlus.java listing youll see a definition of class OKDialog. After the constructor (which is private) youll see a factory method: makeDialog(): static public OKDialog makeDialog(String theMessage) { if (fFrame == null) fFrame = new Frame(); OKDialog theResult = new OKDialog(fFrame, theMessage); fFrame.setSize(theResult.getSize().width, theResult.getSize().height); return theResult; } } // end OKDialog class
The class OKDialog has a static Frame member fFrame. When makeDialog() is first called, it instantiates a new Frame and assigns it to fFrame. Next, makeDialog() instantiates a new OKDialog, using fFrame as the parent. It scales the Frame to the same size as the OKDialog and returns a reference to the OKDialog for use by the calling class.
|
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. |