During the process of creating ColorSwitch, you will learn how to do these tasks:



Similar documents
Tutorial: Time Of Day Part 2 GUI Design in NetBeans


Entering the example employee satisfaction survey in Survey Crafter Professional s Survey Designer window

Microsoft Excel 2013: Macro to apply Custom Margins, Titles, Gridlines, Autofit Width & Add Macro to Quick Access Toolbar & How to Delete a Macro.

Microsoft Access 2010 handout

Acrobat PDF Forms - Part 2

Maximizing the Use of Slide Masters to Make Global Changes in PowerPoint

MICROSOFT OUTLOOK 2010 WORK WITH CONTACTS

MICROSOFT ACCESS 2007 BOOK 2

Google Sites: Site Creation and Home Page Design

Excel basics. Before you begin. What you'll learn. Requirements. Estimated time to complete:

MS Excel Template Building and Mapping for Neat 5

Advanced Presentation Features and Animation

For Introduction to Java Programming, 5E By Y. Daniel Liang

Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

Building and Using Web Services With JDeveloper 11g

Microsoft Outlook 2007 Calendar Features

1.5 MONITOR. Schools Accountancy Team INTRODUCTION

Microsoft Word Track Changes

Adobe Dreamweaver CC 14 Tutorial

USER GUIDE. Unit 2: Synergy. Chapter 2: Using Schoolwires Synergy

Access 2007 Creating Forms Table of Contents

Excel 2007 Basic knowledge

Instructions for Creating Silly Survey Database

Handout: Word 2010 Tips and Shortcuts

Microsoft Office PowerPoint 2013

Creating Custom Crystal Reports Tutorial

Use the Microsoft Office Word Add-In to Create a Source Document Template for Microsoft Dynamics AX 2012 WHITEPAPER

Database Forms and Reports Tutorial

Microsoft PowerPoint 2008

Most of your tasks in Windows XP will involve working with information

Excel 2003 Tutorial I

Introduction To Microsoft Office PowerPoint Bob Booth July 2008 AP-PPT5

FastTrack Schedule 10. Tutorials Manual. Copyright 2010, AEC Software, Inc. All rights reserved.

O UTLOOK 2003 HELP SHEET MAIL. Opening the program. Mail

Bookstore Application: Client Tier

Basic Excel Handbook

Data Visualization. Brief Overview of ArcMap

Getting Started with Excel Table of Contents

Lotus Notes Client Version 8.5 Reference Guide

Using Adobe Dreamweaver CS4 (10.0)

Introduction to Macromedia Dreamweaver MX

Creating a Newsletter with Microsoft Word

Using Clicker 5. Hide/View Explorer. Go to the Home Grid. Create Grids. Folders, Grids, and Files. Navigation Tools

Outlook . User Guide IS TRAINING CENTER. 833 Chestnut St, Suite 600. Philadelphia, PA

Creating tables of contents and figures in Word 2013

Publisher 2007: Part 2 Advanced Features. Grouped Objects in Publisher:

MICROSOFT OFFICE ACCESS NEW FEATURES

MICROSOFT ACCESS 2003 TUTORIAL

Instructions for Creating an Outlook Distribution List from an Excel File

Introduction to MS WINDOWS XP

Writer Guide. Chapter 15 Using Forms in Writer

Add an E-Commerce Catalog to your Website

MS Word 2007 practical notes

Designing portal site structure and page layout using IBM Rational Application Developer V7 Part of a series on portal and portlet development

NetIQ Operations Center 5: The Best IT Management Tool in the World Lab

Word basics. Before you begin. What you'll learn. Requirements. Estimated time to complete:

1.5 MONITOR FOR FMS 6 USER GUIDE

Module One: Getting Started Opening Outlook Setting Up Outlook for the First Time Understanding the Interface...

Chapter 15 Using Forms in Writer

MICROSOFT WORD TUTORIAL

Word 2010: Mail Merge to with Attachments

Lesson 07: MS ACCESS - Handout. Introduction to database (30 mins)

Ansur Test Executive. Users Manual

6. If you want to enter specific formats, click the Format Tab to auto format the information that is entered into the field.

Avigilon Control Center Web Client User Guide

Software Application Tutorial

Module B. Key Applications Using Microsoft Office 2010

ESRI China (Hong Kong) Limited

Create a New Database in Access 2010

Microsoft PowerPoint 2010 Handout

Introduction to Microsoft Access 2003

