Sunteți pe pagina 1din 22

How to use NetBeans for developing GUI applications

How to use NetBeans for developing GUI applications


The NetBeans IDE provides many excellent features that professional Java programmers have come to rely on, including an excellent tool for building graphical user interfaces (GUIs). This GUI builder is known as the Matisse GUI builder, and it is widely considered to be the leading tool for building GUIs using Swing components. This tutorial presents all the skills you need for using the NetBeans GUI builder to develop the GUI application presented in chapter 15 of Murachs Java SE 6. Once you learn those skills, you should be able to apply them as you develop the applications presented in chapters 16 through 17 of our book. Please note, however, that this tutorial assumes that you have already read our basic NetBeans tutorial, How to use NetBeans with Murachs Java SE 6, as well as chapter 15 of our Java book.
How to develop a form ........................................................... 2
How to create a project for a GUI application ............................................... 2 How to add a form to a project ....................................................................... 2 How to add controls to the form ..................................................................... 4 How to set properties ...................................................................................... 6 Common properties for forms and controls ................................................... 8

How to add code to a form .................................................. 10


How to set the variable name for a control ................................................... 10 How to create an event handler for a control ................................................ 12 How to rename or delete an event handler ................................................... 14 How to enter the code for a form .................................................................. 16

How to run and debug a form .............................................. 18


How to run a form ........................................................................................ 18 How to debug a form .................................................................................... 18

Perspective ............................................................................ 20

MIKE MURACH & ASSOCIATES, INC.


1-800-221-5528 (559) 440-9071 Fax: (559) 440-0963 murachbooks@murach.com www.murach.com
Copyright 2007 Mike Murach & Associates. All rights reserved.

How to use NetBeans for developing GUI applications

How to develop a form


If you read our basic tutorial for working with NetBeans, you already know how to create a project and add packages to a project. Once you do that, you can use NetBeans to develop a form. A form is the window that holds the controls that make up the graphical user interface. In this topic, youll learn how to use NetBeans to develop the form for the Future Value application thats presented in chapter 15 of Murachs Java SE 6.

How to create a project for a GUI application


When you create a project for a GUI application, you should deselect the Create Main Class check box so a main class wont be automatically generated. Instead, the class for the first form of your application is going to be used as the main class.

How to add a form to a project


Before you add a form to a project, you should create the package where you want to store the form. In this figure, for example, I created a package named murach.ui (ui stands for user interface) to store the form. Then, I rightclicked on this package and selected the New JFrame Form command to display the dialog box shown in figure 1. Since this automatically enters the murach.ui as package for the form, I only needed to enter the name for the form. In this figure, I entered FutureValueFrame. For most GUI applications, you base the form on the JFrame class. For example, the forms presented in chapters 15 through 17 of Murachs Java SE 6 are all based on the JFrame class. However, its also possible to base a form on other classes. If, for example, if you want to create an applet, you can base the form on the JApplet class as described in chapter 18 of our book. To do that, you can right-click on the package, and select the New JApplet Form command. The JFrame class is a Swing component thats available from the javax.swing package. As a result, when you base a form on the JFrame class, you are using Swing to develop your GUI. Since the NetBeans GUI builder is designed to work with Swing components, you may have to use a different GUI builder if you need to work with a GUI component library like SWT.

How to use NetBeans for developing GUI applications

The New JFrame Form dialog box

Description
For a GUI application, you need to start a new project without a main class. Then, you can use the first form of the application as the main class. To add a form to a project, right-click on the package you want to add the form to and select the New JFrame Form command. Then, enter a name for the class.

Figure 1

How to add a form to a project

How to use NetBeans for developing GUI applications

How to add controls to the form


When you add a form to a project, NetBeans displays the form in Design view. When a form is displayed in this view, the Design button is selected and the Palette window is displayed on the right side of the NetBeans window. When youre working with forms, you can usually open a form in Design view by double-clicking on its .java file in the Projects window. Figure 2 shows how you can use the Palette window to add controls to the form. The easiest way to do that is to click on the control in the Palette window and then click on the form where you want to add the control. In this figure, for example, the JButton control is selected in the Palette window and the user just clicked the mouse pointer to add the second button to the form. Once you add a control to a form, you can resize the control by clicking on the control to display its sizing handles and then dragging one of the handles. Similarly, you can move a control by dragging the control to a new location on the form. As you work with the controls on the form, youll find that alignment guides will appear and attempt to help you align controls with the other controls on the form. In this figure, for example, a horizontal guide is attempting to align the second button with the first button. In addition, a vertical guide is attempting to align the right side of the second button with the right side of the four text boxes. Most of the time, these guides are helpful and make it easier to develop forms that are visually pleasing. Other times, these guides can make it difficult to place controls where you want them to be placed. However, with a little experimentation, you should figure out how best to work with these guides. If you want to change the size of the form, you can drag its edges. In this figure, for example, the form is too tall and too wide for the controls. As a result, it makes sense to drag the bottom and right sides of the form to make it smaller. If you need to work with several controls at the same time, you can select multiple controls by holding down the Ctrl key as you click on the controls. Or, you can click on a blank spot in the form and drag an outline around the controls you want to select. Then, you can work with the selected controls. For example, you can align a group of selected controls by clicking one of the alignment buttons in the toolbar for the forms window.

