CSCI 121 Lab Assignment 5

Fall Semester, 2006

 

Outcomes:

 

Upon completion of the lab, the student will know how to

 

·       Use Java swing components to lay out a simple GUI application

 

·       Implement the Java ActionListener interface to handle events such as button clicks

 

 

Activities:

 

 

1

 

In this assignment you will create a GUI class named Lab5 that performs simple manipulations of strings entered by the user.  These include:

 

·       Convert the string to lower case

·       Convert the string to upper case

·       Reverse the string

·       Clear the input string text field

 

Here is a screen shot to show you how the finished application should look:

 

 

¬ Title bar: “Lab 5: String Operations”

¬ A JLabel: “Input text in …”

¬ A JTextField

¬ A JLabel: “Result of operation”

¬ A JTextField

¬ 4 JButtons

 

You should tackle this problem in two parts:

·        Implement the frame layout

·        Implement the action listener

 

 

 

2

 

The layout of the frame above is pretty simple:

·       The contentPane of the frame uses a GridLayout with 5 rows and 1 column

o      Add a JLabel with text “Input text in the space below:”

o      Add a JTextField object (make this an instance variable for reasons given below)

o      Add a JLabel with text “Result of operation”

o      Add a JTextField object (make this an instance variable)

o      Add a JPanel with GridLayout(1, 4)

§       Add 4 JButtons to the JPanel

 

Skeleton of program to get you started:

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class Lab5 extends JFrame implements ActionListener

{

    private JTextField inText, outText;

 

    public Lab5()

    {

       setTitle("Lab 5: String Operations");

      

       // make application close when window is closed:

       setDefaultCloseOperation(EXIT_ON_CLOSE);

 

       Container pane = getContentPane();

       pane.setLayout(new GridLayout(5,1));

 

       // Add a label "Input text in the space below:"

 

 

       // Add the text field inText

 

 

       // Add a label "Result of operation:"

 

 

       // Add the text field outText

      

 

       // Create a JPanel with GridLayout(1, 4)

 

 

       // Create the 4 JButtons and add them to the JPanel

 

 

       // Add the JPanel to the content pane

 

       pack();

    }

   

    public void actionPerformed(ActionEvent e)

    {

       // empty for now…

    }

   

    public static void main(String [] args)

    {

       Lab5 lab5 = new Lab5();

       lab5.setLocation(200, 300);

       lab5.show();

    }

}

 

When you get this part of the application working, demonstrate it to the lab instructor before proceeding to the next part of the assignment.

 

 

3

 

Now you need to fill in the body of the actionPerformed method.  The actionPerformed method will detect button click events and perform some action involving the JTextField objects inText and outText.  This is basically a nested if-else statement:

 

    if (e.getActionCommand().equals("toUpper"))

    {

       // convert the string in inText to upper case

// and display it in outText

    }

    else if (e.getActionCommand().equals("toLower"))

    {

       // convert the string in inText to lower case

// and display it in outText

    }

    else if (e.getActionCommand().equals("Reverse"))

    {

       // reverse the string in inText and display

// it in outText

    }

    else

    {

       // clear the text fields inText and outText

    }

 

You must also register the action listener with each of the JButtons in the Lab5 constructor.  For example, for the JButton object upButton you would use:

 

    upButton.addActionListener(this);

 

Here are more screen shots to demonstrate the operations.  In each case the operation has been performed on the input text “UPPERCASE and lowercase tExt”:

 

1. Result of clicking the toUpper button:

 

 

2. Result of clicking the toLower button:

 

 

3. Result of clicking the Reverse button:

 

 

4. Result of clicking the Clear button:

 

 

When you complete this portion of the assignment, demonstrate your program to the lab instructor.  Print a copy of your program and get the lab instructor to sign a printed listing of the program.  Hand in this signed copy.