Java Reference
In-Depth Information
Answers to Self-Test Exercises
1. All the methods in Display 18.1. If there is no particular action that you want
the method to perform, you can give the method an empty body.
2. The smaller window goes away but the larger window stays. This is the default action
for the close-window button and we did not change it for the smaller window.
3. dispose
4.
The import statements are the same as in Display 18.2. The rest of the definition fol-
lows. This definition is in the file
WindowListenerDemo3
on the accompanying CD.
extra code
on CD
public class
WindowListenerDemo3
extends
JFrame
implements
WindowListener
{
public static final int
WIDTH = 300;
//for main window
public static final int
HEIGHT = 200;
//for main window
public static final int
SMALL_WIDTH = 200;
//for confirm window
public static final int
SMALL_HEIGHT = 100;
//for confirm window
private class
ConfirmWindow
extends
JFrame
implements
ActionListener
{
public
ConfirmWindow()
{
setSize(SMALL_WIDTH, SMALL_HEIGHT);
getContentPane().setBackground(Color.YELLOW);
setLayout(
new
BorderLayout());
JLabel confirmLabel =
new
JLabel(
"Are you sure you want to exit?");
add(confirmLabel,
BorderLayout.CENTER);
JPanel buttonPanel =
new
JPanel();
buttonPanel.setBackground(Color.ORANGE);
buttonPanel.setLayout(
new
FlowLayout());
JButton exitButton =
new
JButton("Yes");
exitButton.addActionListener(
this
);
buttonPanel.add(exitButton);
JButton cancelButton =
new
JButton("No");
cancelButton.addActionListener(
this
);
buttonPanel.add(cancelButton);
add(buttonPanel,
BorderLayout.SOUTH);
}
public void
actionPerformed(ActionEvent e)
{