第5章+边框.docx

上传人:b****6 文档编号:8598949 上传时间:2023-02-01 格式:DOCX 页数:21 大小:20.47KB
下载 相关 举报
第5章+边框.docx_第1页
第1页 / 共21页
第5章+边框.docx_第2页
第2页 / 共21页
第5章+边框.docx_第3页
第3页 / 共21页
第5章+边框.docx_第4页
第4页 / 共21页
第5章+边框.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

第5章+边框.docx

《第5章+边框.docx》由会员分享,可在线阅读,更多相关《第5章+边框.docx(21页珍藏版)》请在冰豆网上搜索。

第5章+边框.docx

第5章+边框

第5章边框、图标和动作

  本章介绍Swing的三种实用工具:

边框、图标和动作。

  边框绘制在组件的边界周围,它有许多不同的各类:

线边框、雕刻边框、不光滑的边框等等。

边框本身不是组件,所以,它们绘制在指定组件的边衬中。

  图标是图形对象,通常是一个小图像。

与边框一样,图标在指定组件的指定位置上绘制。

  动作封装图形用户界面的一个逻辑操作,并且还简化用户界面元素的构造工作。

动作通常由一个或多个图标或文本字符串组成。

可以把动作添加到某些容器中,这些容器创建一个组件与这个动作相关联。

例如,利用JMenu.add(Action)方法,可把动作添加到一个菜单中。

当一个动作添加到一个菜单中时,这个菜单用与这个动作相关联的文本和图标来创建一个菜单项并把这个菜单项添加到菜单中。

  边框、图标和动作都是很有意义的,因为它们都可以与多个组件相关联。

由于边框和图标都不是组件,但却都能绘制到组件中,所以,可以在支持使用边框和图标的多个组件中共享边框和图标。

动作也必须被多个组件所共享,并且用来作为控制的中心点以维护与这个动作相关联的组件的启用状态。

5.1边框

  通过构造所需类型的边框,然后把这个边框传送给JComponent.setBorder(Border),所有JComponent扩展(JViewport除外)都可以有边框。

虽然每个组件可以只有一个边框,但Swing支持组合边框。

因此,在实际应用中,单个组件可以使数个边框嵌套在一起,使边框有一定的深度。

  边框的使用很简单。

例如,图5-1示出了一个带标题边框的JPanel实例。

  

   图5-1 一个带标题边框的JPanel实例

  例5-1例出了图5-1所示的小应用程序的代码。

例5-1 一个带边框的JPanel的小应用程序

  importjava.awt.BorderLayout;

  importjavax.swing.*;

  importjavax.swing.border.*;

  publicclassTestextendsJApplet{

   publicvoidinit(){

    JPanelpanel=newJPanel();

    panel.setBorder(newTitledBorder("JPanelBorder"));

    getContentPane().add(panel,BorderLayout.CENTER);

   }

  }

  这个小应用程序创建一个带标题的边框,这个边框传递给面板的setBorder方法。

5.1.1边框和边衬

  AWT容器有一个insets属性,它定义容器的边衬。

布局管理器仔细地布局一个容器中的各个组件,以便这些组件不会侵占这个容器的边衬区。

容器的insets属性是一个只读属性,修改AWT容器insets属性唯一的方法是子类化一个容器并重载它的getInsets方法。

5.1.2Swing的边框类型

例5-2显示所有Swing边框类型的小应用程序

importjava.awt.*;

importjavax.swing.*;

importjavax.swing.border.*;

publicclassTestextendsJApplet{

publicvoidinit(){

JPaneljpanel=newAllBordersPanel();

getContentPane().add(jpanel,BorderLayout.CENTER);

}

}