How to use NetBeans for developing GUI applications

A form after some controls have been added to it

Description
To open a form, use the Projects window to navigate to the form. Then, double-click on the .java file for the form. In Design view, NetBeans shows a graphical representation of the form. In Source view, NetBeans shows the source code for the form. To switch between Design view and Source view, click on the Design and Source buttons that are above the form window. To add a control to a form, select the control in the Palette window and click on the form. If the Palette window isnt displayed, you can display it by selecting the Palette command from the Window menu. To select a control, click on it. To move a control, drag it. To size a control, click on it and drag one of its handles. To change the size of the form, drag its edges. To select more than one control, hold down the Ctrl key as you click on the controls. Or, click on a blank spot in the form and drag an outline around the controls. To move a group of controls, drag one of them. To align a group of controls, click one of the alignment buttons that are to the right of the Source and Design buttons.

Figure 2

How to add controls to a form

How to use NetBeans for developing GUI applications

How to set properties


After you have placed controls on a form, you need to set each controls properties. These are the values that determine how the controls will look and work when the form is displayed. In addition, you need to set some of the properties for the form itself. The text property for a control determines what is displayed in or on the control. In figure 3, for example, the text properties of the two buttons have been changed from their default values to Calculate and Exit. The easiest way to set the text property for a control is to double-click on it in Design view. Then, you can edit the text property right on the control. Since you need to set the text property for most controls, you can use this technique to set the text property for all of the controls before you begin using the Properties window to set any other properties. To set the other properties of a form or control, you can work with the Properties window as shown in this figure. To display the properties for a specific control, display the form in Design view and select the control by clicking it. To display the properties for the form, click any blank area of the form. In the Properties window, you can select a property by clicking it. When you do, a brief description of that property is given at the bottom of the Properties window. Then, to change a property setting, you can sometimes edit the entry to the right of the property name by typing a new value or by using a combo box or check box control to alter the property. Other times, you need to click on the ellipsis () button thats displayed to the right of the property to display a dialog box that can be used to edit the property. As you work with properties, youll find that most are set the way you want them by default. In addition, some properties are set interactively as you size and position the form and its controls in Design view. As a result, you usually need to change just a few properties for each control. When you change a property from the default value, the property is displayed in bold in the Properties window. As a result, you can easily identify all properties that youve changed. In this figure, for example, the mnemonic property and the text property for the Exit button have been changed from their defaults and are displayed in bold.

How to use NetBeans for developing GUI applications

A form after the properties have been set

Description
To change the text thats displayed on a control, double-click on the control and enter or edit the text for the control. Or, right-click on the control and select Edit Text. To set a property, select a control and use the Properties window to change the property. To sort the properties for a control by category or name, right-click in the Properties window and select the appropriate command. To search for a specific property when the focus is in the Properties window, type the starting letter or letters of the property. This starts the Quick Search feature. To set a property for more than one control at the same time, select the controls and use the Properties window to change the property. The properties that have been changed from their default values are displayed in bold. A description of the currently selected property is normally displayed at the bottom of the properties window. If this description isnt shown, right-click in the Properties window and select Show Description area.

Figure 3

How to set properties

How to use NetBeans for developing GUI applications

Common properties for forms and controls


Figure 4 shows some common properties for forms and controls. Note that some of these properties only apply to certain types of controls. Thats because different types of controls have different properties. For example, the mnemonic property is available for buttons, but not for labels or text boxes. For most controls, you use the text property to specify the text thats displayed on the control. However, this property works a little differently for each type of control. For a label, this property sets the text thats displayed by the label. For a text box, this property sets the text thats displayed within the text box. For a button, this property sets the text thats displayed on the button. And so on. For simple applications, you only need to set a few properties for each control. To learn more about the properties that are available for a control, select the control and use the Properties window to review its properties.

