Java Application Tutorial

Java Application Tutorial #2

Last time I left you with this.


import java.awt.*;

public class TestApp
{
     public static void main(String args[])
     {
	myFrame Window;

        Window=new myFrame("Untitled");
        Window.show();
     }
}

class myFrame extends Frame
{
     private TextArea myTextArea=new TextArea(" ",25,80);

     myFrame(String Title)
     {
	super(Title);
	setLayout(new GridLayout(1,1));
	this.add(myTextArea);
     }

     public boolean handleEvent(Event ev)
     {
     	if (ev.id == Event.WINDOW_DESTROY)
     	{
		this.dispose();
		System.exit(0);
        	return true;
     	}
     	return super.handleEvent(ev);
     }
}

Don't look so worried. It's not as bad as it looks. We'll ignore the first class even though it looks similar to the TestMain program we looked at in the first tutorial (not read it? Then you should do so now). The class myFrame will create a window for us and inherits (or extends) the existing java class called Frame. Java knows about frames already and we can create our own version based on it.


class myFrame extends Frame
{
     private TextArea myTextArea=new TextArea(" ",25,80);

Now we have created a private variable containing a reference to a text area which acts like a very simple text editor. myTextArea is then given a reference to a new version which is created for us by the compiler. A reference is simply a location (or address) in memory where the text area is stored. Note that the line ends in a semi-colon.


     myFrame(String Title)
     {
	super(Title);
	setLayout(new GridLayout(1,1));
	this.add(myTextArea);
     }

The frame is passed it's title from the main class using the super command (more on that later) and we set a layout (more on those later too). The term this refers to the current class (super refers to the calling class) and the text area is added to the frame.


 public boolean handleEvent(Event ev)
 {
   if (ev.id == Event.WINDOW_DESTROY)
   {
     this.dispose();
     System.exit(0); 
     return true;
   }
   return super.handleEvent(ev);
 }
}

Now we handle the events generated which in this case is whether the user closes the window down (we'll get to mouse events...you guessed it...later). If the user asks to close the class then the class is disposed of and removed from memory (or java will do it for you which in java speak is called garbage collection). We pass the result back to the calling (super) class. This looks horrendous at the moment but it will become clearer the more you look at it. The result of the event is returned to the calling application signalling that the frame has been closed down.

Now we move on to the main class.


import java.awt.*;

Here we import (or include if you're familiar with C) some java libraries. Libraries are code that have already been written for us by the Java developers.


public class TestApp
{
     public static void main(String args[])
     {

This is the same as in our previous tutorial. Then we create a variable using the name of the class we have just created (myFrame). We create a new version of it and give it a title then display it and the myFrame class takes over. This main class is closed once all the references to it have been closed also. And that's it.

	
	myFrame Window;

        Window=new myFrame("Untitled");
        Window.show();
     }
}

I have tried to make this as simple as possible and some functionality has been sacrificed. Time to make it more like a real application.


import java.awt.*;
import java.io.*;
import java.util.*;

public class TestApp
{
     public static void main(String args[])
     {
	myFrame Window;

       	if (args.length > 0)
	{
             Window=new myFrame(args[0]);
       	}
	else
	{        
     	     Window=new myFrame("Untitled");
	     Window.show();
	     Window.resize(Window.preferredSize());
	}
     }
}

Note the addition of the Window.resize function which calls several functions in the class further down to set the window to a size that reflects the content which in this case is a text area.


class myFrame extends Frame
{
     private Dimension myDimension;
     private TextArea myTextArea=new TextArea(" ",25,80);

     myFrame(String Title)
     {
	super(Title);
	setLayout(new GridLayout(1,1));
	this.add(myTextArea);
        myDimension = new Dimension(200,200);
     }

     public Dimension minimumSize()
     {
	return myDimension;
     }

     public Dimension preferredSize()
     {
	return myDimension;
     }

     public boolean handleEvent(Event ev)
     {
     	if (ev.id == Event.WINDOW_DESTROY)
     	{
		this.dispose();
		System.exit(0);
        	return true;
     	}
     	return super.handleEvent(ev);
     }
}

Note the added Dimension functions which set the size of the window. This then is our most basic GUI program. We'll be building on this in the next tutorial.

Back to Java Tutor

Written by Nick Cheesman. Last updated: 01/03/2003
Please eMail me at:
nickjc@nickjc.co.uk