classAllBordersPanelextendsJPanel{

publicAllBordersPanel(){

JPanelbl=newPanelWithTitle("BevelLowered"),

br=newPanelWithTitle("BevelRaised"),

c=newPanelWithTitle("Compound"),

l=newPanelWithTitle("Line"),

m=newPanelWithTitle("Matte"),

e=newPanelWithEmptyBorder("Empty"),

t=newPanelWithTitle("Titled"),

sbr=newPanelWithTitle("SoftBevelRaised"),

sbl=newPanelWithTitle("SoftBevelLowered"),

el=newPanelWithTitle("EtchedLowered"),

er=newPanelWithTitle("EtchedRaised");

setLayout(newGridLayout(4,3,2,2));

ImageIconicon=newImageIcon(this.getClass().getResource("smiley.gif"));

Dimensioniconsz=newDimension(icon.getIconWidth(),

icon.getIconHeight());

bl.setBorder(BorderFactory.createLoweredBevelBorder());

br.setBorder(BorderFactory.createRaisedBevelBorder());

sbr.setBorder(newSoftBevelBorder(BevelBorder.RAISED));

sbl.setBorder(newSoftBevelBorder(BevelBorder.LOWERED));

t.setBorder(BorderFactory.createTitledBorder("Titled"));

l.setBorder(

BorderFactory.createLineBorder(Color.black,2));

c.setBorder(

BorderFactory.createCompoundBorder(

BorderFactory.createCompoundBorder(

BorderFactory.createLineBorder(Color.gray,10),

BorderFactory.createRaisedBevelBorder()),

BorderFactory.createCompoundBorder(

BorderFactory.createLineBorder(Color.blue,5),

BorderFactory.createLoweredBevelBorder())));

el.setBorder(BorderFactory.createEtchedBorder(

getBackground().brighter(),

getBackground().darker()));

er.setBorder(BorderFactory.createEtchedBorder(

getBackground().darker(),

getBackground().brighter()));

m.setBorder(BorderFactory.createMatteBorder(

iconsz.height,iconsz.width,

iconsz.height,iconsz.width,

icon));

add(br);add(bl);add(sbr);

add(sbl);add(c);add(el);

add(er);add(e);add(l);

add(m);add(t);

}

}

classPanelWithTitleextendsJPanel{

privateStringtitle;

publicPanelWithTitle(Stringtitle){

this.title=title;

}

publicvoidpaintComponent(Graphicsg){

FontMetricsfm=g.getFontMetrics();

Dimensionsize=getSize();

inttitleW=fm.stringWidth(title);

g.setColor(Color.black);

g.drawString(title,size.width/2-titleW/2,

size.height/2);

}

}

classPanelWithEmptyBorderextendsPanelWithTitle{

publicPanelWithEmptyBorder(Stringtitle){

super(title);

setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

}

publicvoidpaintComponent(Graphicsg){

Dimensionsize=getSize();

Insetsinsets=getInsets();

g.setColor(Color.red);

g.fillRect(insets.left,insets.top,

size.width-2*insets.left,

size.height-2*insets.top);

super.paintComponent(g);

}

}

5.1.3不透明与透明之间的比较

 

例5-3部分透明的边框的样例

importjavax.swing.*;

importjavax.swing.border.*;

importjava.awt.*;

publicclassTestextendsJApplet{

JPanelpanel=newRainPanel();

TitledBorderborder=newTitledBorder("JPanelBorder");

publicvoidinit(){

panel.setBorder(border);

getContentPane().add(panel,BorderLayout.CENTER);

System.out.println("opaque="+border.isBorderOpaque());

System.out.println(

"insets="+border.getBorderInsets(panel));

}

}

