public class ButtonDemo extends Frame { protected Button startButton, stopButton; public ButtonDemo() { super("Button demo"); // calls Frame constructor // which adds title to window setSize(400,200); // sets the size of the window // sets layout so objects added go from left to right // until fill up row and then go to next row setLayout(new FlowLayout()); // create two new buttons w/labels start and stop startButton = new Button("Start"); stopButton = new Button("Stop"); add(startButton); // add buttons to frame add(stopButton); // create an object to listen to both buttons: ButtonListener myButtonListener = new ButtonListener(); // tell buttons that myButtonListener should be notified startButton.addActionListener(myButtonListener); stopButton.addActionListener(myButtonListener); } ... }Thus ButtonDemo is an extension of Frame (i.e., it represents a window) which has two buttons, startButton and stopButton.
The two method calls:
add(startButton); // add buttons to frame add(stopButton);add the two buttons to the frame according to the rules for FlowLayouts, which essentially just add items from left to right until it runs out of space and then starts a new row of items.
As we have seen, a Frame can also be assigned this layout manager by executing:
setLayout(new FlowLayout());
BorderLayout has two constructors:
public BorderLayout()public BorderLayout(int horizGap, int vertGap)
// leaves given gaps between components.
The layout manager of an applet or panel can be changed to BorderLayout by executing:
setLayout(new BorderLayout())
Components are added by using a more refined version of add:
Thus to add a button on the north, write
Note. all directions must begin with a capital letter or an error will occur.
Components added to "North" and "South" extend to the left and right edges, while those added "East" and "West" extend up and down to the components to the "North" and "South". The component in the "Center" fills the remaining space.
If one component is omitted, other components stretch to fill that space.
With the BorderLayout, components stretch to fill all of the container. As a result using this by itself can often make layouts look distorted.
public GridLayout(int rows, int cols)
// Construct layout w/ given rows and columnspublic GridLayout(int rows, int cols, int h, int v)
// Construct layout w/ given rows and columns, and
// horiz and vertical gaps of h and v between components
Executing
divides the frame or panel into 2 rows and 3 columns. When "add" method is executed, components are added from left to right first across the first row, then second row, etc.
The constructor for a panel is:
The event handling is built into Java and the programmer need not provide either the loop or queue. Instead events are delivered to objects which handle them.
All events inherit from Event from package java.awt. One subclass is AWTEvent. It and its subclasses can be found in the package java.awt.event.
For example, ActionEvents are sent to Action listeners, which are registered by executing an addActionListener(...) method.
An important feature of the Java 1.1. event model is that the container responsible for displaying a component (e.g., the frame containing a button) need not be responsible for handling events generated by that object. By assigning a listener to an object we can explicitly delegate responsibility for handling the events generated by the object.