Outlook. Getting Started Outlook vs. Outlook Express Setting up a profile Outlook Today screen Navigation Pane

Creating and Using Links and Bookmarks in PDF Documents

Check out our website!

LETTERS, LABELS &

Microsoft Word Quick Reference Guide. Union Institute & University

Dreamweaver and Fireworks MX Integration Brian Hogan

Create an Excel BI report and share on SharePoint 2013

Appendix A How to create a data-sharing lab

Chapter 11 Sharing and Reviewing Documents

Creating trouble-free numbering in Microsoft Word

Microsoft Word 2010 Tutorial

Excel 2010: Create your first spreadsheet

Using the SAS Enterprise Guide (Version 4.2)

Writer Guide. Chapter 15 Using Forms in Writer

ADOBE ACROBAT 7.0 CREATING FORMS

Microsoft Excel 2010 Tutorial

Create, Link, or Edit a GPO with Active Directory Users and Computers

SiteBuilder 2.1 Manual

8 CREATING FORM WITH FORM WIZARD AND FORM DESIGNER

HOUR 9. Formatting Worksheets to Look Great

Generating lesson plans with. Adobe Acrobat

Section DB DATABASE - Microsoft Access

Create Charts in Excel

Lab Activity File Management with Windows Explorer Windows XP, Vista, Windows 7 and Windows 8 Brought to you by RMRoberts.com

NDSU Technology Learning & Media Center. Introduction to Google Sites

In this example, Mrs. Smith is looking to create graphs that represent the ethnic diversity of the 24 students in her 4 th grade class.

Create a Poster Using Publisher

CREATE A WEB PAGE WITH LINKS TO DOCUMENTS USING MICROSOFT WORD 2007

Transcription:

GUI Building in NetBeans IDE 3.6 This short tutorial guides you through the process of creating an application called ColorSwitch. You will build a simple program that enables you to switch the color of a panel from light gray to medium gray to black. During the process of creating ColorSwitch, you will learn how to do these tasks: 1. Create a container from a template and place it in a project 2. Select a layout manager, add label and button components to a container, edit the component properties, and create connections between the components 3. Edit Java source code by adding the event handler code 4. Compile and run an application 5. View the class hierarchy, if desired This tutorial takes less than an hour to complete. Creating a Container The top-level container that you create holds the other components (button and label) in your application. You create the container using the JFrame component and place the container in the colorswitch package. (A package is a group of related Java classes and interfaces.) To create the container: 1. Somewhere on your machine's file system, create an IDETutorial directory. 2. Choose File Mount Filesystem. 3. Select Local Directory and click Next. 4. Navigate to your IDETutorial directory, select it, and click Finish. The IDETutorial directory is mounted in the Filesystems window. 5. In the Filesystems window, right-click the IDETutorial node and choose New JFrame Form. 6. Type ColorSwitch in the Name text field. 7. Click Finish. A node for the ColorSwitch Form object is added to the IDETutorial directory in the Filesystems window. The ColorSwitch node can be expanded to display the nodes for the ColorSwitch class and the ColorSwitch form. In addition, two tabs appear in the Source Editor for the ColorSwitch file. The first tab displays the Java source code for the ColorSwitch class. You can type only in the white areas of the Source Editor. The second tab shows the ColorSwitch class in the Form Editor. When you select the Form Editor tab, three windows appear to the right of the Source Editor:

o o o Palette. Contains tabs for various AWT, JFC/Swing, and JavaBeans components, along with a selection of layout managers and border styles. Inspector. Provides a tree hierarchy representation of the components in your application. Properties. Displays the properties of the whatever is selected in the Inspector, Form Editor, or Filesystems window. Selecting a Layout Manager A layout manager assists you in determining the size and position of the components within a container. Each container type has a default layout manager. BorderLayout is the default layout manager for the JFrame component. BorderLayout divides the container into five sections (north, south, east, west, and center). In this step, you switch to the GridLayout layout manager, which creates sections equal in size and displays them in the requested numbers of rows and columns. To change layout managers: 1. In the Palette, click the Layouts tab. The Layouts tab provides access to the layout managers in the Java Foundation Classes (JFC). When you move your mouse pointer over a button in the layouts toolbar, a tool tip displays the name of the layout manager represented by that button. 2. Click the GridLayout button ( ) in the Layouts tab. 3. Click anywhere on the grey ColorSwitch form in the Form Editor to set the layout manager. The GridLayout is added to the JFrame's component hierarchy in the Inspector. 4. In the Inspector, select GridLayout in the component hierarchy. The properties of the layout manager appear in the Properties pane below the Inspector. 5. In the property sheet, set the Columns property to 1 and the Rows property to 2. Press Enter after each entry to change the property values. Although the Form Editor does not display a grid, it now contains a single column and two rows. Adding a Label and Setting Its Properties Your application uses a blank label with an opaque background to display the colors. You need to add the label and set its properties.