How to use NetBeans for developing GUI applications

Common properties for forms


Property
title defaultCloseOperation

Description
Sets the text thats displayed in the title bar for the frame. Sets the action thats performed when the user clicks on the Close button in the upper right corner of the frame. The default value, EXIT_ON_CLOSE, causes the application to exit. Determines whether the user can resize the frame by dragging its edges.

resizable

Common properties for controls


Property
text editable enabled focusable horizontalAlignment mnemonic

Description
Sets the text thats displayed on the control. Typically used for text boxes and other controls that contain text, this property determines whether the user can edit the text thats stored in the control. This property determines whether a control is enabled or disabled. Determines whether the control accepts the focus when the user presses the Tab key to move the focus through the controls on the form. This property determines how the text on a button or in a text box is aligned: left, right, center, trailing, or leading. Typically used for buttons, this property specifies an access character that allows the user to quickly access the control by holding down the Alt key and pressing the specified character. The width and height in pixels for the control.

preferredSize

Description
To learn about the properties that are available for a control, select the control, use the Properties window to scroll through its properties, and read the descriptions for each property.

Figure 4

Common properties for forms and controls

10

How to use NetBeans for developing GUI applications

How to add code to a form


In the last few figures, you learned how to add controls to a form and how to set their properties. As a result, if you run the form, it will look the way youve designed it. However, the form wont work correctly when you interact with it until you add the required Java code to the form.

How to set the variable name for a control


When you add a control to a form, NetBeans creates a generic variable name for each of the controls that you add to the form. For example, it uses JTextField1 for the first JTextField control added to the form, JTextField2 for the second JTextField control added to the form, and so on. Before you write code that uses any of the controls on a form, though, you should change these generic names to meaningful names that are easier to remember and use. As figure 5 shows, the easiest way to set the variable name for a control is to right-click on it in Design View, select the Change Variable Name command, and edit the default name. You may want to do this for all of the controls right after you set the text property for all of the controls. Another way to set the variable name for a control is to select the control in Design view, click on the Code button in the Properties window to access the properties that affect code generation, and edit the default value for the Variable Name property. In this figure, for example, I changed the default value of JButton2 to exitButton. Once you set the variable names for the controls that youre going to refer to in your code, youre ready to start writing the code that will make your form work the way you want it to.

How to use NetBeans for developing GUI applications

11

A form after the variable names have been set for the controls

Typical variable names for controls


paymentTextField calculateButton messageLabel

Description
When you add controls to a form, default variable names are given to the controls. If youre going to refer to these controls in your Java code, though, you should change these names so theyre easier to remember and use. To change the name of the variable thats used for a control, right-click on the control and select Change Variable Name. Or, click on the control, click on the Code button in the Properties window, and change the Variable Name property.

Figure 5

How to set the variable name for a control

12

How to use NetBeans for developing GUI applications

How to create an event handler for a control


An event handler is a special type of method that responds to an event thats triggered when a user interacts with a form. For example, the most common type of event handler is a method thats executed when a user clicks on a button. For an event handler to work, it must be connected, or wired, to the event. This is known as the event wiring, and its generated automatically when you use NetBeans to generate an event handler as described in figure 6. The most common event that youll write code for is the actionPerformed event. This event is a high-level event thats raised when a user clicks the button with the mouse or when a user activates the button using keystrokes. However, there are dozens of other events for each control, including low-level events like the focusGained and focusLost events that occur when the focus is moved to or from a control. As you will see, you can use the same general techniques to work with all types of events. To create an event handler for a control, you begin by displaying the form in Design view. Then, you can right-click on the control, select Events, and find the event you want in the submenus. Or, you can click on the control, click on the Events button in the Properties window, click to the right of the event that you want to handle, and press the Enter key. Either way, NetBeans generates a default name for the method that will handle the event, generates the code for the event handler and its wiring, and switches to Source view. In this figure, for example, NetBeans has generated the name exitButtonActionPerformed for the event handler that will handle the actionPerformed event of the Exit button. At this point, you just need to enter the body of the code for that event handler. If you dont want to use the generated name for an event handler, you can change it in the Properties window before you press the Enter key. If, for example, you want to create an event handler named textBoxFocusGained for the focusGained event of a text box, you can enter textBoxFocusGained to the right of the focusGained event and press the Enter key. Later, if you want to wire another text box to the event handler named textBoxFocusGained, you can use the Properties window to enter textBoxFocusGained to the right of the focusGained event for that text box. That way, the same event handler will be used for two different events. Below the screen in this figure, you can see the code for the event handler thats generated for the Exit button on the Future Value form. Here, the name of the method is the name of the control (exitButton) followed by the name of the event (ActionPerformed). This shows that its important to set the variable name for the control before you start an event handler for a control. That way, the variable name will be reflected in the method name of the event handler. This figure also shows the wiring for the event handler thats generated for the Exit button. This wiring is stored in a generated region of code, and you cant edit it manually. In addition, you cant manually rename or delete the method declaration for an event handler. Instead, if you need to rename or delete an event handler, you must use the Handlers dialog box shown in the next figure.

