AWT Frame Class

The class Frame is a top level window with border and title. It uses BorderLayout as default layout manager.
Following is the declaration for java.awt.Frame class:
public class Frame
   extends Window
      implements MenuContainer
Following are the fields for java.awt.Frame class:
static float BOTTOM_ALIGNMENT -- Ease-of-use constant for getAlignmentY.
static float BOTTOM_ALIGNMENT -- Ease-of-use constant for getAlignmentY.
static int CROSSHAIR_CURSOR  -- Deprecated. replaced by Cursor.CROSSHAIR_CURSOR.
static int CROSSHAIR_CURSOR  -- Deprecated. replaced by Cursor.CROSSHAIR_CURSOR.
static int DEFAULT_CURSOR  -- Deprecated. replaced by Cursor.DEFAULT_CURSOR.
static int DEFAULT_CURSOR  -- Deprecated. replaced by Cursor.DEFAULT_CURSOR.
static int E_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.E_RESIZE_CURSOR.
static int E_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.E_RESIZE_CURSOR.
static int HAND_CURSOR  -- Deprecated. replaced by Cursor.HAND_CURSOR.
static int HAND_CURSOR  -- Deprecated. replaced by Cursor.HAND_CURSOR.
static int ICONIFIED  -- This state bit indicates that frame is iconified.
static int ICONIFIED  -- This state bit indicates that frame is iconified.
static int MAXIMIZED_BOTH  -- This state bit mask indicates that frame is fully maximized (that is both horizontally and vertically).
static int MAXIMIZED_BOTH  -- This state bit mask indicates that frame is fully maximized (that is both horizontally and vertically).
static int MAXIMIZED_HORIZ  -- This state bit indicates that frame is maximized in the horizontal direction.
static int MAXIMIZED_HORIZ  -- This state bit indicates that frame is maximized in the horizontal direction.
static int MAXIMIZED_VERT  -- This state bit indicates that frame is maximized in the vertical direction.
static int MAXIMIZED_VERT  -- This state bit indicates that frame is maximized in the vertical direction.
static int MOVE_CURSOR  -- Deprecated. replaced by Cursor.MOVE_CURSOR.
static int MOVE_CURSOR  -- Deprecated. replaced by Cursor.MOVE_CURSOR.
static int N_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.N_RESIZE_CURSOR.
static int N_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.N_RESIZE_CURSOR.
static int NE_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.NE_RESIZE_CURSOR.
static int NE_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.NE_RESIZE_CURSOR.
static int NORMAL  -- Frame is in the "normal" state.

static int NORMAL  -- Frame is in the "normal" state.

static int NW_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.NW_RESIZE_CURSOR.
static int NW_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.NW_RESIZE_CURSOR.
static int S_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.S_RESIZE_CURSOR.
static int S_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.S_RESIZE_CURSOR.
static int SE_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.SE_RESIZE_CURSOR.
static int SE_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.SE_RESIZE_CURSOR.
static int SW_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.SW_RESIZE_CURSOR.
static int SW_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.SW_RESIZE_CURSOR.
static int TEXT_CURSOR  -- Deprecated. replaced by Cursor.TEXT_CURSOR.
static int TEXT_CURSOR  -- Deprecated. replaced by Cursor.TEXT_CURSOR.
static int W_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.W_RESIZE_CURSOR.
static int W_RESIZE_CURSOR  -- Deprecated. replaced by Cursor.W_RESIZE_CURSOR.
static int WAIT_CURSOR  -- Deprecated. replaced by Cursor.WAIT_CURSOR.
static int WAIT_CURSOR  -- Deprecated. replaced by Cursor.WAIT_CURSOR.
Frame() 
Constructs a new instance of Frame that is initially invisible.
Frame(GraphicsConfiguration gc) 
Constructs a new, initially invisible Frame with the specified GraphicsConfiguration.
Frame(String title) 
Constructs a new, initially invisible Frame object with the specified title.
Frame(String title, GraphicsConfiguration gc) 
Constructs a new, initially invisible Frame object with the specified title and a GraphicsConfiguration.
void addNotify() 
Makes this Frame displayable by connecting it to a native screen resource.
AccessibleContext	getAccessibleContext()
Gets the AccessibleContext associated with this Frame.
int getCursorType() 
Deprecated. As of JDK version 1.1, replaced by Component.getCursor().
int getExtendedState() 
Gets the state of this frame.
static Frame[]	getFrames() 
Returns an array of all Frames created by this application.
Image getIconImage() 
Returns the image to be displayed as the icon for this frame.
Rectangle getMaximizedBounds() 
Gets maximized bounds for this frame.
MenuBar getMenuBar() 
Gets the menu bar for this frame.
int getState() 
Gets the state of this frame (obsolete).
String	getTitle()
Gets the title of the frame.
boolean isResizable() 
Indicates whether this frame is resizable by the user.
boolean isUndecorated() 
Indicates whether this frame is undecorated.
protected String paramString() 
Returns a string representing the state of this Frame.
void remove(MenuComponent m) 
Removes the specified menu bar from this frame.
void removeNotify() 
Makes this Frame undisplayable by removing its connection to its native screen resource.
void setCursor(int cursorType) 
Deprecated. As of JDK version 1.1, replaced by Component.setCursor(Cursor).
void setExtendedState(int state) 
Sets the state of this frame.
void setIconImage(Image image) 
Sets the image to be displayed as the icon for this window.
void setMaximizedBounds(Rectangle bounds) 
Sets the maximized bounds for this frame.
void setMenuBar(MenuBar mb) 
Sets the menu bar for this frame to the specified menu bar.
void setResizable(boolean resizable) 
Sets whether this frame is resizable by the user.
void setState(int state) 
Sets the state of this frame (obsolete).
void setTitle(String title) 
Sets the title for this frame to the specified string.
void setUndecorated(boolean undecorated) 
Disables or enables decorations for this frame.
This class inherits methods from the following classes:
java.awt.Window
java.awt.Window
java.awt.Container
java.awt.Container
java.awt.Component
java.awt.Component
java.lang.Object
java.lang.Object
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtContainerDemo {
   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;
   private Label msglabel;

   public AwtContainerDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtContainerDemo  awtContainerDemo = new AwtContainerDemo();          
      awtContainerDemo.showFrameDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);
   
      msglabel = new Label();
      msglabel.setAlignment(Label.CENTER);
      msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   
   private void showFrameDemo(){
      headerLabel.setText("Container in action: Frame");   

      final Frame frame = new Frame();
      frame.setSize(300, 300);
      frame.setLayout(new FlowLayout());       
      frame.add(msglabel);
      frame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            frame.dispose();
         }        
      });    
      Button okButton = new Button("Open a Frame");

      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            statusLabel.setText("A Frame shown to the user.");
            frame.setVisible(true);
         }
      });
      controlPanel.add(okButton);

      mainFrame.setVisible(true);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT  and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtContainerDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtContainerDemo
Verify the following output

    13 Lectures 
     2 hours 

 EduOLC
 Print
 Add Notes
 Bookmark this page