CPE 102

Winter 2007

Lab 9

 

Objectives

 

 

Resources

 

 

Ground Rules

 

 

Orientation 

 

In this lab you will be creating a simple GUI-based Java application.  This application will consist of an application frame, a panel, a button, and a text field.  When the button is pressed the text field will be updated with a message and a count reflecting how many times the button has been pressed.  The button label and message will be provided to the program at run time via the command line arguments.

 

Specification

 

Below are several images of the GUI application you must develop.  Details of the specifications follow the images.  The application must be provided two strings as command line arguments.  You can either start the application from the command line or modify you IDE to invoke the program with parameters (details vary by IDE).  Because this is a GUI-base application you will not be able to run it via the SSH terminal window.  Here is an example of running it from the Windows command-window:

 

 

Here is what the application looks like when it starts.  Notice that the first string provided to the program via the command-line, “Push Me!”, shows up on the JButton.  Your program must do this too!  The string is quoted because it contains spaces – if it were not quoted it would be treated as two arguments instead of one by the shell.  The second string has no spaces so it does not need to be quoted.

 

 

Here is what the application looks like after pushing the button once.  Notice that the text in the JTextField comes partly from the second argument provided via the command-line when the program was started.  The number comes from the program and represents the number of times the JButton has been pressed.

 

 

Here is what the application looks like after pushing the button five times:

 

 

Here are some important details to keep in mind as you implement your solution:

 

  1. Don’t forget that the JFrame’s content pane defaults to the Border Layout Manager – you’ll need to use an additional panel with the Flow Layout Manager or replace the content pane with a JPanel using the Flow Layout Manager to get the correct appearance.

 

  1. Use the ActionListener interface to listen for the JButton press (not MouseListener!)

 

  1. You should use the pack method to size the JFrame after adding the JButton and JTextFiled to it.

 

  1. Allow the JButtons size to be determined by the text you put on it (which comes from the command-line) – this means you do not need to set its size explicitly.

 

  1. Set the preferred size of the JTextFiled to be 200 pixels wide and 20 pixels high.

 

  1. Don’t forget to set the default close behavior of the JFrame to EXIT_ON_CLOSE.

 

  1. Don’t forget to make you JFrame visible!