Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using the Netbeans GUI Builder. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans.

Similar presentations


Presentation on theme: "Using the Netbeans GUI Builder. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans."— Presentation transcript:

1 Using the Netbeans GUI Builder

2 The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The GUI Builder offers similar functionality to Visual Basic. The GUI Builder offers similar functionality to Visual Basic. It automatically creates code for you. You don’t have to manually write code for: It automatically creates code for you. You don’t have to manually write code for: GUI components. GUI components. Event Listeners. Event Listeners.

3 There are 2 ways to access the GUI builder. There are 2 ways to access the GUI builder. 1. If you are already creating a Java application: 1. If you are already creating a Java application: Right click on the project. Right click on the project. Select “JFrame.” Select “JFrame.” The GUI Builder and the palette will come up. The GUI Builder and the palette will come up. 2. If you are creating a new Java application: 2. If you are creating a new Java application: File  New Project  Under “Projects” select “Java Desktop Application”  Click “Next” File  New Project  Under “Projects” select “Java Desktop Application”  Click “Next”

4 Name your project and click “Finish.” Name your project and click “Finish.”

5 Here is what the interface for the GUI Builder looks like:

6 Let’s take a quick look at the individual components of the GUI Builder…. Let’s take a quick look at the individual components of the GUI Builder…. Project Window Project Window Design Window / Source Code Window Design Window / Source Code Window Palette Palette GUI Components Properties Window GUI Components Properties Window

7 The Project Window is the same. The Java class that you will be working with is the one that ends with the word “View.”

8 The Design Window allows you to toggle back and forth between the Design View and the Source View for the code.

9 The Palette gives you the ability to drag and drop GUI components onto the JFrame.

10 The Properties Window contains the properties for the GUI components. The Properties Window contains the properties for the GUI components. This is similar to the properties window in Visual Basic. This is similar to the properties window in Visual Basic.

11 To add a panel: To add a panel: Drag and Drop a panel from the palette onto the JFrame. Drag and Drop a panel from the palette onto the JFrame. Reposition the panel on the JFrame. Reposition the panel on the JFrame.

12 To add a border and title to the panel: To add a border and title to the panel: Click on the button beside the border property in the Properties Window Click on the button beside the border property in the Properties Window Select “Titled Border” at the bottom, and then type in your title text. Select “Titled Border” at the bottom, and then type in your title text.

13 Now your panel has a border and a title: Now your panel has a border and a title:

14 Continue to drag and drop components to the panel: Continue to drag and drop components to the panel: Netbeans will automatically give the GUI components generic names like “jLabel1.” Netbeans will automatically give the GUI components generic names like “jLabel1.”

15 To edit the display text of the GUI components: To edit the display text of the GUI components: Right click the component and select “Edit Text” Right click the component and select “Edit Text”OR Click the component and wait half a second… Click the component and wait half a second…OR Change the text property in the properties window. Change the text property in the properties window.

16 To change the border of a label: To change the border of a label: Find the “border” property in the Properties Window Find the “border” property in the Properties Window To change the alignment of a GUI component: To change the alignment of a GUI component: Find the “horizontal alignment” property in the Properties Window Find the “horizontal alignment” property in the Properties Window Hold down Ctrl and click on multiple objects to move more than one at a time. This helps with formatting. Hold down Ctrl and click on multiple objects to move more than one at a time. This helps with formatting.

17 Here is our updated JFrame: Here is our updated JFrame:

18 Notice that if you switch to the Source Code view, you can see the section of code that defines your GUI components: Notice that if you switch to the Source Code view, you can see the section of code that defines your GUI components: Netbeans does not allow you to manually change the code they generate. Netbeans does not allow you to manually change the code they generate.

19 To change the name of a GUI component: To change the name of a GUI component: Click on the GUI component. Click on the GUI component. Go to the Property Window. Go to the Property Window. Select “Code” Select “Code” Change the Variable Name property. Change the Variable Name property.

20 It is often helpful to rename your GUI components to easily recognizable names. It is often helpful to rename your GUI components to easily recognizable names. In our example I renamed: In our example I renamed: jTextField1 to jTextNum1 jTextField1 to jTextNum1 jTextField2 to jTextNum2 jTextField2 to jTextNum2 jButton1 to jButtonAdd jButton1 to jButtonAdd jButton2 to jButtonSubtract jButton2 to jButtonSubtract Ect…. Ect….

21 Creating an Event Listener for a GUI component is easy. Creating an Event Listener for a GUI component is easy. Right-click the button (or GUI component). Right-click the button (or GUI component). Choose  Events  Action  actionPerformed. Choose  Events  Action  actionPerformed. Type the code in the space provided. Type the code in the space provided. For example, if we follow these steps for our Exit button, we would then type the following code: For example, if we follow these steps for our Exit button, we would then type the following code:

22 Here is the code for the Clear Button: Here is the code for the Clear Button: private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) { // This clears the text fields and labels jLabelResult.setText(" "); jLabelSymbol.setText(" ? "); jTextNum1.setText(" "); jTextNum2.setText(" "); }

23 Here is the code for the Add Button: Here is the code for the Add Button: private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) { //Declare variables int intResult; int intNum1; int intNum2; //Get information from text fields intNum1 = Integer.parseInt(jTextNum1.getText()); intNum2 = Integer.parseInt(jTextNum2.getText()); //Perform calculations intResult = intNum1 + intNum2; //Return and display data jLabelResult.setText(String.valueOf(intResult)); //Set symbol jLabelSymbol.setText(" + "); }

24 To change the Window Title: To change the Window Title: Go to the “.resources” folder in the Project Window. Go to the “.resources” folder in the Project Window. Select the section that ends in “…App.properties” Select the section that ends in “…App.properties” Change the “Application.title” property Change the “Application.title” property

25 To change the color of a panel (or any other GUI component): To change the color of a panel (or any other GUI component): Select the component. Select the component. In the Property Window, select “background” In the Property Window, select “background” Select your color. Select your color. If you don’t like it, just select “Reset to default.” If you don’t like it, just select “Reset to default.”

26 Now you can let Netbeans do the hard work! Now you can let Netbeans do the hard work!


Download ppt "Using the Netbeans GUI Builder. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans."

Similar presentations


Ads by Google