To add a label: 1. Click the Swing tab on the Palette. 2. Click the JLabel button from the Swing palette. 3. Click anywhere on the ColorSwitch form in the Form Editor. A label with the text jlabel1 is displayed in the Form Editor. In the Inspector, a node named jlabel1 [JLabel] is displayed and highlighted in the tree view. The properties for the component are displayed in the Properties pane. Source code for the label is displayed in the Source Editor. 4. In the Properties tab, scroll to the text property and delete jlabel1, leaving the value blank. Press Enter. 5. Scroll down to Other Properties and check the check box for the opaque property. All you should see in the Form Editor is a line that demarks the edge boundaries of the jlabel1 component. Adding a Button You will now add the button that operates the ColorSwitch application. To add a button: 1. In the Swing palette, click the JButton button. 2. Click anywhere on the ColorSwitch form in the Form Editor. The button is displayed in the Form Editor, its corresponding node is displayed in the Inspector, and its source code is displayed in the Source Editor. 3. In the Properties tab of the Properties pane, set the text property to Switch Color. 4. In the Properties tab, click the ellipsis (...) button for the font property. 5. In the jbutton dialog box, select Bold for font style and 14 for font size. Click OK. The button label, Switch Color, should appear in larger bold type. You should see Switch Color in half of the Form Editor pane. In the other half, you should see an empty space with a dark boundary line between the two components. Setting Up the Button to Switch Color To add the mouse event: 1. In the Inspector, select the jbutton1 node (if it is not already selected). 2. In the Events tab of the Properties window, select the mouseclicked property. The value changes from none to jbutton1mouseclicked.

3. Press Enter. The Source Editor switches to the tab containing the Java source code for the ColorSwitch class and displays the listener code jbutton1.addmouselistener and event method jbutton1mouseclicked(). Adding Event Handler Code Now that the jbutton1mouseclicked() method has been created, you can add custom code for handling this event. You want a click of the button to change the color of the label. To add the code: 1. In the Source Editor toolbar, locate the Quick Browse combo box and select JButton1 from the list of your ColorSwitch application's elements. The cursor appears at the beginning of the JButton1 variable declaration in a blue guarded block. Guarded blocks of Java code generated by the Form Editor have a light blue background and cannot be edited. 2. Move the cursor to the line below the variable declaration section (on or about line 71) and declare the following new variable: 3. private java.awt.color currentcolor = java.awt.color.lightgray; You can display line numbers in the Source Editor by right-clicking in the left margin and choosing Show Line Numbers. 4. 5. Select jbutton1mouseclicked in the Quick Browse combo box. Place the cursor after //Add your handling code here: below the guarded block (on or about line 53) and type the following: 6. if (currentcolor == java.awt.color.lightgray) 7. currentcolor = java.awt.color.gray; 8. else if (currentcolor == java.awt.color.gray) 9. currentcolor = java.awt.color.black; 10. else 11. currentcolor = java.awt.color.lightgray; 12. jlabel1.setbackground (currentcolor); Note that the last line is jlabel with the number one ("1") before the period and not the letter "l". 13. 14. Save the file by choosing File Save (Ctrl + S).

Compiling and Running Your Program Now that you have created the user interface and the connections between the components, you can compile and run your application. To compile and run the program: 1. Compile ColorSwitch by choosing Build Compile (F9). If the build is successful, the "Finished ColorSwitch" message appears in the status bar in the main window. If there are problems with the build, error messages are displayed in the Output window. You can double-click an error message to jump to the line in the source code where the error occurred. Once the build is successful, you can run the application. 2. Run ColorSwitch by choosing Build Execute (F6). You can also skip step 1 and simply execute your program. The IDE will compile any classes that are out of date before running the program. 3. 4. If there are no execution errors, the IDE displays the Output and Execution windows. 5. The ColorSwitch application is displayed: 6. 7. In the application, click the button three times to check that the label area above the button changes from light gray to medium gray to black. 8. Exit the application.