Java Reference
In-Depth Information
center.add(go);
this.add(center, BorderLayout.CENTER);
this.add(hello, BorderLayout.SOUTH);
//Set up the event handling.
PrintHello listener = new PrintHello(hello, name);
go.addActionListener(listener);
}
}
import java.awt.*;
import java.awt.event.*;
public class PrintHello implements ActionListener
{
private Label label;
private TextField textField;
public PrintHello(Label s, TextField t)
{
label = s;
textField = t;
}
public void actionPerformed(ActionEvent a)
{
String name = textField.getText();
if(name != null && !(name.equals(“”)))
{
label.setText(“Hello, “ + name);
}
}
}
I want to make a few comments about this applet:
Most of the code of the HelloWorldApplet appears in the init() method,
which is overriding the init() method from the parent class Applet. The
Web browser invokes init() immediately after it creates an instance of
HelloWorldApplet. I could have used a constructor, but I wanted to
demonstrate the init() method.
■■
Within init(), the layout of the applet is changed to BorderLayout. (It
had FlowLayout by default.)
■■
A PrintHello object is listening for an ActionEvent from the Go button.
■■
Search WWH ::




Custom Search