How to use NetBeans for developing GUI applications

13

The actionPerformed event for the Exit button

The code thats generated for the actionPerformed event of the Exit button
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }

The generated code that wires the event handler to the event
exitButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitButtonActionPerformed(evt); } });

Description
To create an event handler with a generated name, right-click on the control, select Events, and select the appropriate event in the submenus. Or, select the control, click the Events button in the Properties window, click in the text box for the event that you want to handle, and press the Enter key. To create an event handler with a custom name, select the control, click the Events button in the Properties window, click in the text box for the event that you want to handle, enter a custom name for the event handler, and press the Enter key. To wire an event to an existing event handler, enter the name of the existing event handler before you press the Enter key. When you create an event handler, NetBeans will generate all of the required code for the event handler and switch from Design view to Source view so you can complete the code.
Figure 6 How to create an event handler for a control

14

How to use NetBeans for developing GUI applications

How to rename or delete an event handler


When you generate event handlers, there will be times when you will want to delete the event handler. Sometimes, for example, you will accidentally generate an event handler. Then, to delete the handler, you need to display the Handlers dialog box as shown in figure 7, select the event handler, click on the Remove button, and click on the OK button. When you do, the event handler and its wiring will be deleted from the source code for the form. There will also be times when you will want to rename an event handler. If, for example, you want to use one event handler to handle multiple events, you may want to rename it so it uses a name thats appropriate for all of those events. For instance, the application for chapter 17 of our Java book wires the focusGained event of three text boxes to the same event handler. But if you generate an event handler for the first text box in this application, the default name will be paymentTextFieldFocusGained. As a result, you may want to rename this event handler to something that reflects the purpose of the event handler more accurately. To rename an event handler, you again display the Handlers dialog box as shown in this figure. Then, you can select the event, click the Rename button, enter a new name for the event handler, and click the OK button. When you do, the event handler and its wiring will be renamed in the source code for the form. Later, when you generate the event handler for other text boxes, you can wire them to the same event handler thats used by the first text box as described in figure 6. To do that, you select the control, click on the Events button in the Properties window, click to the right of the focusGained event, enter the name of the first event handler, and press Enter. When you do, the focusGained event of the next control will be wired to the same event handler as the first control.

How to use NetBeans for developing GUI applications

15

The Handlers dialog box

Description
Since an event handler includes wiring code thats automatically generated and stored in a different location than the event handler code, you should always use the Handlers dialog box if you need to rename or remove an event handler. That way, both the event handler and its wiring are updated or removed in a single operation. To rename or remove an event handler, select the control, click on the Events button in the Properties window, and click on the ellipsis button to the right of the event that you want to work with. Then, use the Handlers dialog box to remove or rename the event handler.

Figure 7

How to remove or rename an event handler

16

How to use NetBeans for developing GUI applications

How to enter the code for a form


