cmcculloh web design
cmcculloh.com - Personal - Tutorials - Java Lectures - 2/23/06

2/23/06

Michael Xue
Christopher McCulloh
2.23.06

Valid XHTML 1.0 Strict
We are going to add a button to our window.

When you have a window, you have something called panels in it. The windows manager decides where they go. You also have a border layout. It has North, South, East, West, and Center. When you put something on the panel, you have to tell it where to go. Otherwise, it will just put it right in the center.

So now what we are going to do is, we are going to set the layout with what we are familiar with. The flow layout. The way to do that is, you say "setLayout(new FlowLayout());"

With that we are able to put a listener on the button, so that when you click the button, it makes the button disappear. So you add an action listener, and use the frame itself as a listener. If the frame is going to be ther listener, you have to implement the actionlistener. So now we also need to have a method called action performed. So, action listener with an action event method that disposes the button.

Now let's take the circle applet that displayed the radius, and make it into a desktop application.

Right now we are doing a very small application, but what if you had a very big application? What if you wanted to have a version for the web, and a version for the desktop? Wouldn't it be nice if instead of having to copy and pst a whole bunch of code, we created a common component.

So we let's say we put on another panel, we can put a bunch of stuff on it that contains all the graphics components and stuff, and then we can just call this panel from the applet, or we can just call the panel from the frame.

So we need to develop a customized panel

....
public class AppPanel extends Panel implements ___, ____{
		Listeners...
		Paint...
}

We need to think about this very genericly. It needs to be applied both in the frames and the applet situation

So lets think about having a text field, button, and label.

So, we need to have:

public ApplPanel extends Panel implements ActionListener {
	TextField name;
	Button OK;
	String s = "";

	public AppPanel(){
		name = new TextField(20);
		b = new Button("ok");
		setSize(300,400);
		b.addActionListener(this);
		add(name);
		add(b);
	}

	public actionPerformed(ActionEvent evt){
		s = "Hello" + name.getText();
		repaint();
	}

	public void paint(Graphics g){
		g.drawString(s, 20, 100);
	}

Great, so now let's use the panel. We will use it in a frame session

public class B extends Frame {
	public B() {
		add(new helloCirclePanel());
		setSize(400, 500);
		setVisible(true);
	}

	public static void main(String[] args){
		new B();
	}
}

So my code looks like this:

helloCirclePanel.java

winpanel.java

Yay.

Now let's make an app panel

public class A extends Applet {
	public void init(){
		add(new AppPanel());
	}
}

Since in your applet you are using the flow layout, even if you set your panel to a certain size, the layout manager decides how big your panel is going to be for you. So we need to set it to NOT flow layout. We need to use border layout. So now we:

public class A extends Applet {
	public void init(){
		setLayout(new BorderLayout());
		add(new AppPanel());
	}
}

Inheritance allows you to customize alot of things. It would be horrible to have to keep duplicating your code over and over again.

When we want a window to close, we have to implement the windows listener and have like 7 methods. But instead, let's say we have a class with alot of methods. We can extend that class.

public class Xue implements WindowListener{
	All 7 window listnere methods with empty bodies
}

The compile it, and have all those empty bodies there

------------ Frame X{
	addWindowListener(
		new Xue(){
			public void windowClosing(...){
				dispose();
			}
		}
	);
}

But there is no reason to do this, because Sun has already thought about this. It is called windowAdapter. It does exactly the same thing. This is inheritance, or override.

So now we want to add to our frame version to make it close the window.

Now, up to this point when we make a listener, we say "this", because we want to use the applet itself. But in this case we created a new class. So it is not the frame itself that is the listener. So let's make it so that the panel is only displaying things and another class is listening to the panel with the "backend" code there. So try writing another class that implements the action listner and performs the action. Then on our panel, we addActionListener and new blah(). Then when you click it, it should perform the action. Try making just a button that calls an action that prints out to the console.

Next week will talk about "interface".

Instructor's Notes