public class Frame
extends
Window
implements
MenuContainer
Frame 是带有标题和边界的顶层窗口。
frame 的大小包括边界指定的所有区域。边界区域的尺寸可以使用 getInsets 方法获得,但是,由于这些尺寸是与平台相关的,因此在通过调用 pack 或 show 将 frame 设置为可显示的之前,无法获得有效的 insets 值。由于 frame 的总大小包括了边界区,因此边界有效地模糊了 frame 的部分区域,约束了可用于在矩形中呈现和/或显示子部件的区域,矩形左上角的位置为 (insets.left, insets.top),宽度为 width - (insets.left + insets.right),长度为 height - (insets.top + insets.bottom)。
frame 的默认布局为 BorderLayout。
使用 setUndecorated,frame 可以关闭本机装饰(例如 Frame 和 Titlebar)。只有在 frame 不是 displayable 时才能完成此操作。
在多屏幕环境中,通过使用 Frame(GraphicsConfiguration) 或 Frame(String title, GraphicsConfiguration) 构造 Frame,可以在不同的屏幕设备上创建 Frame。GraphicsConfiguration 对象是目标屏幕设备的 GraphicsConfiguration 对象之一。
在虚拟设备多屏幕环境中(其中桌面区域可以跨越多物理屏幕设备),所有配置的边界都是相对于虚拟坐标系统的。虚拟坐标系统的原点位于主物理屏幕的左上角。是否使用负坐标,取决于主物理屏幕在虚拟设备中的位置,如下图所示。
在此环境中,调用 setLocation 时,必须传递一个虚拟坐标到此方法中。类似地,对 Frame 调用 getLocationOnScreen 将返回虚拟设备坐标。调用 GraphicsConfiguration 的 getBounds 方法,以查找它在虚拟坐标系统中的原点。
以下代码将 Frame 的位置设置为 (10, 10)(相对于相应的 GraphicsConfiguration 的物理屏幕的原点)。如果不考虑 GraphicsConfiguration 的边界,则 Frame 的位置将被设置为 (10, 10)(相对于虚拟坐标系统),并出现在主物理屏幕上,主物理屏幕不同于指定的 GraphicsConfiguration 的物理屏幕。
Frame f = new Frame(GraphicsConfiguration gc);
Rectangle bounds = gc.getBounds();
f.setLocation(10 + bounds.x, 10 + bounds.y);
Rectangle virtualBounds = new Rectangle();
GraphicsEnvironment ge = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice[] gs =
ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
GraphicsDevice gd = gs[j];
GraphicsConfiguration[] gc =
gd.getConfigurations();
for (int i=0; i < gc.length; i++) {
virtualBounds =
virtualBounds.union(gc[i].getBounds());
}
}
Frame 能够产生以下类型的 WindowEvent:
WINDOW_OPENEDWINDOW_CLOSING:
在处理事件时,如果程序没有显式地隐藏或移除窗口,则取消窗口关闭操作。WINDOW_CLOSEDWINDOW_ICONIFIEDWINDOW_DEICONIFIEDWINDOW_ACTIVATEDWINDOW_DEACTIVATEDWINDOW_GAINED_FOCUSWINDOW_LOST_FOCUSWINDOW_STATE_CHANGED
-
从以下版本开始:
- JDK1.0
- import java.awt.*;
- import javax.swing.*;
-
/**
* @author bo.wang
*
*/
/**
* @author bo.wang
*
*/
public class HelloWorldSwing1 {
public static void main(String[] args) {
JFrame frame = new JFrame("HelloWorldSwing1");
//GraphicsConfiguration gc=GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
GraphicsEnvironment ge = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice[] gs =
ge.getScreenDevices();
GraphicsDevice gd = gs[0];
GraphicsConfiguration[] gcall =
gd.getConfigurations();
GraphicsConfiguration gc=gcall[0];
System.out.println(gc.toString());
Frame f = new Frame(gc);
Rectangle bounds = gc.getBounds();
f.setLocation(1000 + bounds.x, 1000 + bounds.y);
final JLabel label = new JLabel("Hello World1");
frame.getContentPane().add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();//缩紧所有组件,使初始打开页面时即可显示所有的组件
frame.setVisible(true);
}
}
2052





