|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Listing 9.2 The Source Code for the About Form 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 'About' is 10 * created in the main() method. 11 */ 12 public class About extends Form 13 { 14 public About() 15 { 16 super(); 17 18 // Required for Visual J++ Form Designer support 19 initForm(); 20 21 } 22 23 /** 24 * About overrides dispose so it can clean up the 25 * component list. 26 */ 27 public void dispose() 28 { 29 super.dispose(); 30 components.dispose(); 31 } 32 33 /** 34 * NOTE: The following code is required by the Visual J++ form 35 * designer. It can be modified using the form editor. Do not 36 * modify it using the code editor. 37 */ 38 Container components = new Container(); 39 Label label1 = new Label(); 40 Button button1 = new Button(); 41 42 private void initForm() 43 { 44 this.setText("About"); 45 this.setAutoScaleBaseSize(13); 46 this.setClientSize(new Point(305, 88)); 47 48 label1.setFont(new Font("MS Sans Serif", 12.0f)); 49 label1.setLocation(new Point(8, 8)); 50 label1.setSize(new Point(280, 24)); 51 label1.setTabIndex(0); 52 label1.setTabStop(false); 53 label1.setText("Hello Dialog World!"); 54 label1.setTextAlign(HorizontalAlignment.CENTER); 55 56 button1.setLocation(new Point(88, 48)); 57 button1.setSize(new Point(128, 24)); 58 button1.setTabIndex(1); 59 button1.setText("OK"); 60 61 this.setNewControls(new Control[] { 62 button1, 63 label1}); 64 } 65 66 /** 67 * The main entry point for the application. 68 * 69 * @param args Array of parameters passed to the application 70 * via the command line. 71 */ 72 public static void main(String args[]) 73 { 74 Application.run(new About()); 75 } 76 } When the program runs, youll see the blank Form window. When you click in the Form window, the About dialog appears, as shown in Figure 9.3. (Notice that the minimize and maximize buttons have been removed.)
You should note a few important things in the source code of Listings 9.1 and 9.2. First and foremost, notice how similar they are. The Form1 and About classes are both extended from the Form class. The Form1 class has a Form1_click() method. (Remember, this is a convenient way to get a place to test things in your program.) In this method, in lines 3135 of Listing 9.1, you can see that an About class was created, and then a call to its showDialog() method was made. Where the About class is different is in the controls that were added to it. Notice that at lines 39 and 40 of Listing 9.2, there are declarations for a Label and a Button class. These objects are set up in the initForm() method. The nondefault properties for the label1 object are set in lines 4854. In lines 5659 you can see that the nondefault properties are set for the button1 object. Handling Dialog Button EventsSo you have a program that displays a dialog. Now you need to handle the events. One event is already handled for youthe close event. When users click the close button in the upper-right corner of the dialog, it closes and returns control to your program. Lets take the current program with the About dialog. Well make the OK button the Accept button. In Visual J++, the Accept button is the one that users click to accept the values in a dialog. These buttons are usually labeled with OK. Also in Visual J++, the Cancel button is the one that users click to cancel a dialog without changing any values. These buttons are usually labeled with Cancel. There are two steps to doing this. First, go to the About Forms properties and find the acceptButton property. Edit the property so that the button you want for your accept button is selected. Next, go to the OK buttons properties. Find the dialogResult property and select OK. Now, when you run your program and bring up the About dialog, clicking the OK button will cause the dialog to disappear. I hope youre doing these steps as I describe them. If you are, youll see the following code, which sets the properties for the Button and the About form in the initForm() method: button1.setLocation(new Point(88, 48)); button1.setSize(new Point(128, 24)); button1.setTabIndex(1); button1.setText("OK"); button1.setDialogResult(DialogResult.OK); this.setText("About"); this.setAcceptButton(button1); this.setAutoScaleBaseSize(13); this.setClientSize(new Point(305, 88)); this.setMaximizeBox(false); this.setMinimizeBox(false); So how is the calling code supposed to know the difference? Did the user click the close button or the OK button? Note that the Buttons properties have set the dialogResult property to DialogResult.OK. Also bear in mind that the close button has its own dialogResult property of DialogResult.CANCEL. The calling code can use the getDialogResult() method to obtain the result code that was generated by the user. The following code shows how to handle the two possibilities in our program: About dlg = new About(); dlg.showDialog(); int nResult = dlg.getDialogResult(); if( nResult == DialogResult.OK ) { // We got an OK signal. } else if( nResult == DialogResult.CANCEL ) { // We got a CANCEL signal. }
|
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. |