Introduction to Java using NetBeans

Section 2 - GUI

 


Creating a Project with a GUI

  1. Click on the New Project icon in the toolbar or from the menu bar select File, New Project.
  2. Create a new Java Application
  1. Enter CapturingData in the Project Name field and specify your project location.

4. Right Click on CapturingData in the Projects List. Choose New > JFrame Form and Name your Form

 

 

 

Building your GUI

This is the main window of your capturing input program.
  1. Drag a Label from the Palette to your application window.
    Double click on it and change the text to read, “Enter Your Name:”.
    In Properties window, modify font and foreground
  2. Drag a Text Field from the Palette
    In the Properties window find the text property and remove any text you find there so that your field is blank to start with.
  3. Drag a Button from the Palette to your application window and place it as shown in the image above.
    In the Properties window find the text property and change it to “Enter”.
  4. Drag another Label from the Palette to your application and place it under the button.
    Change the text property to “My first GUI program”.

Modify your form and make it look a little better

  1. Change the component names to something more meaningful: nameLabel, nameInput, enterButton, and outputLabel.
    Right click on the component and choose change variable name:

Adding Functionality

 

  1. Right click on the Enter button. From the menu that appears select Event, Action, ActionPerformed. This will create what is called an ActionListener for the Enter button. This means that the program will be listening for the ActionPerformed event to occur, and if it does, it will run the set of instructions that you give it within that event. The ActionPerformed event listens for the button to be pressed by either the mouse or the keyboard.
  2. At this point the IDE will display the code that goes along with your application. Netbeans has added lots of code, locate this line

This is the auto created function for the Enter button.

  1. We are now going to add code that controls what the Enter button does.

This is the completed code for the Enter button.

Congratulations. You have now just completed your first program with a GUI. It is now time to make it run

 

 

Modify the Capturing Data code as follows:

 

Modifying the Code

Now that you have a running program with a working GUI, it is time to experiment. Try changing the properties of some of the components. Try adding new components. Play around with the code and see what you can do.

A good place to start is by adding a new button and labeling it Exit. Now add an event to this button that causes the program to close when the button is pressed. Use the following code inside your ActionPerformed method to end the program:

System.exit(0);

 

 

Use the website below to find Java String Methods to complete the programs below.

http://www.tutorialspoint.com/java/java_strings.htm

 

Assignment 2.10 - Birthday Program

In this assignment, you will create a Birthday Program that makes use of the following:

  • Three labels displaying the title and information requests.
  • Two input text fields for name and date.
  • A label to display the resulting message.
  • A button.

 

Enrichment Icon

See if you can add an image to your application. To do this, you will need to set an image file to a components icon property and the image size will have to be the same as the component size.


A sample layout for the input/output screen is shown below.

Feel free to create your own version of this screen using a different background, colour, and/or a different text font style/size.

This shows a possible output screen for the Happy Birthday program.

Note: Use a Panel the size of your form to set the Background!

 

Modify the Program so that when the user enters First Name and Last Name in the "Enter Name" field the program the following output will be displayed

LASTNAME Family Birthdays ( lastname All caps)

Firstname's Birthday is on May 29, 1982

i.e.

Input

Jane Doe
Feb. 29, 2015

Output

DOE Family Birthdays

Jane's Birthday is on Feb. 29, 2015

ICS4U modify the program to include 2 additional names.

Assignment 2.11 - Monogram

Create a monogram application that prompts the user to enter his or her first, middle, and last names and then displays a monogram with the first and middle initial in lowercase and the last initial in uppercase.

 

Assignment 2.12 - Replace String

Create a Replace String application that displays the new string in a lable. The new string should take a sentence entered by the user and replace every occurence of a substring with the new string supplied.

ICS4U modify the program so that the user can modify all instances or only the first instance.

Assignment 2.13 - Remove String

Create a Remove Sting application that displays a new string in a label. The new string should take a sentence entered by the user and remove every occurrence of a substring supplied by the user.

ICS4U modify the program so that the user can remove all instances or only the first instance.

 

Complete the S2 Rubric and place it in a Rubrics folder on your Google Drive. Make sure I have access to edit this file.

Put a link to your Rubric on your website.

Include link to folder, link to jar file, code.

Creating a Jar File
Right-click on the Project name
Select Properties
Click Packaging
Check Build JAR after Compiling
Check Compress JAR File
Click OK to accept changes
Right-click on a Project name
Select Build or Clean and Build