第5章+边框Word文档格式.docx

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

第5章+边框Word文档格式.docx

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

第5章+边框Word文档格式.docx

  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("

l.setBorder(

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

c.setBorder(

BorderFactory.createCompoundBorder(

BorderFactory.createCompoundBorder(

BorderFactory.createLineBorder(Color.gray,10),

BorderFactory.createRaisedBevelBorder()),

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,

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));

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部分透明的边框的样例

JPanelpanel=newRainPanel();

TitledBorderborder=newTitledBorder("

panel.setBorder(border);

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

System.out.println("

opaque="

+border.isBorderOpaque());

System.out.println(

"

insets="

+border.getBorderInsets(panel));

classRainPanelextendsJPanel{

Iconicon=newImageIcon(getClass().getResource("

rain.gif"

intpatchW=icon.getIconWidth(),

patchH=icon.getIconHeight();

for(intr=0;

r<

size.width;

r+=patchW){

for(intc=0;

c<

size.height;

c+=patchH)

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

5.1.4边框包

5.1.5边框接口

5.1.6AbstractBoorder类

5.1.7边框库——共享边框

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

ContainercontentPane=getContentPane();

JPanelpanel=newJPanel();

JPanelpanel2=newJPanel();

Borderborder=BorderFactory.createRaisedBevelBorder();

Borderborder2=BorderFactory.createRaisedBevelBorder();

panel2.setBorder(border2);

contentPane.add(panel,BorderLayout.NORTH);

contentPane.add(panel2,BorderLayout.SOUTH);

if(border==border2)

System.out.println("

bevelbordersareshared"

else

bevelbordersareNOTshared"

5.1.8替换内置边框

5.1.9实现定制的边框

例5-5HandleBorder类清单

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,

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使用定制边框

importjava.awt.event.*;

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.length;

++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类清单

classColorIconimplementsIcon{

privateColorfillColor;

privateintw,h;

publicColorIcon(ColorfillColor,intw,inth){

this.fillColor=fillColor;

this.w=w;

this.h=h;

publicvoidpaintIcon(Componentc,Graphicsg,intx,inty){

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菜单项中的图标

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

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

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

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类清单

c

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

当前位置:首页 > PPT模板 > 自然景观

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

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