Java Reference
In-Depth Information
3.
Save the ExitButton source code.
To add the ExitButton to UsefulFrame (i.e., to relate them through composition), we must create an ExitButton
object in UsefulFrame and then add it to UsefulFrame.
4.
Add the following statement immediately after the UsefulFrame class header:
private
ExitButton eb=
new
ExitButton();
This creates an ExitButton class variable called eb and assigns an ExitButton object to eb.
5.
Add the following statement to the constructor, then format, and save the source code.
this
.add(eb);
This statement adds the ExitButton object to UsefulFrame. The UsefulFrame executable source code should look
like the following:
package
c4;
import
java.awt.Frame;
import
java.awt.event.WindowListener;
import
java.awt.event.WindowEvent;
public class
UsefulFrame
extends
Frame
implements
WindowListener {
private
ExitButton eb=
new
ExitButton();
public
UsefulFrame () {
this
.setBounds(350, 200, 400, 400);
this
.setLayout(
null
);
this
.addWindowListener(
this
);
this
.add(eb);
}
public void
windowClosing(WindowEvent e) {
this
.dispose();
}
public void
windowClosed(WindowEvent e) {}
public void
windowDeiconified(WindowEvent e) {}
public void
windowIconified(WindowEvent e) {}
public void
windowOpened(WindowEvent e) {}
public void
windowActivated(WindowEvent e) {}
public void
windowDeactivated(WindowEvent e) {}
}
6.
Run EmployeeApp.
The frame should look like Figure
4-2
.