Creation of a Frame

Your Frame is a container for your UI components and the main window of your application. Why is it the main window? If you had a Window as the main window and you wanted to change the icon or cursor of the application you might need to create some native methods. Whereas the Frame already contains a basic "framework" for a main windo in a standalone application.

Note: A Container can contain other Components and other Containers(since Container is a sub-class of Component anyhow).

Thus an application starts off like this...

import java.awt.*;

public class MyApp extends Frame
	// "final" variables are constants
	static final int H_SIZE = 300;
	static final int V_SIZE = 200;

	public MyApp()
		// Calls the parent constructor 
		// Frame(string title)
		// Equivalent to setTitle("My First Application")
		super("My First Application");

		resize(H_SIZE, V_SIZE);

	public static void main(String args[])
		new MyApp();
In short the class above Is-A Frame, since it extends(i.e inherits) from it. It has one method, the constructor taking no parameters, thus the following cases are illegal.
new MyApp("Hello World");
new MyApp(300, 200, "My First App");
but would be perfectly legal if we defined two more constructors
public MyApp(String title){}
public MyApp(int x, int y, String title){}
Also notice, we made an explicit call to the super constructor(i.e parent constructor) if we did not Java would call the constructor anyways. But we decided to utilize the parent constructor and pass it a string which is the title of our application.

show() is required because by default, AWT Containers are created hidden and must be made visible. Pack() is used here to properly adjust Components such as Buttons, Lists, etc. to their correct sizes.

Note: static void main(String args[]) is considered a class method, not an instance method. It really does not depend on an instance of MyApp and does not belong to it.

This is the result of the above code under Win95.

It has the following attributes:

To change it, you add the following code to your constructor for initialization purposes.
  1. Change the font to a plain 12pt Times Roman font.
  2. Set the background to white and foreground to black
  3. Set the layout for this Container to be BorderLayout(see Introduction to the AWT and Panels and Layouts)
setFont(new Font("TimesRoman", Font.PLAIN, 12));
setLayout(new BorderLayout());
And this for some graphics. Using methods defined in the Graphics class.
public void paint(Graphics g)
	// 24 pt red font used here
	g.setFont(new Font("TimesRoman", Font.BOLD, 24));
	g.drawString(getTitle(), 30, 50);
Add this for two buttons called "Ok" and "Cancel" located at the bottom.
Panel p = new Panel();
p.add(new Button("Ok"));
p.add(new Button("Cancel"));
add("South", p);
Containers add Component via one of the two add() methods defined:
add(Component c)
add(String s, Component c)
Also, why do we use Panels here? What are Panels? The answer to those questions are rather simple. We liked to organize these buttons in a certain way. We could have done the following
add("South", new Button("Ok"));
add("South", new Button("Cancel"));
But why? Why not? It's nice to organize Components into nice little sections. In fact the Panel can be then considered a separate enitity from the Frame. You can then change the background, layout style, font, etc. without changing it in the Frame.

Do not worry if some of the material is flying over your head, I will explain in detail later. First we get our feet wet... very wet!

This should be the final result.

If you run the code above, you'll realize whenever you try to close the Window, nothing happens? Why?

You haven't handled the Event.WINDOW_DESTROY(static variable) message and properly exited.
So create an event handler for Event.WINDOW_DESTROY in handleEvent(Event evt).

public boolean handleEvent(Event evt)
	// Return false if we want the system to also process
	// the message, otherwise return true to say we're done
	// with the message
		// Event.WINDOW_DESTROY documentation
		// can be found in the Event classes
		case Event.WINDOW_DESTROY:
			return true;
	}		return false;
Now try compiling and executing the application. It exits when you close it right? Good and that concludes this section of the Tutorial.
[Next] [Prev] [Home]
Nelson Yu
Last modified: Jan 2 1996