Java Reference
In-Depth Information
JFrame f = new JFrame();
getContentPane().add(new JButton("Ok")); // Add the button
to the frame's content pane.
f.pack();
f.setVisible(true);
RootPaneContainer complements getContentPane() with a void
setContentPane(Container content) methodthatyou'llfindhelpfulwhen
you want to replace the current content pane with a new content pane. The following
exampledemonstrates setContentPane() bycreatinganewpanel,populatingthe
panel(asdescribedbythecomment),andusing setContentPane() toreplacethe
existing content pane with this panel:
JFrame f = new JFrame();
JPanel pnl = new JPanel();
// Populate the panel.
f.setContentPane(pnl);
Tip Becausetheglasspaneispaintedlast,youcandrawovertheGUI.Also,because
eventsarefirstsenttotheglasspane,youcanusethispanetoblockmouseandother
events from reaching the GUI.
JFrame declares a void setDefaultCloseOperation(int opera-
tion) method for specifying the operation that occurs by default when the user
choosestoclosethiswindow.Theargumentpassedto operation isoneofthefollow-
ingconstants(declaredinthe javax.swing.WindowConstants interface,which
JFrame and JDialog implement):
DO_NOTHING_ON_CLOSE :Don'tdoanything;requiretheprogramtohandle
the operation in the windowClosing() method of a registered Win-
dowListener object. This operation is equivalent to what you would do in
AWT as discussed earlier.
HIDE_ON_CLOSE : Automatically hide the frame window after invoking any
registered WindowListener objects. This is the default operation.
DISPOSE_ON_CLOSE :Automaticallyhideanddisposeoftheframewindow
after invoking any registered WindowListener objects.
EXIT_ON_CLOSE (alsodeclaredin JFrame ):Exittheapplicationvia Sys-
tem.exit() .
Search WWH ::




Custom Search