classRainPanelextendsJPanel{

publicvoidpaintComponent(Graphicsg){

Iconicon=newImageIcon(getClass().getResource("rain.gif"));

Dimensionsize=getSize();

intpatchW=icon.getIconWidth(),

patchH=icon.getIconHeight();

for(intr=0;r

for(intc=0;c

icon.paintIcon(this,g,r,c);

}

}

}

5.1.4边框包

 

5.1.5边框接口

 

5.1.6AbstractBoorder类

 

5.1.7边框库——共享边框

 

例5-4从边框库中获得边框

importjava.awt.*;

importjavax.swing.*;

importjavax.swing.border.*;

publicclassTestextendsJApplet{

publicvoidinit(){

ContainercontentPane=getContentPane();

JPanelpanel=newJPanel();

JPanelpanel2=newJPanel();

Borderborder=BorderFactory.createRaisedBevelBorder();

Borderborder2=BorderFactory.createRaisedBevelBorder();

panel.setBorder(border);

panel2.setBorder(border2);

contentPane.add(panel,BorderLayout.NORTH);

contentPane.add(panel2,BorderLayout.SOUTH);

if(border==border2)

System.out.println("bevelbordersareshared");

else

System.out.println("bevelbordersareNOTshared");

}

}

5.1.8替换内置边框

 

5.1.9实现定制的边框

 

例5-5HandleBorder类清单

importjava.awt.*;

importjavax.swing.*;

importjavax.swing.border.*;

publicclassHandleBorderextendsAbstractBorder{

protectedColorlineColor;

protectedintthick;

publicHandleBorder(){

this(Color.black,6);

}

publicHandleBorder(ColorlineColor,intthick){

this.lineColor=lineColor;

this.thick=thick;

}

publicvoidpaintBorder(Componentc,Graphicsg,intx,

inty,intw,inth){

Graphicscopy=g.create();

if(copy!

=null){

try{

copy.translate(x,y);

paintRectangle(c,copy,w,h);

paintHandles(c,copy,w,h);

}

finally{

copy.dispose();

}

}

}

publicInsetsgetBorderInsets(){

returnnewInsets(thick,thick,thick,thick);

}

protectedvoidpaintRectangle(Componentc,Graphicsg,

intw,inth){

g.setColor(lineColor);

g.drawRect(thick/2,thick/2,w-thick-1,h-thick-1);

}

protectedvoidpaintHandles(Componentc,Graphicsg,

intw,inth){

g.setColor(lineColor);

g.fillRect(0,0,thick,thick);//upperleft

g.fillRect(w-thick,0,thick,thick);//upperright

g.fillRect(0,h-thick,thick,thick);//lowerleft

g.fillRect(w-thick,h-thick,thick,thick);//lowerright

g.fillRect(w/2-thick/2,0,thick,thick);//midtop

g.fillRect(0,h/2-thick/2,thick,thick);//midleft

g.fillRect(w/2-thick/2,h-thick,thick,thick);//midbottom

g.fillRect(w-thick,h/2-thick/2,thick,thick);//midright

}

}

例5-6使用定制边框

importjavax.swing.*;

importjavax.swing.border.*;

importjava.awt.*;

importjava.awt.event.*;

publicclassTestextendsJApplet{

publicvoidinit(){

ContainercontentPane=getContentPane();

JPanel[]panels={newJPanel(),

newJPanel(),newJPanel()};

Border[]borders={newHandleBorder(),

newHandleBorder(Color.red,8),

newHandleBorder(Color.blue,10)};

contentPane.setLayout(

newFlowLayout(FlowLayout.CENTER,20,20));

for(inti=0;i

panels[i].setPreferredSize(newDimension(100,100));

panels[i].setBorder(borders[i]);

contentPane.add(panels[i]);

}

}

}

 

5.2图标

例5-7一个绘制图标的小应用程序

importcom.sun.java.swing.JApplet;

importjava.awt.Color;

importjava.awt.Graphics;

publicclassIconTestextendsJApplet{

ColorIconredIcon;

ColorIconblueIcon;

ColorIconyellowIcon;

publicvoidpaint(Graphicsg){

redIcon.paintIcon(this,g,0,0);

blueIcon.paintIcon(this,g,redIcon.getIconWidth()+10,0);

yellowIcon.paintIcon(this,g,redIcon.getIconWidth()+10+blueIcon.getIconWidth()+10,0);

}

publicIconTest(){

redIcon=newColorIcon(Color.red,50,50);

blueIcon=newColorIcon(Color.blue,60,60);

yellowIcon=newColorIcon(Color.yellow,70,70);

}

}

例5-8ColorIcon类清单

importjava.awt.*;

importjavax.swing.*;

classColorIconimplementsIcon{

privateColorfillColor;

privateintw,h;

publicColorIcon(ColorfillColor,intw,inth){

this.fillColor=fillColor;

this.w=w;

this.h=h;

}

publicvoidpaintIcon(Componentc,Graphicsg,intx,inty){

g.setColor(Color.black);

g.drawRect(x,y,w-1,h-1);

g.setColor(fillColor);

g.fillRect(x+1,y+1,w-2,h-2);

}

publicintgetIconWidth(){

returnw;

}

publicintgetIconHeight(){

returnh;

}

}

 

 

5.2.1把图标与组件相关联

 

例5-9菜单项中的图标

importjava.awt.*;

importjavax.swing.*;

publicclassTestextendsJApplet{

ColorIconredIcon=newColorIcon(Color.red,40,15),

blueIcon=newColorIcon(Color.blue,40,15),

yellowIcon=newColorIcon(Color.yellow,40,15);

publicvoidinit(){

JMenuBarmb=newJMenuBar();

JMenucolors=newJMenu("Colors");

colors.add(newJMenuItem(redIcon));

colors.add(newJMenuItem(blueIcon));

colors.add(newJMenuItem(yellowIcon));

mb.add(colors);

setJMenuBar(mb);

}

}

5.2.2在组件中共享图标

 

例5-10修改后的ColorIcon类清单

importjava.awt.*;

importjavax.swing.*;

c

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 工学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1