- AWT 教程
- AWT - 首頁
- AWT - 概述
- AWT - 環境
- AWT - 控制元件
- AWT - 事件處理
- AWT - 事件類
- AWT - 事件監聽器
- AWT - 事件介面卡
- AWT - 佈局
- AWT - 容器
- AWT - 選單
- AWT - 圖形
- AWT 有用資源
- AWT - 快速指南
- AWT - 有用資源
- AWT - 討論
AWT 視窗類
介紹
Window 類是一個頂級視窗,沒有邊框也沒有選單欄。它使用 BorderLayout 作為預設佈局管理器。
類宣告
以下是 java.awt.Window 類的宣告
public class Window
extends Container
implements Accessible
類建構函式
| 序號 | 建構函式 & 描述 |
|---|---|
| 1 | Window(Frame owner) 構造一個新的、最初不可見的視窗,並指定 Frame 作為其所有者。 |
| 2 | Window(Window owner) 構造一個新的、最初不可見的視窗,並指定 Window 作為其所有者。 |
| 3 | Window(Window owner, GraphicsConfiguration gc) 構造一個新的、最初不可見的視窗,並指定所有者 Window 和螢幕裝置的 GraphicsConfiguration。 |
類方法
| 序號 | 方法 & 描述 |
|---|---|
| 1 | void addNotify() 透過建立與本地螢幕資源的連線,使此 Window 可顯示。 |
| 2 | void addPropertyChangeListener(PropertyChangeListener listener) 向監聽器列表新增 PropertyChangeListener。 |
| 3 | void add Property ChangeListener(String property Name, Property Change Listener listener) 向特定屬性的監聽器列表新增 PropertyChangeListener。 |
| 4 | void addWindowFocusListener(WindowFocusListener l) 新增指定的視窗焦點監聽器以接收來自此視窗的視窗事件。 |
| 5 | void addWindowListener(WindowListener l) 新增指定的視窗監聽器以接收來自此視窗的視窗事件。 |
| 6 | void addWindowStateListener(WindowStateListener l) 新增指定的視窗狀態監聽器以接收來自此視窗的視窗事件。 |
| 7 | void applyResourceBundle(ResourceBundle rb) 已棄用。從 J2SE 1.4 開始,由 Component.applyComponentOrientation 替換。 |
| 8 | void applyResourceBundle(String rbName) 已棄用。從 J2SE 1.4 開始,由 Component.applyComponentOrientation 替換。 |
| 9 | void createBufferStrategy(int numBuffers) 為此元件建立用於多緩衝的新策略。 |
| 10 | void createBufferStrategy(int numBuffers, BufferCapabilities caps) 為此元件建立具有所需緩衝功能的多緩衝新策略。 |
| 11 | void dispose() 釋放此 Window、其子元件及其所有擁有子級使用的所有本地螢幕資源。 |
| 12 | AccessibleContext getAccessibleContext() 獲取與此 Window 關聯的 AccessibleContext。 |
| 13 | BufferStrategy getBufferStrategy() 返回此元件使用的 BufferStrategy。 |
| 14 | boolean getFocusableWindowState() 返回此 Window 是否可以成為聚焦視窗,前提是它滿足 isFocusableWindow 中概述的其他要求。 |
| 15 | Container getFocusCycleRootAncestor() 始終返回 null,因為 Window 沒有祖先;它們代表 Component 層次結構的頂部。 |
| 16 | Component getFocusOwner() 如果此 Window 已聚焦,則返回具有焦點的此 Window 的子元件;否則返回 null。 |
| 17 | Set<AWTKeyStroke> getFocusTraversalKeys(int id) 獲取此 Window 的焦點遍歷鍵。 |
| 18 | GraphicsConfiguration getGraphicsConfiguration() 此方法返回此 Window 使用的 GraphicsConfiguration。 |
| 19 | List<Image> getIconImages() 返回要顯示為此視窗圖示的影像序列。 |
| 20 | InputContext getInputContext() 獲取此視窗的輸入上下文。 |
| 21 | <T extends EventListener> T[] getListeners(Class<T> listenerType) 返回當前在此 Window 上註冊為 FooListeners 的所有物件的陣列。 |
| 22 | Locale getLocale() 如果已設定語言環境,則獲取與此視窗關聯的 Locale 物件。 |
| 23 | Dialog.ModalExclusionType getModalExclusionType() 返回此視窗的模態排除型別。 |
| 24 | Component getMostRecentFocusOwner() 返回此 Window 的子元件,當此 Window 聚焦時,該子元件將接收焦點。 |
| 25 | Window[] getOwnedWindows() 返回一個包含此視窗當前擁有的所有視窗的陣列。 |
| 26 | Window getOwner() 返回此視窗的所有者。 |
| 27 | static Window[] getOwnerlessWindows() 返回此應用程式建立的所有沒有所有者的 Window 的陣列。 |
| 28 | Toolkit getToolkit() 返回此框架的工具包。 |
| 29 | String getWarningString() 獲取與此視窗一起顯示的警告字串。 |
| 30 | WindowFocusListener[] getWindowFocusListeners() 返回在此視窗上註冊的所有視窗焦點監聽器的陣列。 |
| 31 | WindowListener[] getWindowListeners() 返回在此視窗上註冊的所有視窗監聽器的陣列。 |
| 32 | static Window[] getWindows() 返回此應用程式建立的所有 Window(包括已擁有和未擁有)的陣列。 |
| 33 | WindowStateListener[] getWindowStateListeners() 返回在此視窗上註冊的所有視窗狀態監聽器的陣列。 |
| 34 | void hide() 已棄用。從 JDK 版本 1.5 開始,由 setVisible(boolean) 替換。 |
| 35 | boolean isActive() 返回此 Window 是否處於活動狀態。 |
| 36 | boolean isAlwaysOnTop() 返回此視窗是否始終位於其他視窗之上。 |
| 37 | boolean isAlwaysOnTopSupported() 返回此視窗是否支援始終位於頂部的模式。 |
| 38 | boolean isFocusableWindow() 返回此 Window 是否可以成為聚焦視窗,即此 Window 或其任何子元件是否可以成為焦點所有者。 |
| 39 | boolean isFocusCycleRoot() 始終返回 true,因為所有 Window 必須是焦點遍歷迴圈的根。 |
| 40 | boolean isFocused() 返回此 Window 是否已聚焦。 |
| 41 | boolean isLocationByPlatform() 如果此 Window 下次可見時將顯示在本地視窗系統的預設位置,則返回 true。 |
| 42 | boolean isShowing() 檢查此 Window 是否顯示在螢幕上。 |
| 43 | void pack() 導致此 Window 調整大小以適應其子元件的首選大小和佈局。 |
| 44 | void paint(Graphics g) 繪製容器。 |
| 45 | boolean postEvent(Event e) 已棄用。從 JDK 版本 1.1 開始,由 dispatchEvent(AWTEvent) 替換。 |
| 46 | protected void processEvent(AWTEvent e) 處理此視窗上的事件。 |
| 47 | protected void processWindowEvent(WindowEvent e) 透過將發生的視窗事件分派給任何已註冊的 WindowListener 物件來處理此視窗上發生的視窗事件。 |
| 48 | protected void processWindowFocusEvent(WindowEvent e) 透過將發生的視窗焦點事件分派給任何已註冊的 WindowFocusListener 物件來處理此視窗上發生的視窗焦點事件。 |
| 49 | protected void processWindowStateEvent(WindowEvent e) 透過將發生的視窗狀態事件分派給任何已註冊的 WindowStateListener 物件來處理此視窗上發生的視窗狀態事件。 |
| 50 | void removeNotify() 透過移除其與本地螢幕資源的連線,使此 Container 不可顯示。 |
| 51 | void removeWindowFocusListener(WindowFocusListener l) 移除指定的視窗焦點監聽器,使其不再接收來自此視窗的視窗事件。 |
| 52 | void removeWindowListener(WindowListener l) 移除指定的視窗監聽器,使其不再接收來自此視窗的視窗事件。 |
| 53 | void removeWindowStateListener(WindowStateListener l) 移除指定的視窗狀態監聽器,使其不再接收來自此視窗的視窗事件。 |
| 54 | void reshape(int x, int y, int width, int height) 已棄用。從 JDK 版本 1.1 開始,由 setBounds(int, int, int, int) 替換。 |
| 55 | void setAlwaysOnTop(boolean alwaysOnTop) 設定此視窗是否應始終位於其他視窗之上。 |
| 56 | void setBounds(int x, int y, int width, int height) 移動並調整此元件的大小。 |
| 57 | void setBounds(Rectangle r) 移動並調整此元件的大小,使其符合新的邊界矩形 r。 |
| 58 | void setCursor(Cursor cursor) 將游標影像設定為指定的游標。 |
| 59 | void setFocusableWindowState(boolean focusableWindowState) 設定此 Window 是否可以成為聚焦視窗,前提是它滿足 isFocusableWindow 中概述的其他要求。 |
| 60 | void setFocusCycleRoot(boolean focusCycleRoot) 不執行任何操作,因為 Window 必須始終是焦點遍歷迴圈的根。 |
| 61 | void setIconImage(Image image) 設定要顯示為此視窗圖示的影像。 |
| 62 | void setIconImages(List<? extends Image> icons) 設定要顯示為此視窗圖示的影像序列。 |
| 63 | void setLocationByPlatform(boolean locationByPlatform) 設定此 Window 是否應顯示在本地視窗系統的預設位置,或者顯示在 Window 下次可見時的當前位置(由 getLocation 返回)。 |
| 64 | void setLocationRelativeTo(Component c) 設定視窗相對於指定元件的位置。 |
| 65 | void setMinimumSize(Dimension minimumSize) 將此視窗的最小大小設定為常量值。 |
| 66 | void setModalExclusionType(Dialog.ModalExclusionType exclusionType) 指定此視窗的模態排除型別。 |
| 67 | void setSize(Dimension d) 調整此元件的大小,使其寬度為 d.width,高度為 d.height。 |
| 68 | void setSize(int width, int height) 調整此元件的大小,使其寬度為 width,高度為 height。 |
| 69 | void setVisible(boolean b) 根據引數 b 的值顯示或隱藏此 Window。 |
| 70 | void show() 已棄用。從 JDK 版本 1.5 開始,由 setVisible(boolean) 替換。 |
| 71 | void toBack() 如果此 Window 可見,則將此 Window 傳送到後面,如果它是聚焦或活動視窗,則可能會導致其失去焦點或啟用。 |
| 72 | void toFront() 如果此 Window 可見,則將此 Window 放在最前面,並可能使其成為聚焦視窗。 |
繼承的方法
此類繼承自以下類的方法
java.awt.Window
java.awt.Container
java.awt.Component
java.lang.Object
Window 示例
使用您選擇的任何編輯器建立以下 Java 程式,例如在 D:/ > AWT > com > tutorialspoint > gui > 中。
AwtContainerDemo.javapackage 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 showWindowDemo(){
headerLabel.setText("Container in action: Window");
final MessageWindow window =
new MessageWindow(mainFrame,
"Welcome to TutorialsPoint AWT Tutorial.");
Button okButton = new Button("Open a Window");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
window.setVisible(true);
statusLabel.setText("A Window shown to the user.");
}
});
controlPanel.add(okButton);
mainFrame.setVisible(true);
}
class MessageWindow extends Window{
private String message;
public MessageWindow(Frame parent, String message) {
super(parent);
this.message = message;
setSize(300, 300);
setLocationRelativeTo(parent);
setBackground(Color.gray);
}
public void paint(Graphics g) {
super.paint(g);
g.drawRect(0,0,getSize().width - 1,getSize().height - 1);
g.drawString(message,50,150);
}
}
}
使用命令提示符編譯程式。轉到 D:/ > AWT 並鍵入以下命令。
D:\AWT>javac com\tutorialspoint\gui\AwtContainerDemo.java
如果沒有任何錯誤,則表示編譯成功。使用以下命令執行程式。
D:\AWT>java com.tutorialspoint.gui.AwtContainerDemo
驗證以下輸出