AWT Frame Class

  • Post author:
  • Post category:AWT
  • Post comments:0 Comments
Frame class

Introduction

The Frame class is a top-level window with a border and title. It uses BorderLayout as the default layout manager.

Class declaration

Following is the declaration for java.awt.Frame class:

public class Frame
   extends Window
      implements MenuContainer

Field

Following are the fields for java.awt.Frame class:

  • static float BOTTOM_ALIGNMENT — Ease-of-use constant for getAlignmentY.
  • static int CROSSHAIR_CURSOR — Deprecated. replaced by Cursor.CROSSHAIR_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 HAND_CURSOR — Deprecated. replaced by Cursor.HAND_CURSOR.
  • 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_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 MOVE_CURSOR — Deprecated. replaced by Cursor.MOVE_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 NORMAL — Frame is in the “normal” state.
  • 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 SE_RESIZE_CURSOR — Deprecated. replaced by Cursor.SE_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 W_RESIZE_CURSOR — Deprecated. replaced by Cursor.W_RESIZE_CURSOR.
  • static int WAIT_CURSOR — Deprecated. replaced by Cursor.WAIT_CURSOR.

Class constructors

S.N.Constructor & Description
1Frame()Constructs a new instance of Frame that is initially invisible.
2Frame(GraphicsConfiguration gc)Constructs a new, initially invisible Frame with the specified GraphicsConfiguration.
3Frame(String title)Constructs a new, initially invisible Frame object with the specified title.
4Frame(String title, GraphicsConfiguration gc)Constructs a new, initially invisible Frame object with the specified title and a GraphicsConfiguration.

Class methods

S.N.Method & Description
1void addNotify()Makes this Frame displayable by connecting it to a native screen resource.
2AccessibleContext getAccessibleContext()Gets the AccessibleContext associated with this Frame.
3int getCursorType()Deprecated. As of JDK version 1.1, replaced by Component.getCursor().
4int getExtendedState()Gets the state of this frame.
5static Frame[] getFrames()Returns an array of all Frames created by this application.
6Image getIconImage()Returns the image to be displayed as the icon for this frame.
7Rectangle getMaximizedBounds()Gets maximized bounds for this frame.
8MenuBar getMenuBar()Gets the menu bar for this frame.
9int getState()Gets the state of this frame (obsolete).
10String getTitle()Gets the title of the frame.
11boolean isResizable()Indicates whether this frame is resizable by the user.
12boolean isUndecorated()Indicates whether this frame is undecorated.
13protected String paramString()Returns a string representing the state of this Frame.
14void remove(MenuComponent m)Removes the specified menu bar from this frame.
15void removeNotify()Makes this Frame undisplayable by removing its connection to its native screen resource.
16void setCursor(int cursorType)Deprecated. As of JDK version 1.1, replaced by Component.setCursor(Cursor).
17void setExtendedState(int state)Sets the state of this frame.
18void setIconImage(Image image)Sets the image to be displayed as the icon for this window.
19void setMaximizedBounds(Rectangle bounds)Sets the maximized bounds for this frame.
20void setMenuBar(MenuBar mb)Sets the menu bar for this frame to the specified menu bar.
21void setResizable(boolean resizable)Sets whether this frame is resizable by the user.
22void setState(int state)Sets the state of this frame (obsolete).
23void setTitle(String title)Sets the title for this frame to the specified string.
24void setUndecorated(boolean undecorated)Disables or enables decorations for this frame.

Methods inherited

This class inherits methods from the following classes:

  • java.awt.Window
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

Frame Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > adglob > gui >AwtContainerDemo.java

package com.adglob.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 Adglob 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\adglob\gui\AwtContainerDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\AWT>java com.adglob.gui.AwtContainerDemo

Verify the following output

Frame class

Previous Page:-Click Here

Leave a Reply