Example Java applet (Widgets) Dr. David Matuszek, dave@acm.org,Villanova University |
This applet contains an example of just about every type of AWT component you are likely to ever need. In addition, it allows you to switch from a FlowLayout to a BorderLayout. Try it out! (By the way, I haven't provided a way to change back to a FlowLayout, but you can do that by reloading this page.)
In the BorderLayout, the program sets the background colors so that you can see where NORTH, SOUTH, etc. are located.
In this example I make extensive use of "anonymous inner classes." For example,
clickMe.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
showStatus ("Button clicked.");
}
});
Here, ActionListener
is an interface (not a
class!) that requires you to supply the method public void actionPerformed (ActionEvent event)
.
In the above code, everything in the outermost pair of parentheses says, "Create a new
(anonymous) class that implements ActionListener ()
, and then the code
inside the {}
braces is the class definition.
Finally, here's the source code. Please feel free to copy and use whatever portions of it you think might be handy.