Figure 8 shows how to enter the code for a form. To start, you should enter any import statements that you need for classes that are going to be used by the form. For instance, after the package statement in this figure, you can see that I coded an import statement for the NumberFormat class thats available from the java.text package of the Java SE API, and I coded an import statement for the FinancialCalculations class thats available from the murach.business package. After you code these statements, you can refer to these classes in your event handlers without qualifying them with their package names. Next, you can enter the code for the event handlers that youve generated for the form. In this figure, for example, I have entered the code for the event handlers for the actionPerformed event of the Exit and Calculate buttons. As I entered the code, I used the variable names that I entered to refer to the text box controls. For example, I used the paymentTextField variable name to refer to the entry in the Monthly Payment text box. Although the code in this figure doesnt use any helper methods, you can add helper methods to a form, just as you would add methods to any other class. For example, the GUI application presented in chapter 16 of Murachs Java SE 6 uses a helper method to enable or disable the controls that are on the form. When you add a helper method to a form class, you typically add the method near the method declarations for the event handlers. In this figure, for example, you could add a helper method after the exitButtonActionPerformed method. If you need to use code to initialize a control, you can add that code to the end of the constructor for the form class, after the initComponents method call. Although the code for this constructor isnt shown in this figure, you can display it by clicking on the plus sign (+) thats displayed to its left. Then, you can enter any initialization code after the initComponents method. If, for example, you look at the NetBeans version of the GUI application presented in chapter 16, youll find that it uses code to initialize the list box and combo boxes on the form. This is a common practice since its difficult to use the Properties window to initialize these types of controls. In general, you can ignore the generated code for the form thats stored in the Generated Code region of the form. Since you must always use Design view to modify this code, you rarely need to view it. However, if youre curious, by all means review this code. To do that, you can expand this region by clicking on the plus sign (+) thats displayed to its left. If you study this code, youll find that it uses many of the same techniques that are covered in Murachs Java SE 6. However, it also uses a few techniques that arent described in our book. For example, this code uses a layout manager known as the GroupLayout manager that isnt a standard part of Java. This layout manager is included in the swing-layout-1.0.jar file thats automatically included in the build path for any GUI application thats built with NetBeans.

How to use NetBeans for developing GUI applications

17

The source code after two event handlers have been coded

Description
You can enter the code for an event handler just as you would enter any other code in the code editor. To refer to the controls that are used on the form, use the variable names that you assigned to the controls. You can review these variable names in the Navigator window for the frame, and they are also listed at the end of the source code for the form. To import a library, code an import statement after the package statement thats at the start of the class. If you need to initialize a control, you can add code to the constructor for the class. To add a helper method, you can code the method just below the event handlers. NetBeans shades all generated code, and you cant manually edit this code. If you need to change this code, use Design view to make the change.

Figure 8

How to enter the code for a form

18

How to use NetBeans for developing GUI applications

How to run and debug a form


Figure 9 shows how to run and debug a form. Since these skills are the same for running or debugging any other class that contains a main method, you shouldnt have any trouble with this. If you do, please refer to our How to use NetBeans with Murachs Java SE 6" tutorial that you can download from our web site (www.murach.com).

How to run a form


Since NetBeans automatically generates a main method for each form, you can run a form by right-clicking on the .java file for the form and selecting the Run File command. In this figure, for example, I right-clicked on the FutureValueFrame.java file and selected that command. Then, NetBeans ran the form in its own application window. To test the form, I entered valid numbers for the first three text boxes and clicked on the Calculate button. This application then displayed the result of the calculation in the fourth text box. If youve read chapter 15 of Murachs Java SE 6, you should understand how this application works.

How to debug a form


If you need to debug a form, you can start by setting a breakpoint in the code for the form. Then, you can right-click on the .java file for the form, select the Debug File command, step through code when the application reaches the breakpoint, and check the values of your variables after each step.

How to use NetBeans for developing GUI applications

19

The window thats displayed when you run the form

Description
To run a form, right-click on the .java file for the form and select the Run File command. When you do, NetBeans will run the form in its own application window. To debug a form, set a breakpoint in your code, right-click on the .java file for the form, and select the Debug File command. Then, you can step through the statements and check the values of your variables as each statement is executed.

Figure 9

How to run and debug a project

20

How to use NetBeans for developing GUI applications

Perspective
In this tutorial, you learned how to use the Matisse GUI builder thats available from NetBeans to build the GUI for the Future Value application presented in chapter 15 of Murachs Java SE 6. Now, you should be able to use the same skills to build the applications presented chapters 16 and 17. If you have trouble any applying these skills to those applications, though, you can refer to the NetBeans versions of these applications that you can download from our web site (www.murach.com). In my experience, there are only a few spots where applying the skills presented in this chapter are tricky. First, to place a radio button in a group, you must add a ButtonGroup control to the form even though that control isnt visible on the form. Then, you can use the ButtonGroup property of the radio button to specify the group for the radio button. This creates the logical group for the radio buttons. To visually group the radio buttons, you place them on a JPanel control. Second, to load a list or a combo box, Ive found that its easiest to add code to the constructor for the class that defines the frame. For a combo box, you can use the addItem method of the combo box to add each item to the combo box. Usually, this means using a for statement to loop through an array of values for the dialog box. For a list box, you can use the setListData method of the list box to populate the list box with an array of strings. Third, to wire several events to the same event handler, you create an event handler for the first event as described in figure 6. Then, if necessary, you rename the event handler as described in figure 7. Finally, to wire other events to this event handler, you follow the procedure in figure 6 again, but you enter the name of the first event handler instead of accepting the default name thats suggested by NetBeans.

