|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
EventsMouse and keyboard events are generated for labels, but no selection or action events are. Its rare that youll need to handle events from labels. Java 1.0 did not even have this capability. Sample ProgramThis section just presents a simple program that displays four labels, all with different alignments. The source code is given in Listing 7.11. Listing 7.11 This Simple Program Shows Simple Label Alignments 1 import java.awt.*; 2 import java.applet.*; 3 4 public class Applet1 extends Applet 5 { 6 7 public void init() 8 { 9 Label label1, label2, label3, label4; 10 label1 = new Label( " Label 1 " ); 11 label2 = new Label( " Label 2 ", Label.LEFT ); 12 label3 = new Label( " Label 3 ", Label.RIGHT ); 13 label4 = new Label( " Label 4 ", Label.CENTER ); 14 15 Font font = new Font( "Helvetica", Font.BOLD, 25 ); 16 label2.setFont( font ); 17 label3.setFont( font ); 18 label4.setFont( font ); 19 20 add( label1 ); 21 add( label2 ); 22 add( label3 ); 23 add( label4 ); 24 25 } 26 27 } When the program runs, youll see four labels in the applet window, as shown in Figure 7.7. The first one uses the default text and the default alignment. The next three use a larger font with a left alignment, right alignment, and center alignment.
SummaryJava provides the functionality required to implement cross-platform Java applets with sophisticated and interactive user interfaces. The Advanced Windowing Toolkit contains many interface components. These can be placed within containers via the add() method. A great deal of your Java programming will contain and include these Java interface components. Q&AQ How many ways are there to handle Button events? A There are two ways. One approach is to extend the Button class, call the enableEvents() method, and add a processActionEvent() method to the class for handling the events. Another approach is to create a class that implements the ActionListener interface. Using the addActionListener() method to add an instantiation of the newly created class that implements the ActionListener interface will then cause the new classs actionPerformed() method to get called whenever an action event is triggered. Q How can you use check boxes without handling Checkbox events? A Anytime you want to get the state of a check box, simply call the getState() method. If it returns true, the check box is selected; false, the check box is not selected. Q How many constructors does the Choice control have? A One. The Choice controls only constructor takes no arguments. To add selectable items to a Choice control, you have to use the addItem() method. Q How do you make check boxes act like radio buttons such that only one simultaneous selection in the group is allowed? A You must first create a CheckboxGroup control. Then when you create the Checkbox controls, use the constructor that accepts a CheckboxGroup object as an argument. All the check boxes that are placed into this CheckboxGroup will be grouped together in a radio-button family. Q Whats the different between an Edit control and a TextArea control? A An Edit control contains a single line of editable text. A TextArea control contains multiple lines of text. Q When would you want to not handle events for user-interface controls? A When you have no action that must be performed during an event. A good example of this is a Checkbox control. You might only need to know what the user has selected when you go to perform a task. In this case, you can simply use the getState() method to find out whether the check box is selected or deselected. This approach requires far less code than is required to handle Checkbox events. On the other hand, if you need to take some action when the user clicks on a check box, youll have to handle the Checkbox events. Q What happens if you dont use the add() method to add these controls to the applets window? A Nothing. The controls will not be displayed to users, and they wont have the ability to interact with the control. Q Can I write my own custom controls and use them rather than the controls discussed in this chapter? A Sure, if you want to work harder than you have to and not provide cross-platform look and feel. First of all, its hard to write a control. Second, the beauty of these controls is that they fit the platform on which theyre running. Exercises
|
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. |