Documente Academic
Documente Profesional
Documente Cultură
Perspective ............................................................................ 20
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
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
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
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
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
10
11
A form after the variable names have been set for the controls
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
12
13
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
15
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
16
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
18
19
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
20
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.
21
Exercise 1
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.
22
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.