位置:首頁 > Java技術 > AWT > Frame類

Frame類

類框架(Frame )是一個頂層窗口邊框和標題。它使用BorderLayout作為默認的布局管理器。

類的聲明

以下是的聲明的java.awt.Frame類:

public class Frame
   extends Window
      implements MenuContainer

字段域

以下java.awt.Frame類的字段:

  • 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.

類的構造函數

S.N. 構造函數與說明
1 Frame() 
Constructs a new instance of Frame that is initially invisible.
2 Frame(GraphicsConfiguration gc) 
Constructs a new, initially invisible Frame with the specified GraphicsConfiguration.
3 Frame(String title) 
Constructs a new, initially invisible Frame object with the specified title.
4 Frame(String title, GraphicsConfiguration gc) 
Constructs a new, initially invisible Frame object with the specified title and a GraphicsConfiguration.

類方法

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

繼承的方法

這個類繼承的方法從以下類:

  • java.awt.Window

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

Frame 實例

選擇使用任何編輯器創建以下java程序 D:/ > AWT > com > yiibai > gui >

AwtContainerDemo
package com.yiibai.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);  
   }
}

編譯程序,使用命令提示符。進入到D:/> AWT,然後鍵入以下命令。

D:AWT>javac comyiibaiguiAwtContainerDemo.java

如果冇有錯誤出現,這意味著編譯成功。使用下麵的命令來運行程序。

D:AWT>java com.yiibai.gui.AwtContainerDemo

驗證下麵的輸出

AWT Frame