swing中的最上层组件

swing中几乎所有组件都是从JComponent衍生而来,也就是说这些组件lightweight component,均由纯java code所编写而成.swing中以下几个组件不是由JComponent继承而来:
JFrame(JRoot Pane)//常用来建立主窗口
JDialog(JRoot Pane)//对话框窗口
JWindow(JRoot Pane)//经常用做logo窗口,无标题栏,无边框
JApplet(JRoot Pane)//用来构造applet小应用程序
以上四个组件是heavyweight Component,必须使用到native code来画出这四个窗口组件.因为要在操作系统中显示窗口画面,必须使用操作系统的窗口资源,而以往的AWT组件大多使用native code所构造出来,因此Swing中的JFrame便继承原有AWT中的Frame类,而不是继承JComponent类.同样,JApplet是继承原有AWT中的JApplet类,也不是继承JComponent类.
JFrame,JDialog,JWindow及JApplet这四个组件统称为最上层组件,因为其余的swing组件都必须依附在此四组件之一上才能显示出来,也就是说swing中要建立窗口必须使用其中的一个最上层组件.此四组件均含有RootPane组件,且均实现了RootPaneContainer这个接口.附图:

[img]http://dl.iteye.com/upload/attachment/408137/6f48e0b8-c201-3859-bb23-4d9515ef702b.gif[/img]


关于RootPaneContainer接口.RootPaneContainer接口定义了各种容器取得与设置的方法,这里的各种容器指的是JRootPane(虚拟的容器),GlassPane,Content.共有五个类实现了RootPaneContainer接口,除了上面所提到得四个最上层得重量级组件之外,还有轻量级的JInternalFrame,它不能单独显示.RootPaneContainer定义了以下几种方法:
Container getContentPane();
Component getGlassPane();
JLayeredPane getLayeredPane();
JRootPane getRootPane();
void setContentPane(Container contentpane);
void setGlassPane(Component glasspane);
void setLayeredPane(JLayeredPane layeredpane);
关于JRootPane类.JRootPane由GlassPane,ContentPane以及MenuBar(可选)组成,其中ContentPane和MenuBar又都由LayeredPane管理.GlassPane位于最上面,用来捕捉鼠标行为.LayeredPane就像家中放鞋子的鞋架,有很多层,而ContentPane则只是其中的一层,一般我们只要对这一层进行操作就行了.
附图:

[img]http://dl.iteye.com/upload/attachment/408139/f7bd406c-acdf-397d-8577-ae4a910740c4.gif[/img]

我们要在最上层组件上加入任何组件只能在GlassPane和ContentPane上面增加,也就是在Layered Pane上面或者在Layered Pane的ContentPane上面增加.
以JFrame为例,一般我们要在JFrame上加入其他组件(如JButton,JLabel等)必须先取得JFrame的Content Pane,然后将要加入的组件放在此Content Pane中,而不是直接就加到JFrame上.因此若要在JFrame中加入一个按钮,不能像以前AWT时一样写成frame.add(button)的形式,而必须先取得JFrame的Content Pane,然后将按钮加入Content Pane中,如:
frame.getContentPane().add(button)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值