Java Reference
In-Depth Information
to be displayed on the screen. Thus, a window is a valuable resource that should be given
back to the system when it's no longer needed. Class Window (an indirect superclass of
JFrame ) declares method dispose for this purpose. When a Window is no longer needed in
an application, you should explicitly dispose of it. This can be done by calling the Window 's
dispose method or by calling method setDefaultCloseOperation with the argument
WindowConstants.DISPOSE_ON_CLOSE . Terminating an application also returns window
resources to the system. Using DO_NOTHING_ON_CLOSE indicates that the program will de-
termine what to do when the user attempts to close the window. For example, the program
might want to ask whether to save a file's changes before closing a window.
Displaying and Positioning Windows
By default, a window is not displayed on the screen until the program invokes the win-
dow's setVisible method (inherited from class java.awt.Component ) with a true argu-
ment. A window's size should be set with a call to method setSize (inherited from class
java.awt.Component ). The position of a window when it appears on the screen is speci-
fied with method setLocation (inherited from class java.awt.Component ).
Window Events
When the user manipulates the window, this action generates window events . Event lis-
teners are registered for window events with Window method addWindowListener . The
WindowListener interface provides seven window-event-handling methods— window-
Activated (called when the user makes a window the active window), windowClosed
(called after the window is closed), windowClosing (called when the user initiates closing
of the window), windowDeactivated (called when the user makes another window the ac-
tive window), windowDeiconified (called when the user restores a minimized window),
windowIconified (called when the user minimizes a window) and windowOpened (called
when a program first displays a window on the screen).
22.4 Using Menus with Frames
Menus are an integral part of GUIs. They allow the user to perform actions without un-
necessarily cluttering a GUI with extra components. In Swing GUIs, menus can be at-
tached only to objects of the classes that provide method setJMenuBar . Two such classes
are JFrame and JApplet . The classes used to declare menus are JMenuBar , JMenu , JMenu-
Item , JCheckBoxMenuItem and class JRadioButtonMenuItem .
Look-and-Feel Observation 22.1
Menus simplify GUIs because components can be hidden within them. These components
will be visible only when the user looks for them by selecting the menu.
Overview of Several Menu-Related Components
Class JMenuBar (a subclass of JComponent ) contains the methods necessary to manage a
menu bar , which is a container for menus. Class JMenu (a subclass of javax.swing.JMenu-
Item ) contains the methods necessary for managing menus. Menus contain menu items
and are added to menu bars or to other menus as submenus. When a menu is clicked, it
expands to show its list of menu items.
 
 
Search WWH ::




Custom Search