Module java.desktop
Package java.awt

Class Frame

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible
Direct Known Subclasses:
JFrame

public class Frame extends Window implements MenuContainer
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 getInsets method, however, since these dimensions are platform-dependent, a valid insets value cannot be obtained until the frame is made displayable by either calling pack or show. 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).

The default layout for a frame is BorderLayout.

A frame may have its native decorations (i.e. Frame and Titlebar) turned off with setUndecorated. This can only be done while the frame is not displayable.

In a multi-screen environment, you can create a Frame on a different screen device by constructing the Frame with Frame(GraphicsConfiguration) or Frame(String title, GraphicsConfiguration). The GraphicsConfiguration object is one of the GraphicsConfiguration objects of the target screen device.

In a virtual device multi-screen environment in which the desktop area could span multiple physical screen devices, the bounds of all configurations are relative to the virtual-coordinate system. The origin of the virtual-coordinate system is at the upper left-hand corner of the primary physical screen. Depending on the location of the primary screen in the virtual device, negative coordinates are possible, as shown in the following figure.

Diagram of virtual device
 encompassing three physical screens and one primary physical screen. The
 primary physical screen shows (0,0) coords while a different physical screen
 shows (-80,-100) coords.

In such an environment, when calling setLocation, you must pass a virtual coordinate to this method. Similarly, calling getLocationOnScreen on a Frame returns virtual device coordinates. Call the getBounds method of a GraphicsConfiguration to find its origin in the virtual coordinate system.

The following code sets the location of the Frame at (10, 10) relative to the origin of the physical screen of the corresponding GraphicsConfiguration. If the bounds of the GraphicsConfiguration is not taken into account, the Frame location would be set at (10, 10) relative to the virtual-coordinate system and would appear on the primary physical screen, which might be different from the physical screen of the specified GraphicsConfiguration.

      Frame f = new Frame(GraphicsConfiguration gc);
      Rectangle bounds = gc.getBounds();
      f.setLocation(10 + bounds.x, 10 + bounds.y);
 

Frames are capable of generating the following types of WindowEvents:

  • WINDOW_OPENED
  • WINDOW_CLOSING:
    If the program doesn't explicitly hide or dispose the window while processing this event, the window close operation is canceled.
  • WINDOW_CLOSED
  • WINDOW_ICONIFIED
  • WINDOW_DEICONIFIED
  • WINDOW_ACTIVATED
  • WINDOW_DEACTIVATED
  • WINDOW_GAINED_FOCUS
  • WINDOW_LOST_FOCUS
  • WINDOW_STATE_CHANGED
