jAVA FRAME

public class Frame
   
   
    
    extends 
    
    Window
   
   
   
   
    
    implements 
    
    MenuContainer
   
   
 

Frame 是带有标题和边界的顶层窗口。

frame 的大小包括边界指定的所有区域。边界区域的尺寸可以使用 getInsets 方法获得,但是,由于这些尺寸是与平台相关的,因此在通过调用 packshow 将 frame 设置为可显示的之前,无法获得有效的 insets 值。由于 frame 的总大小包括了边界区,因此边界有效地模糊了 frame 的部分区域,约束了可用于在矩形中呈现和/或显示子部件的区域,矩形左上角的位置为 (insets.left, insets.top),宽度为 width - (insets.left + insets.right),长度为 height - (insets.top + insets.bottom)

frame 的默认布局为 BorderLayout

使用 setUndecorated,frame 可以关闭本机装饰(例如 FrameTitlebar)。只有在 frame 不是 displayable 时才能完成此操作。

在多屏幕环境中,通过使用 Frame(GraphicsConfiguration)Frame(String title, GraphicsConfiguration) 构造 Frame,可以在不同的屏幕设备上创建 FrameGraphicsConfiguration 对象是目标屏幕设备的 GraphicsConfiguration 对象之一。

在虚拟设备多屏幕环境中(其中桌面区域可以跨越多物理屏幕设备),所有配置的边界都是相对于虚拟坐标系统的。虚拟坐标系统的原点位于主物理屏幕的左上角。是否使用负坐标,取决于主物理屏幕在虚拟设备中的位置,如下图所示。

包括三个物理屏幕和一个主物理屏幕的虚拟设备。主物理屏幕
显示坐标为 (0,0),而另一个物理屏幕显示坐标为 (-80,-100)。

在此环境中,调用 setLocation 时,必须传递一个虚拟坐标到此方法中。类似地,对 Frame 调用 getLocationOnScreen 将返回虚拟设备坐标。调用 GraphicsConfigurationgetBounds 方法,以查找它在虚拟坐标系统中的原点。

以下代码将 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_OPENED
  • WINDOW_CLOSING:
    在处理事件时,如果程序没有显式地隐藏或移除窗口,则取消窗口关闭操作。
  • WINDOW_CLOSED
  • WINDOW_ICONIFIED
  • WINDOW_DEICONIFIED
  • WINDOW_ACTIVATED
  • WINDOW_DEACTIVATED
  • WINDOW_GAINED_FOCUS
  • WINDOW_LOST_FOCUS
  • WINDOW_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);
 }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值