14. 80. 7. Extending JFrame: Closing Frames by Default |
|
When extending JFrame, this class has two important protected methods: |
protected void frameInit()
protected JRootPane createRootPane()
|
|
import javax.swing.JFrame;
class ExitableJFrame extends JFrame {
public ExitableJFrame() {
}
public ExitableJFrame(String title) {
super(title);
}
protected void frameInit() {
super.frameInit();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class ExitableJFrameTest {
public static void main(String[] a) {
JFrame frame = new ExitableJFrame("Adornment Example");
frame.setSize(300, 100);
frame.setVisible(true);
}
}
|
|
If you do override the frameInit() method of JFrame, remember to call super.frameInit() first,
to initialize the default behaviors. |