|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
When the program runs, youll see a single text field thats initialized with the value of Hello TextField!. If you click inside the text field with the mouse, youll see a message that says focus was gained. If you click outside the text field with the mouse, youll see a message that says focus was lost. If you edit the text field, youll see each figure you type appear in the display string thats telling you which actions are being handled. If you press the Enter key, youll see that an action event was triggered. Figure 7.6 shows the program running within Internet Explorer.
This time, Ill go in sequential order through the source code instead of skipping to a point halfway down the example. Looking at line 9, you can see that this is where the extended TextField class was created. Notice that the constructor takes a single string argument. This is the only constructor weve implemented. At line 13, the TextField object is added to the applet. Line 21 is where the extended class begins. Notice that at line 24, the one and only constructor begins. Pay attention to line 27. Its at this line where we enable the events. Notice that were enabling three separate event types: action events, focus events, and key events. At line 31, the method that handles key events can be seen. Its the processKeyEvent() method. At line 41, the method that handles action events, the processActionEvent() method, can be seen. Lastly, at line 48, the method that handles focus events can be seenthe processFocusEvent() method. The TextArea ControlThe TextArea control can contain multiple lines of text. Like the TextField control, TextArea controls can be created empty or with an initial string. TextArea controls can be defined with an initial number of rows and columns. The TextArea control inherits most of its functionality from the Text control. ConstructorsThere are five constructors for creating TextArea controls. The first constructor takes no arguments and creates an empty TextArea control. The following example shows how to do this: TextArea text = new TextArea(); The second constructor lets you create a text area and specify the number of rows and columns. It takes two integer arguments, the first one being the number of rows you want, and the second one being the number of columns you want. The following example lets you create a text field of 25 rows and 45 columns: TextArea text = new TextArea( 25, 45 ); The third constructor takes a single argument. That argument is a string. When created, this string will be displayed in the text area. The following example lets you create a text area with a string in it: TextArea text = new TextArea( "Hello there TextArea!" ); The fourth constructor takes three arguments. The first argument is the string that will be initially placed in the text area. The second argument is the number of rows you want, and the third argument is the number of columns you want the text area to have. The next example shows how to create a TextArea object with a string, 25 rows and 45 columns: TextArea text = new TextArea( "Hello there TextArea!", 25, 45 ); The fifth and final constructor is almost identical to the preceding one we discussed, except that it adds a fourth argument that determines how the text areas scrollbars are managed. Table 7.1 shows the available values. Following is an example of the final constructor: TextArea text = new TextArea( "Hello there TextArea!", 25, 45, TextArea.SCROLLBARS_NONE );
EventsTextAreas do not generate action events. They do, however, generate keyboard, focus, and text events. These events are handled exactly in the same way as TextField events. The Label ControlThe most basic user interface control is the Label. The Label is a text string that is usually used to indicate the name of other user-interface components. Label components can be assigned an arbitrary text string and alignment attribute. After a label has been created, the text string and alignment can be queried and modified. ConstructorsThree constructors are available when you create Label controls. The first constructor takes no arguments. The next example shows how to create a label using this constructor: Label label = new Label(); Normally, though, when you create a label, you tell it what text to use. The second label constructor takes a single argument, which is a string. This is the string that will be displayed in the label. The next example shows how to create a label with the text Hello Label: Label label = new Label( "Hello Label" ); The last way to create a Label control is with a constructor that takes two arguments. The first argument is a string that determines the text that will be in the label, and the second argument is the alignment. Three alignment types are available: label.LEFT, label.CENTER, and label.RIGHT. The following example shows how to create a label with the text Hello Label and an alignment of LEFT: Label label = new Label( "Hello Label", Label.LEFT );
|
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. |