Since:
1.0
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • addNotify

      public void addNotify()
      Makes this Frame displayable by connecting it to a native screen resource. Making a frame displayable will cause any of its children to be made displayable. This method is called internally by the toolkit and should not be called directly by programs.
      Overrides:
      addNotify in class Window
      See Also:
    • getTitle

      public String getTitle()
      Gets the title of the frame. The title is displayed in the frame's border.
      Returns:
      the title of this frame, or an empty string ("") if this frame doesn't have a title.
      See Also:
    • setTitle

      public void setTitle(String title)
      Sets the title for this frame to the specified string.
      Parameters:
      title - the title to be displayed in the frame's border. A null value is treated as an empty string, "".
      See Also:
    • getIconImage

      public Image getIconImage()
      Returns the image to be displayed as the icon for this frame.

      This method is obsolete and kept for backward compatibility only. Use Window.getIconImages() instead.

      If a list of several images was specified as a Window's icon, this method will return the first item of the list.

      Returns:
      the icon image for this frame, or null if this frame doesn't have an icon image.
      See Also:
    • getMenuBar

      public MenuBar getMenuBar()
      Gets the menu bar for this frame.
      Returns:
      the menu bar for this frame, or null if this frame doesn't have a menu bar.
      See Also:
    • setMenuBar

      public void setMenuBar(MenuBar mb)
      Sets the menu bar for this frame to the specified menu bar.
      Parameters:
      mb - the menu bar being set. If this parameter is null then any existing menu bar on this frame is removed.
      See Also:
    • isResizable

      public boolean isResizable()
      Indicates whether this frame is resizable by the user. By default, all frames are initially resizable.
      Returns:
      true if the user can resize this frame; false otherwise.
      See Also:
    • setResizable

      public void setResizable(boolean resizable)
      Sets whether this frame is resizable by the user.
      Parameters:
      resizable - true if this frame is resizable; false otherwise.
      See Also:
    • setState

      public void setState(int state)
      Sets the state of this frame (obsolete).

      In older versions of JDK a frame state could only be NORMAL or ICONIFIED. Since JDK 1.4 set of supported frame states is expanded and frame state is represented as a bitwise mask.

      For compatibility with applications developed earlier this method still accepts Frame.NORMAL and Frame.ICONIFIED only. The iconic state of the frame is only changed, other aspects of frame state are not affected by this method. If the state passed to this method is neither Frame.NORMAL nor Frame.ICONIFIED the method performs no actions at all.

      Note that if the state is not supported on a given platform, neither the state nor the return value of the getState() method will be changed. The application may determine whether a specific state is supported via the Toolkit.isFrameStateSupported(int) method.

      If the frame is currently visible on the screen (the Window.isShowing() method returns true), the developer should examine the return value of the WindowEvent.getNewState() method of the WindowEvent received through the WindowStateListener to determine that the state has actually been changed.

      If the frame is not visible on the screen, the events may or may not be generated. In this case the developer may assume that the state changes immediately after this method returns. Later, when the setVisible(true) method is invoked, the frame will attempt to apply this state. Receiving any WindowEvent.WINDOW_STATE_CHANGED events is not guaranteed in this case also.

      Parameters:
      state - either Frame.NORMAL or Frame.ICONIFIED.
      See Also:
    • setExtendedState

      public void setExtendedState(int state)
      Sets the state of this frame. The state is represented as a bitwise mask.
      • NORMAL
        Indicates that no state bits are set.
      • ICONIFIED
      • MAXIMIZED_HORIZ
      • MAXIMIZED_VERT
      • MAXIMIZED_BOTH
        Concatenates MAXIMIZED_HORIZ and MAXIMIZED_VERT.

      Note that if the state is not supported on a given platform, neither the state nor the return value of the getExtendedState() method will be changed. The application may determine whether a specific state is supported via the Toolkit.isFrameStateSupported(int) method.

      If the frame is currently visible on the screen (the Window.isShowing() method returns true), the developer should examine the return value of the WindowEvent.getNewState() method of the WindowEvent received through the WindowStateListener to determine that the state has actually been changed.

      If the frame is not visible on the screen, the events may or may not be generated. In this case the developer may assume that the state changes immediately after this method returns. Later, when the setVisible(true) method is invoked, the frame will attempt to apply this state. Receiving any WindowEvent.WINDOW_STATE_CHANGED events is not guaranteed in this case also.

      Parameters:
      state - a bitwise mask of frame state constants
      Since:
      1.4
      See Also:
    • getState

      public int getState()
      Gets the state of this frame (obsolete).

      In older versions of JDK a frame state could only be NORMAL or ICONIFIED. Since JDK 1.4 set of supported frame states is expanded and frame state is represented as a bitwise mask.

      For compatibility with old programs this method still returns Frame.NORMAL and Frame.ICONIFIED but it only reports the iconic state of the frame, other aspects of frame state are not reported by this method.

      Returns:
      Frame.NORMAL or Frame.ICONIFIED.
      See Also:
    • getExtendedState

      public int getExtendedState()
      Gets the state of this frame. The state is represented as a bitwise mask.
      • NORMAL
        Indicates that no state bits are set.
      • ICONIFIED
      • MAXIMIZED_HORIZ
      • MAXIMIZED_VERT
      • MAXIMIZED_BOTH
        Concatenates MAXIMIZED_HORIZ and MAXIMIZED_VERT.
      Returns:
      a bitwise mask of frame state constants
      Since:
      1.4
      See Also:
    • setMaximizedBounds

      public void setMaximizedBounds(Rectangle bounds)
      Sets the maximized bounds for this frame.

      When a frame is in maximized state the system supplies some defaults bounds. This method allows some or all of those system supplied values to be overridden.

      If bounds is null, accept bounds supplied by the system. If non-null you can override some of the system supplied values while accepting others by setting those fields you want to accept from system to Integer.MAX_VALUE.

      Note, the given maximized bounds are used as a hint for the native system, because the underlying platform may not support setting the location and/or size of the maximized windows. If that is the case, the provided values do not affect the appearance of the frame in the maximized state.

      Parameters:
      bounds - bounds for the maximized state
      Since:
      1.4
      See Also:
    • getMaximizedBounds

      public Rectangle getMaximizedBounds()
      Gets maximized bounds for this frame. Some fields may contain Integer.MAX_VALUE to indicate that system supplied values for this field must be used.
      Returns:
      maximized bounds for this frame; may be null
      Since:
      1.4
      See Also:
    • setUndecorated

      public void setUndecorated(boolean undecorated)
      Disables or enables decorations for this frame.

      This method can only be called while the frame is not displayable. To make this frame decorated, it must be opaque and have the default shape, otherwise the IllegalComponentStateException will be thrown. Refer to Window.setShape(java.awt.Shape), Window.setOpacity(float) and Window.setBackground(java.awt.Color) for details

      Parameters:
      undecorated - true if no frame decorations are to be enabled; false if frame decorations are to be enabled
      Throws:
      IllegalComponentStateException - if the frame is displayable
      IllegalComponentStateException - if undecorated is false, and this frame does not have the default shape
      IllegalComponentStateException - if undecorated is false, and this frame opacity is less than 1.0f
      IllegalComponentStateException - if undecorated is false, and the alpha value of this frame background color is less than 1.0f
      Since:
      1.4
      See Also:
    • isUndecorated

      public boolean isUndecorated()
      Indicates whether this frame is undecorated. By default, all frames are initially decorated.
      Returns:
      true if frame is undecorated; false otherwise.
      Since:
      1.4
      See Also:
    • remove

      public void remove(MenuComponent m)
      Removes the specified menu bar from this frame.
      Specified by:
      remove in interface MenuContainer
      Overrides:
      remove in class Component
      Parameters:
      m - the menu component to remove. If m is null, then no action is taken
      See Also:
    • removeNotify

      public void removeNotify()
      Makes this Frame undisplayable by removing its connection to its native screen resource. Making a Frame undisplayable will cause any of its children to be made undisplayable. This method is called by the toolkit internally and should not be called directly by programs.
      Overrides:
      removeNotify in class Container
      See Also:
    • paramString

      protected String paramString()
      Returns a string representing the state of this Frame. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.
      Overrides:
      paramString in class Container
      Returns:
      the parameter string of this frame
    • setCursor

      @Deprecated public void setCursor(int cursorType)
      Deprecated.
      As of JDK version 1.1, replaced by Component.setCursor(Cursor).
      Sets the cursor for this frame to the specified type.
      Parameters:
      cursorType - the cursor type
    • getCursorType

      @Deprecated public int getCursorType()
      Deprecated.
      As of JDK version 1.1, replaced by Component.getCursor().
      Returns:
      the cursor type for this frame
    • getFrames

      public static Frame[] getFrames()
      Returns an array of all Frames created by this application. If called from an applet, the array includes only the Frames accessible by that applet.

      Warning: this method may return system created frames, such as a shared, hidden frame which is used by Swing. Applications should not assume the existence of these frames, nor should an application assume anything about these frames such as component positions, LayoutManagers or serialization.

      Note: To obtain a list of all ownerless windows, including ownerless Dialogs (introduced in release 1.6), use Window.getOwnerlessWindows.

      Returns:
      the array of all Frames created by this application
      Since:
      1.2
      See Also:
    • getAccessibleContext

      public AccessibleContext getAccessibleContext()
      Gets the AccessibleContext associated with this Frame. For frames, the AccessibleContext takes the form of an AccessibleAWTFrame. A new AccessibleAWTFrame instance is created if necessary.
      Specified by:
      getAccessibleContext in interface Accessible
      Overrides:
      getAccessibleContext in class Window
      Returns:
      an AccessibleAWTFrame that serves as the AccessibleContext of this Frame
      Since:
      1.3