26
Using the Using the Netbeans GUI Netbeans GUI Builder Builder

Using the Netbeans GUI Builder

  • Upload
    neil

  • View
    79

  • Download
    0

Embed Size (px)

DESCRIPTION

Using the Netbeans GUI Builder. 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. It automatically creates code for you. You don’t have to manually write code for: - PowerPoint PPT Presentation

Citation preview

Page 1: Using the  Netbeans GUI Builder

Using the Using the Netbeans GUI Netbeans GUI

BuilderBuilder

Page 2: Using the  Netbeans GUI Builder

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

The GUI Builder offers similar The GUI Builder offers similar functionality to Visual Basic.functionality to Visual Basic.

It automatically creates code for you. It automatically creates code for you. You don’t have to manually write code You don’t have to manually write code for:for: GUI components.GUI components. Event Listeners.Event Listeners.

Page 3: Using the  Netbeans GUI Builder

There are 2 ways to access the GUI There are 2 ways to access the GUI builder.builder.

1. If you are already creating a Java 1. If you are already creating a Java application: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 2. If you are creating a new Java application:application:

File File New Project New Project Under “Projects” select Under “Projects” select “Java Desktop Application” “Java Desktop Application” Click “Next” Click “Next”

Page 4: Using the  Netbeans GUI Builder

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

Page 5: Using the  Netbeans GUI Builder

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

Page 6: Using the  Netbeans GUI Builder

Let’s take a quick look at the Let’s take a quick look at the individual components of the GUI individual components of the GUI Builder….Builder….

Project WindowProject Window Design Window / Source Code WindowDesign Window / Source Code Window PalettePalette GUI Components Properties WindowGUI Components Properties Window

Page 7: Using the  Netbeans GUI Builder

The Project Window is the same.

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

Page 8: Using the  Netbeans GUI Builder

The Design Window allows you to toggle backand forth between the Design View and the SourceView for the code.

Page 9: Using the  Netbeans GUI Builder

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

Page 10: Using the  Netbeans GUI Builder

The Properties Window contains the The Properties Window contains the properties for the GUI components.properties for the GUI components.

This is similar to the properties This is similar to the properties window in Visual Basic.window in Visual Basic.

Page 11: Using the  Netbeans GUI Builder

To add a panel:To add a panel: Drag and Drop a panel from the palette Drag and Drop a panel from the palette

onto the JFrame.onto the JFrame. Reposition the panel on the JFrame.Reposition the panel on the JFrame.

Page 12: Using the  Netbeans GUI Builder

To add a border and title to the To add a border and title to the panel:panel: Click on the button beside the border Click on the button beside the border

property in the Properties Windowproperty in the Properties Window

Select “Titled Border” at the Select “Titled Border” at the bottom, and then type in bottom, and then type in your title text.your title text.

Page 13: Using the  Netbeans GUI Builder

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

Page 14: Using the  Netbeans GUI Builder

Continue to drag and drop Continue to drag and drop components to the panel:components to the panel:

Netbeans will automatically give the GUI Netbeans will automatically give the GUI components generic names like “jLabel1.”components generic names like “jLabel1.”

Page 15: Using the  Netbeans GUI Builder

To edit the display text of the GUI To edit the display text of the GUI components:components: Right click the component and select Right click the component and select

“Edit Text”“Edit Text”OROR

Click the component and wait half a Click the component and wait half a second…second…OROR

Change the text property in the Change the text property in the properties window.properties window.

Page 16: Using the  Netbeans GUI Builder

To change the border of a label:To change the border of a label: Find the “border” property in the Find the “border” property in the

Properties WindowProperties Window

To change the alignment of a GUI To change the alignment of a GUI component:component: Find the “horizontal alignment” Find the “horizontal alignment”

property in the Properties Windowproperty in the Properties Window

Hold down Ctrl and click on multiple Hold down Ctrl and click on multiple objects to move more than one at a objects to move more than one at a time. This helps with formatting.time. This helps with formatting.

Page 17: Using the  Netbeans GUI Builder

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

Page 18: Using the  Netbeans GUI Builder

Notice that if you switch to the Source Notice that if you switch to the Source Code view, you can see the section of Code view, you can see the section of code that defines your GUI components:code that defines your GUI components:

Netbeans does not allow you to manually Netbeans does not allow you to manually change the code they generate.change the code they generate.

Page 19: Using the  Netbeans GUI Builder

To change the name of a GUI To change the name of a GUI component: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.

Page 20: Using the  Netbeans GUI Builder

It is often helpful to rename your It is often helpful to rename your GUI components to easily GUI components to easily recognizable names.recognizable names.

In our example I renamed:In our example I renamed: jTextField1 to jTextNum1jTextField1 to jTextNum1 jTextField2 to jTextNum2jTextField2 to jTextNum2 jButton1 to jButtonAddjButton1 to jButtonAdd jButton2 to jButtonSubtractjButton2 to jButtonSubtract Ect….Ect….

Page 21: Using the  Netbeans GUI Builder

Creating an Event Listener for a GUI Creating an Event Listener for a GUI component is easy. component is easy.

Right-click the button (or GUI Right-click the button (or GUI component).component).

Choose Choose Events Events Action Action actionPerformed.actionPerformed.

Type the code in the space provided.Type the code in the space provided.

For example, if we follow these steps for For example, if we follow these steps for our Exit button, we would then type the our Exit button, we would then type the following code:following code:

Page 22: Using the  Netbeans GUI Builder

Here is the code for the Clear Here is the code for the Clear Button:Button:

private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) { // This clears the text fields and labels jLabelResult.setText(" "); jLabelSymbol.setText(" ? "); jTextNum1.setText(" "); jTextNum2.setText(" ");

}

Page 23: Using the  Netbeans GUI Builder

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(" + "); }

Page 24: Using the  Netbeans GUI Builder

To change the Window Title:To change the Window Title: Go to the “.resources” folder in the Go to the “.resources” folder in the

Project Window.Project Window. Select the section that ends in “…Select the section that ends in “…

App.properties”App.properties” Change the “Application.title” propertyChange the “Application.title” property

Page 25: Using the  Netbeans GUI Builder

To change the color To change the color of a panel (or any of a panel (or any other GUI other GUI component):component):

Select the component.Select the component. In the Property In the Property

Window, select Window, select “background”“background”

Select your color.Select your color. If you don’t like it, If you don’t like it,

just select “Reset to just select “Reset to default.”default.”

Page 26: Using the  Netbeans GUI Builder

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