Window
介绍 (Introduction)
类Window是一个顶级窗口,没有边框,没有菜单栏。 它使用BorderLayout作为默认布局管理器。
类声明
以下是java.awt.Window类的声明:
public class Window
extends Container
implements Accessible
类构造函数
SN | 构造函数和描述 |
---|---|
1 | Window(Frame owner) 构造一个新的,最初不可见的窗口,并将指定的Frame作为其所有者。 |
2 | Window(Window owner) 构造一个新的,最初不可见的窗口,并将指定的Window作为其所有者。 |
3 | Window(Window owner, GraphicsConfiguration gc) 使用指定的所有者Window和屏幕设备的GraphicsConfiguration构造一个新的,最初不可见的窗口。 |
类方法
SN | 方法和描述 |
---|---|
1 | void addNotify() 通过创建与其本机屏幕资源的连接,使此窗口可显示。 |
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() 如果它满足isFocusableWindow中列出的其他要求,则返回此Window是否可以成为焦点窗口。 |
15 | Container getFocusCycleRootAncestor() 始终返回null,因为Windows没有祖先; 它们代表组件层次结构的顶部。 |
16 | Component getFocusOwner() 如果此窗口被聚焦,则返回此窗口的子Component,该窗口具有焦点; 否则返回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() 返回此窗口聚焦时将接收焦点的此窗口的子Component。 |
25 | Window[] getOwnedWindows() 返回一个包含此窗口当前拥有的所有窗口的数组。 |
26 | Window getOwner() 返回此窗口的所有者。 |
27 | static Window[] getOwnerlessWindows() 返回此应用程序创建的所有Windows的数组,该数组没有所有者。 |
28 | Toolkit getToolkit() 返回此框架的工具包。 |
29 | String getWarningString() 获取此窗口显示的警告字符串。 |
30 | WindowFocusListener[] getWindowFocusListeners() 返回在此窗口中注册的所有窗口焦点侦听器的数组。 |
31 | WindowListener[] getWindowListeners() 返回在此窗口中注册的所有窗口侦听器的数组。 |
32 | static Window[] getWindows() 返回由此应用程序创建的所有Windows(包括所有Windows和无主机)的数组。 |
33 | WindowStateListener[] getWindowStateListeners() 返回在此窗口中注册的所有窗口状态侦听器的数组。 |
34 | void hide() 已过时。 从JDK 1.5版开始,由setVisible(boolean)取代。 |
35 | boolean isActive() 返回此Window是否处于活动状态。 |
36 | boolean isAlwaysOnTop() 返回此窗口是否为始终在顶部的窗口。 |
37 | boolean isAlwaysOnTopSupported() 返回此窗口是否支持always-on-top模式。 |
38 | boolean isFocusableWindow() 返回此Window是否可以成为焦点窗口,即此Window或其任何子组件是否可以成为焦点所有者。 |
39 | boolean isFocusCycleRoot() 始终返回true,因为所有Windows必须是焦点遍历循环的根。 |
40 | boolean isFocused() 返回此Window是否已聚焦。 |
41 | boolean isLocationByPlatform() 如果此窗口在下一次显示此窗口时将显示在本机窗口系统的默认位置,则返回true。 |
42 | boolean isShowing() 检查此窗口是否显示在屏幕上。 |
43 | void pack() 使此窗口的大小适合其子组件的首选大小和布局。 |
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 version 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) 设置如果此窗口满足isFocusableWindow中列出的其他要求,是否可以成为焦点窗口。 |
60 | void setFocusCycleRoot(boolean focusCycleRoot) 什么都不做因为Windows必须始终是焦点遍历循环的根源。 |
61 | void setIconImage(Image image) 设置要显示为此窗口图标的图像。 |
62 | void setIconImages(List《? extends Image》 icons) 设置要显示的图像序列作为此窗口的图标。 |
63 | void setLocationByPlatform(boolean locationByPlatform) 设置此窗口是否应显示在本机窗口系统的默认位置,或者在下次使窗口可见时显示在当前位置(由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) 调整此组件的大小,使其具有宽度宽度和高度。 |
69 | void setVisible(boolean b) 根据参数b的值显示或隐藏此窗口。 |
70 | void show() 已过时。 从JDK 1.5版开始,由setVisible(boolean)取代。 |
71 | void toBack() 如果此窗口可见,则将此窗口发送到后面,如果它是聚焦或活动窗口,则可能导致它失去焦点或激活。 |
72 | void toFront() 如果此窗口可见,则将此窗口置于前面,并使其成为焦点窗口。 |
方法继承
该类继承以下类中的方法:
java.awt.Window
java.awt.Container
java.awt.Component
java.lang.Object
窗口示例
使用您选择的任何编辑器创建以下java程序,例如D:/ 》 AWT 》 com 》 iowiki 》 gui 》
AwtContainerDemo.javapackage com.iowiki.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 IoWiki 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 IoWiki 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\iowiki\gui\AwtContainerDemo.java
如果没有错误,那意味着编译成功。 使用以下命令运行程序。
D:\AWT>java com.iowiki.gui.AwtContainerDemo
验证以下输出
