swing常用的package
pachage | 内 容
-----------------|-------------------------------
javax.swing | 最常用的pachage,包含了各种swing组件的类
javax.swing.border | 包含与swing组件外框有关的类
javax..swing.colorchooser | 针对swing调色盘组件(jcolorchooser)所设计的类
javax.swing.event | 处理由swing组件产生的事件,有别于awt事件
javax.swing.filechooser | 包含针对swing文件选择对话框(jfilechooser)所设计的类
----------------------------------------------
javax.swing.plaf | 处理swing组件外观的相关类
javax.swing.plaf.basic |
javax.swing.plaf.metal |
javax.swing.plaf.multi |
----------------------------------------------
javax.swing.table | 针对swing表格组件(jtable)所设计的类
----------------------------------------------
javax.swing.text | 包含与swing文字组件相关的类
javax.swing.text.html |
javax.swing.text.html.parser |
javax.swing.text.rtf |
----------------------------------------------
javax.swing.tree | 针对swing树关元件(jtree)所设计的类
javax.swing.undo | 提供swing文字组件redo或undo的功能
1-2:swing的版面结构
swing中几乎所有组件都是从jcomponent衍生而来,也就是说这些组件都是lightweight component,均由纯java code所编写面成
、swing中以下几个组件不是由jcomponent继承面来:
jframe(jroot pane)
jdialog(jroot pane)
jwindow(jroot pane)
japplet(jroot pane)
以上四个组件是heavyweight component,必须使用到native code来画出这四个窗口组件,因为要在操作系统中显示窗口画面,必
须使用操作系统的宣传品资源,面以往的awt组件大多使用native code所构造出来,因此swing中的jframe便继承原有awt中的frame
类,面不是继承jcomponent类。同样,japplet是继承原有awt中的japplet类,也不是继承jcomponent类。
jframe、jdialog、jwindow及japplet这四个组件统称为最上层组件,因为其余的swing组件都必须依附在此四组件之一上才能
显示出来。此四组件均实现(implement)rootpanecontainer这个界面(interface),此界面定义了各种容器取得与设置并不是真实的容器,它是由glass pane与layered pane所组成(layered pane里拥有content pane与menu bar,而menu bar可选择使用或不使用),
我们不能在jrootpane上加入任何的组件,因为它只是一个虚拟的容器,若要在最上层组件上加入组件,必须加在layered pane或是
layered pane里的content pane上。以jframe为例,一般我们要在jframe上加入其他组件(如jbutton、jlabel等)必须先取得jframe
的content pane,然后将要加入的组件放在此content pane中,而不是直接就加到jframe上。因此若要在jframe中加入一个按钮,不
能像以前awt时一样写成frame.add(button)的形式,而必须先取得jframe的content pane,然后将按钮加入content pane中,如:
frame.getcontentpane().add(button)
否则在编译的时候将有错误信息产生。
注意:
1.组件必须加在容器中,而容器本身具有层次性的关系,就如同珠宝盒一般,大盒子里面可以放小盒子,小盒子里面还可以放更小的盒子,而珠宝就可以放在某一个盒子中,这里的珠宝就代表组件,盒子就代表容器。因此若您想在jframe加入任何组件时,必须
先取得jframe的容器来放置这些组件,而由于jframe、jdialog、jwindow与japplet是显示swing组件的源头,我们可以称它们为根
组件,也就是所谓的最上层组件。
2.rootpanecontainer它是一个interface,共有5个类实现(implement)它,分别是jframe、jappleet、jwindow、jdialog、
jinternalframe,其中jinternalframe是一个lightweight component,它不是一个最上层组件,也就是说jinternalframe不能单独显示出来,必须依附在最上层组件中,我们将在下面讨论组件,而jframe,japplet,jwindow,jdialog均为最上层组件。
rootpanecontainer定义了下面几种方法:
方法
container getcontentpane()返回contentpane
component getglasspane()返回glasspane
jlayeredpane getlayeredpane()返回layeredpane
jrootpane getrootpane返回属于这个组件的jrootpane
void setcontentpane(container contentpane)设置contentpane
void setglasspane(component glasspane)设置glasspane
void setlayeredpane(jlayeredpane layeredpane)设置layeredpane
jframe如何取得content pane的实际流程,下面是一段很简单的程序代码:
public class simple{
simple(){
jframe frame=new jframe();
container contentpane=frame.getcontentpane();
jbutton button=new jbutton();
contentpane.add(button);
}
}
当我们写frame.getcontentpane()时,会返回此frame的content pane,也就是一个容器组件,有了容器之后我们才能将button组件
摆进去,此时jframe才算拥有button组件。所以jframe就好像是一块空地,要在这空地上信人应该先盖一栋房子(容器),然后人
、家具、设备等等(组件)就能搬进此房子中。下面的层次结构说明了最上层组件都含有jrootpane组件,jrootpane本身就含有容
器组件,可让最上层组件装入其他的组件。
|frame------jframe(jroot pane)
|
window|dialog-----jdialog(jroot pane)
|
|
|-----------jwindow(jroot pane)
applet -----------japplet(jroot pane)
图示:
|grass pane
|
root pane|
| |content pane
|layered pane|
|menu bar
闽公网安备 35060202000074号