Java Reference
In-Depth Information
Display 18.2
A Window Listener (part 2 of 3)
35
public void windowDeactivated(WindowEvent e)
36
{}
37
} //End of inner class CheckOnExit
38
private class ConfirmWindow extends JFrame implements ActionListener
39
{
40
public ConfirmWindow()
Another inner class.
41
{
42
setSize(SMALL_WIDTH, SMALL_HEIGHT);
43
getContentPane().setBackground(Color.YELLOW);
44
setLayout( new BorderLayout());
45
JLabel confirmLabel = new JLabel(
46
"Are you sure you want to exit?");
add(confirmLabel, BorderLayout.CENTER);
47
48
JPanel buttonPanel = new JPanel();
49
buttonPanel.setBackground(Color.ORANGE);
50
buttonPanel.setLayout( new FlowLayout());
51
JButton exitButton = new JButton("Yes");
52
exitButton.addActionListener( this );
53
buttonPanel.add(exitButton);
54
JButton cancelButton = new JButton("No");
55
cancelButton.addActionListener( this );
56
buttonPanel.add(cancelButton);
add(buttonPanel, BorderLayout.SOUTH);
57
58
}
59
public void actionPerformed(ActionEvent e)
60
{
61
String actionCommand = e.getActionCommand();
62
if (actionCommand.equals("Yes"))
63
System.exit(0);
64
else if (actionCommand.equals("No"))
65
dispose(); //Destroys only the ConfirmWindow.
66
else
System.out.println("Unexpected Error in Confirm Window.");
67
68
}
69
} //End of inner class ConfirmWindow
Search WWH ::




Custom Search