GUI

Chapter: GUI

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.

This will be a series of exercises leading you to a program we'll call MeGUI. Begin in Eclipse by choosing New->Class. In the dialog you'll set the name (MeGUI) and tell it about the superclass javax.swing.JFrame. You still want a public static void main stub. 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 MeGui object.


Exercise 3

You need a constructor for the MeGUI class so Java will know what to do when the main method creates a MeGUI object. Type in (or copy and paste) the following constructor:
public MeGUI() {
  super("MeGUI");
  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. 1
Do you want to get rid of the warning message that "The serializable class MeGUI does not..."?


All of the methods called so far are provided by the Java API. They are like the built-in methods of the characters in the galleries in Alice. In the next chapter we'll add an object and add a method of our own.


rhyspj@gwu.edu