Same thing but GUI

Chapter: Same thing but GUI

The tasks for the rest of the lab will remind you of Exercise 4 in Lab 5. It is my hope that you will be making more sense of the steps and the code you write.

Now let's do the equivalent program, but make it GUI. Instead of System.out.println let's make stuff appear in a more friendly place. This is more like Alice.

In Alice you start with a world and you add characters from a gallery, and then you add instructions to make what you want happen. In GUI Java we'll start with a javax.swing.JFrame and we'll add objects from a Java library, then we'll add instructions to make what we want happen.

Begin in Eclipse by choosing New->Package (to be called ex2) and New->Class. In the dialog for the new class you'll set the name (MeAndJavaGUI) and tell it about the superclass javax.swing.JFrame. You will want a public static void main stub because you're creating an application rather than an applet.

Q. 1
Huh??


Begin your new class like this:

Eclipse will start writing the code for you:

You can save it and run it if you like, but nothing will happen. Just like an empty world in Alice, you have not told it to do anything, so the play button does nothing.

To make things happen, we need to construct a MeAndJavaGUI object.

You need a constructor for the MeAndJavaGUI class so Java will know what to do when the main method creates a MeAndJavaGUI object. Type in (or copy and paste) the following constructor:

public MeAndJavaGUI() {
  super("MeAndJavaGUI");
  setSize(400,400);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setVisible(true);
}
Also replace the comment in the main method by a statement that calls the constructor of the class. Your edit window should now look like:

Save your code and run.

Now here's an explanation for what you just did.

Q. 2
Do you want to get rid of the warning message that "The serializable class MeAndJavaGUI does not..."?


We are going to make a button appear in our JFrame that has our name on it. And a second button that has a label matching our opinion of Java so far. First of all we need to declare the button objects. We need to import the JButton class, so we say:

import javax.swing.JButton;
and place that right after the other import statement. Then we declare:
private JButton myName, myComment;
That line can go after the header public class ... but before the constructor public MeAndJavaGUI(). It declares an instance variable of type JButton (which is to be found in the package (or, as you would say in Alice, in the gallery) javax.swing).

If you declare the JButtons first, before the import, Eclipse would detect the error and prompt you to add the import statement.

Next we need to actually create JButton objects. That means calling a constructor of the JButton class. Go ahead, check out the documentation and find the constructor that lets you set the label as you create the button. To make the button with your name, you'll need to code:

myName = new JButton("Huckleberry Hound");

Q. 3
Where does that line of code go?


Don't forget you'll need a similar statement to create the button whose label reflects your opinion of Java.

Now we need to cause the JButtons to appear in the content pane of the JFrame (that is the part of the JFrame that's not taken up with title bar, borders, etc.) and to have a label that corresponds to your name. The class Container is to be found in the java.awt package (Go ahead, check it out!) so we'll need:

import java.awt.Container;
and we're also going to need a layout manager to make it all look nice so we'll
import java.awt.FlowLayout;

We'll declare a variable to refer to the content pane, so we can give it a layout manager:

		Container contentPane;
		contentPane = getContentPane();
		contentPane.setLayout(new FlowLayout());

We'll add the buttons and call validate to make sure it appears.

		contentPane.add(myName);
		validate();

I bet you can figure out how to add the second button.


Exercise 2

Add all the code described above. Test the program and make sure it does what you expect. Most importantly, add comments to the code that make it clear to the grader that you understand what you are doing and help the reader understand the code.


rhyspj@gwu.edu