|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
After youve added your Button control to the form, clicked the form, and opened it up again, youll see that four lines of code were added to initialize the Button control. These four lines of code that were added to my control are shown in the next example: button1.setLocation(new Point(32, 24)); button1.setSize(new Point(72, 24)); button1.setTabIndex(0); button1.setText("Button One"); The Button class is extended from the Control class. It inherits all the methods and fields of the Control class. One very interesting method, though, that you might end up using in the Button class is the performClick() method. This generates a click event for the button simulating a click by a user. The event list for a button contains the event handlers you would expect: click, mouse move, mouse enter, mouse leave, and so forth. However, the one youll use most often is the click event. Ive created a program that has two buttons. Above both buttons is a label. For each button, Ive added a click event handler. If you remember, its easy to do this. All you do is select your Button control on the form, causing the Properties window to change to reflect the properties for that window; click the events button in the Properties window; and then double-click on the click event. This automatically adds source code to the click event handler. After I did this, in each click handler, I added text that changed the label so that it showed which button had just been pressed. The complete source code for this program is given in Listing 8.1. Listing 8.1 This Program has Two Buttons, Each of Which has a Click Event Handler 1 import com.ms.wfc.app.*; 2 import com.ms.wfc.core.*; 3 import com.ms.wfc.ui.*; 4 import com.ms.wfc.html.*; 5 6 /** 7 * This class can take a variable number of parameters on the command 8 * line. Program execution begins with the main() method. The class 9 * constructor is not invoked unless an object of type 'Form1' is 10 * created in the main() method. 11 */ 12 public class Form1 extends Form 13 { 14 String m_strText = " "; 15 16 public Form1() 17 { 18 // Required for Visual J++ Form Designer support 19 initForm(); 20 21 // TODO: Add any constructor code after initForm call 22 } 23 24 /** 25 * Form1 overrides dispose so it can clean up the 26 * component list. 27 */ 28 public void dispose() 29 { 30 super.dispose(); 31 components.dispose(); 32 } 33 34 private void Form1_click(Object source, Event e) 35 { 36 37 } 38 39 private void button1_click(Object source, Event e) 40 { 41 label1.setText( "Button One has been pressed." ); 42 } 43 44 private void button2_click(Object source, Event e) 45 { 46 label1.setText( "Button Two has been pressed." ); 47 } 48 49 /** 50 * NOTE: The following code is required by the Visual J++ form 51 * designer. It can be modified using the form editor. Do not 52 * modify it using the code editor. 53 */ 54 Container components = new Container(); 55 Button button1 = new Button(); 56 Button button2 = new Button(); 57 Label label1 = new Label(); 58 59 private void initForm() 60 { 61 this.setText("Form1"); 62 this.setAutoScaleBaseSize(13); 63 this.setClientSize(new Point(292, 273)); 64 this.addOnClick(new EventHandler(this.Form1_click)); 65 66 button1.setLocation(new Point(32, 96)); 67 button1.setSize(new Point(72, 24)); 68 button1.setTabIndex(0); 69 button1.setText("Button One"); 70 button1.addOnClick(new EventHandler(this.button1_click)); 71 72 button2.setLocation(new Point(128, 96)); 73 button2.setSize(new Point(72, 24)); 74 button2.setTabIndex(1); 75 button2.setText("Button Two"); 76 button2.addOnClick(new EventHandler(this.button2_click)); 77 78 label1.setLocation(new Point(48, 24)); 79 label1.setSize(new Point(144, 24)); 80 label1.setTabIndex(2); 81 label1.setTabStop(false); 82 label1.setText("No button has been pressed."); 83 label1.setTextAlign(HorizontalAlignment.CENTER); 84 85 this.setNewControls(new Control[] { 86 label1, 87 button2, 88 button1}); 89 } 90 91 /** 92 * The main entry point for the application. 93 * 94 * @param args Array of parameters passed to the application 95 * via the command line. 96 */ 97 public static void main(String args[]) 98 { 99 Application.run(new Form1()); 100 } 101 } When the program runs, youll see a label that says, No button has been pressed. Two buttons appear below that. If you press Button One, the label changes to say, Button One has been pressed. If you press Button Two, the label changes to say, Button Two has been pressed. You can see this program running in Figure 8.2.
Notice the button1_click() method at line 39. It doesnt do much; it simply sets the label text so that the label says, Button One has been pressed. At line 44, the button2_click() method can be found. This method also doesnt do very much except to set the label text so that users can see that Button Two has been pressed. At line 66, you can see that the default methods are inserted so that the button will be minimally functional. At line 72, the methods needed so that the second button will be functional are added. Lastly, at line 78, you can see that the methods needed to make the Label control functional have been added. CheckBox ControlA CheckBox control is encapsulated in a CheckBox class. These check boxes are labeled boxes that are selected or unselected. The check box icon is shown as it appears in the Toolbox:
|
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. |