Summary
A GUI builder is a graphical tool that a developer can use to generate the code thats used to display a graphical user interface and to write the code that makes the GUI work. The Matisse GUI builder is one of the most popular GUI builders for Java developers. It works with Swing components and is available from the NetBeans IDE. A form is the window that holds the controls that comprise the graphical user interface. When working with Swing components, a form typically inherits the JFrame class. Properties are the values that determine how a form and its controls will look and work when the form is displayed. An event handler is a special type of method that responds to an event thats triggered when a user interacts with a form. NetBeans automatically generates the code that wires the method for an event handler to an event.

How to use NetBeans for developing GUI applications

21

Before you do the exercise that follows


Before you do the exercise that follows, you should have the skills that are presented in How to use NetBeans with Murachs Java SE 6, which can be downloaded from our web site. In addition, you should download the NetBeans versions of the folders and files for the applications in our book from our web site and unzip them into the c:\murach\java6 folder. If this folder doesnt exist, you should create it. Then, the GUI versions of the applications for chapters 15 through 17 in our book will be stored in this directory:
c:\murach\java6\netbeans\matisse_apps

You can also create your new applications in this directory.

Exercise 1

Use NetBeans to develop a GUI application

Please read chapter 15 in Murachs Java SE 6 before you do this exercise. This exercise will guide you through the process of using NetBeans to develop the GUI application presented in chapter 15.

Start a new project and add two packages to it


1. 2. 3. Start a new Java Application project named FutureValueGUI, but deselect the Create Main Class option before you complete the operation. Add a new package to the project named murach.ui (ui is for user interface). This is the package that the class for the Future Value form will be stored in. Open the Java project named FutureValue thats stored in the directory shown above. Then, use the Projects tab to copy the murach.business package from the FutureValue project to the FutureValueGUI project that you just created. This should create the murach.business package and copy the FinancialCalculations class thats stored in this package into your project. Use the procedure in figure 1 to add a form named FutureValueFrame to the murach.ui package of the FutureValueGUI project. Use the procedures in figure 2 to add, size, and align the 10 controls that are shown in this figure to the form. Then, size the form. Double-click on each control, and change its text property to the one shown in figure 3. Use the procedure in figure 5 to change the variable names for the four text boxes and two buttons so they can easily be referred to in your Java code. Use the procedure in figure 3 to change any of the other properties for the form or controls so they will look and act the way you want them.

Add a form and its controls to the project


4. 5. 6. 7. 8.

22

How to use NetBeans for developing GUI applications

9.

Test the form, by right-clicking on the .java file for the form in the Projects window and selecting Run File. This should display the form so you can see how it looks, but the form wont work. To close the form, you can click on the Close button in the upper right corner of the form.

10. Another way to preview the form is to click on the Preview Design button in the toolbar for Design view. Try that. This just shows that NetBeans has many features that arent mentioned in this introductory tutorial. 11. If you need to change anything to get the form to look the right way, do that now and test the form again.

Add code to the form class


12. Switch to Source view and add import statements for the java.text.NumberFormat and murach.business.FinancialCalculations classes as shown in figure 8. 13. Use the procedure in figure 6 to generate the starting code and wiring for an event handler that handles the ActionPerformed event of the Exit button. Then, add the source code shown in figure 8 to this event handler. 14. Use the same procedure to generate the starting code and wiring for an event handler that handles the ActionPerformed event of the Calculate button. Then, add the source code shown in figure 8 to this event handler. 15. Run the form again. This time it should work. To prove that, test it with valid entries. Then, if something doesnt work right, fix it and test the application again.

Review the code that was generated for the application


16. In the code editor window, review the code for the constructor of the FutureValueFrame class. Note that it calls an InitComponents method. 17. Click on the plus sign to the left of the Generated Code region to display the InitComponents method within this region. Then, scroll through this method and review its code. Note that this code uses the non-standard GroupLayout manager rather than the Flow and Border layout managers that are described in chapter 15 of Murachs Java SE 6. Finally, take a moment to consider how much work NetBeans has done for you when compared to manually writing this code as described in chapter 15. 18. View the main method near the end of the code, and note that it creates an instance of the FutureValueFrame class, which in turn causes the constructor near the start of the class to be executed.

S-ar putea să vă placă și