GUI
[Framework practical 6 of 7]
This practical we're going to build a GUI for our application.
We're going to make our application extend Frame, so we can see it as a Window, and then add a menu so we can run our readData and writeData methods from the GUI.
First off, let's get our app a windows-based GUI.
Copy last practical's code into a new directory for framework6
. Empty the
constructor for Analyst, but leave everything else as it is.
First up, let's make the Analyst class extend Frame. We'll need to import java.awt.*
and we might as well
import java.awt.event.*
at the same time, as we'll need it later. Here's the new Analyst class:
import java.awt.*;
import java.awt.event.*;
public class Analyst extends Frame{
public Analyst () {
// Our Analyst code this practical will go here.
}
public static void main (String args[]) {
new Analyst();
}
}
Get your Analyst code in the form above, then add in the code to set the size of the Frame (to 300 by 300) and set its visibility to 'true'. Remember, we've inherited these methods from Frame - we don't need an object to use them. Compile and run the file.
Make sure you set the Frame size before you set it as visible. Indeed, in general you should try to do pretty much all you need to do to the GUI before you set it as visible for the user.
Remember that if you want to shut the window at the moment you'll have to use Ctrl-C. You
can fix this, if you like, by adding the anonymous inner class we saw in the lecture. It would go inside
the Analyst's constructor (as we've now extended Frame, we've invisibly picked up the addWindowListener
method). This is one of the few cases where you can put a class inside another, and a method inside a method.
Next, go on to Part Two, where we'll set up a menu.