Your shopping cart is empty!
A Frame is a top-level window with a title and a border. The size of the frame includes any area designated for the border. The dimensions of the border area may be obtained using the getInsetsmethod. Since the border area is included in the overall size of the frame, the border effectively obscures a portion of the frame, constraining the area available for rendering and/or displaying subcomponents to the rectangle which has an upper-left corner location of (insets.left, insets.top), and has a size of width - (insets.left + insets.right)by height - (insets.top + insets.bottom).
width - (insets.left + insets.right)
height - (insets.top + insets.bottom)
A frame, implemented as an instance of the JFrame class, is a window that has decorations such as a border, a title, and supports button components that close or iconify the window. Applications with a GUI usually include at least one frame. Applets sometimes use frames, as well.
To make a window that is dependent on another window — disappearing when the other window is iconified, for example — use a dialog instead of frame.. To make a window that appears within another window, use an internal frame.
Here is a picture of the extremely plain window created by the FrameDemodemonstration application. You can find the source code in FrameDemo.java. You can run FrameDemo ( download JDK 6 or later).
The following FrameDemocode shows how to create and set up a frame.
//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//2. Optional: What happens when the frame closes?
//3. Create components and put them in the frame.
//4. Size the frame.
//5. Show it.
Here are some details about the code:
For frames that have menus, you'd typically add the menu bar to the frame here using the setJMenuBarmethod. See How to Use Menus for details.
This example does not set the frame location, but it is easy to do so using either the setLocationRelativeToor setLocationmethod. For example, the following code centers a frame onscreen:
By default, window decorations are supplied by the native window system. However, you can request that the look-and-feel provide the decorations for a frame. You can also specify that the frame have no window decorations at all, a feature that can be used on its own, or to provide your own decorations, or with full-screen exclusive mode.
Besides specifying who provides the window decorations, you can also specify which icon is used to represent the window. Exactly how this icon is used depends on the window system or look and feel that provides the window decorations. If the window system supports minimization, then the icon is used to represent the minimized window. Most window systems or look and feels also display the icon in the window decorations. A typical icon size is 16x16 pixels, but some window systems use other sizes.
The following snapshots show three frames that are identical except for their window decorations. As you can tell by the appearance of the button in each frame, all three use the Java look and feel. The first uses decorations provided by the window system, which happen to be Microsoft Windows, but could as easily be any other system running the Java platform.The second and third use window decorations provided by the Java look and feel. The third frame uses Java look and feel window decorations, but has a custom icon.
Here is an example of creating a frame with a custom icon and with window decorations provided by the look and feel:
//Ask for window decorations provided by the look and feel.
//Create the frame.
JFrame frame = new JFrame("A window");
//Set the frame icon to an image loaded from a file.
As the preceding code snippet implies, you must invoke the setDefaultLookAndFeelDecoratedmethod before creating the frame whose decorations you wish to affect. The value you set with setDefaultLookAndFeelDecoratedis used for all subsequently created JFrames. You can switch back to using window system decorations by invoking JFrame.setDefaultLookAndFeelDecorated(false). Some look and feels might not support window decorations; in this case, the window system decorations are used.
The full source code for the application that creates the frames pictured above is in FrameDemo2.java. Besides showing how to choose window decorations, FrameDemo2 also shows how to disable all window decorations and gives an example of positioning windows. It includes two methods that create the Imageobjects used as icons — one is loaded from a file, and the other is painted from scratch.
The argument to setDefaultCloseOperationmust be one of the following values, the first three of which are defined in the WindowConstants interface (implemented by JFrame, JInternalPane, and JDialog):
For more information about handling window-closing events, see How to Write Window Listeners. Besides handling window-closing events, window listeners can also react to other window state changes, such as iconification and activation.
The following tables list the commonly used JFrameconstructors and methods. Other methods you might want to call are defined by the java.awt.Frame, java.awt.Window, and java.awt.Component classes, from which JFramedescends.
Because each JFrameobject has a root pane, frames have support for interposing input and painting behavior in front of the frame children, placing children on different "layers", and for Swing menu bars. These topics are introduced in Using Top-Level Containers and explained in detail in How to Use Root Panes.
The API for using frames falls into these categories:
All of the standalone applications in this trail use JFrame. The following table lists a few and tells you where each is discussed.