ImageVerifierCode 换一换
格式:DOC , 页数:21 ,大小:127KB ,
资源ID:5973414      下载积分:10 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/5973414.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(第5章+边框.doc)为本站上传会员【xrp****65】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

第5章+边框.doc

1、第5章 边框、图标和动作   本章介绍Swing的三种实用工具:边框、图标和动作。   边框绘制在组件的边界周围,它有许多不同的各类:线边框、雕刻边框、不光滑的边框等等。边框本身不是组件,所以,它们绘制在指定组件的边衬中。   图标是图形对象,通常是一个小图像。与边框一样,图标在指定组件的指定位置上绘制。   动作封装图形用户界面的一个逻辑操作,并且还简化用户界面元素的构造工作。动作通常由一个或多个图标或文本字符串组成。可以把动作添加到某些容器中,这些容器创建一个组件与这个动作相关联。例如,利用JMenu.add(Action)方法,可把动作添加到一个菜单中。当一个动作添加到一个菜单中

2、时,这个菜单用与这个动作相关联的文本和图标来创建一个菜单项并把这个菜单项添加到菜单中。   边框、图标和动作都是很有意义的,因为它们都可以与多个组件相关联。由于边框和图标都不是组件,但却都能绘制到组件中,所以,可以在支持使用边框和图标的多个组件中共享边框和图标。动作也必须被多个组件所共享,并且用来作为控制的中心点以维护与这个动作相关联的组件的启用状态。 5.1 边框   通过构造所需类型的边框,然后把这个边框传送给JComponent.setBorder(Border),所有JComponent扩展(JViewport除外)都可以有边框。虽然每个组件可以只有一个边框,但Swing支持组合

3、边框。因此,在实际应用中,单个组件可以使数个边框嵌套在一起,使边框有一定的深度。   边框的使用很简单。例如,图5-1示出了一个带标题边框的JPanel实例。       图5-1 一个带标题边框的JPanel实例   例5-1例出了图5-1所示的小应用程序的代码。 例5-1 一个带边框的JPanel的小应用程序   import java.awt.BorderLayout;   import javax.swing.*;   import javax.swing.border.*;   public class Test extends JApplet {    p

4、ublic void init() {     JPanel panel = new JPanel();     panel.setBorder(new TitledBorder("JPanel Border"));     getContentPane().add(panel, BorderLayout.CENTER);    }   }   这个小应用程序创建一个带标题的边框,这个边框传递给面板的setBorder方法。 5.1.1 边框和边衬   AWT容器有一个insets属性,它定义容器的边衬。布局管理器仔细地布局一个容器中的各个组件,以便这些组件不会侵占这个

5、容器的边衬区。容器的insets属性是一个只读属性,修改AWT容器insets属性唯一的方法是子类化一个容器并重载它的getInsets方法。 5.1.2 Swing的边框类型 例5-2 显示所有Swing边框类型的小应用程序 import java.awt.*; import javax.swing.*; import javax.swing.border.*; public class Test extends JApplet { public void init() { JPanel jpanel = new AllBordersPanel(); ge

6、tContentPane().add(jpanel, BorderLayout.CENTER); } } class AllBordersPanel extends JPanel { public AllBordersPanel() { JPanel bl = new PanelWithTitle("Bevel Lowered"), br = new PanelWithTitle("Bevel Raised"), c = new PanelWithTitle("Compound"), l = new PanelWithTitle("Line")

7、 m = new PanelWithTitle("Matte"), e = new PanelWithEmptyBorder("Empty"), t = new PanelWithTitle("Titled"), sbr = new PanelWithTitle("Soft Bevel Raised"), sbl = new PanelWithTitle("Soft Bevel Lowered"), el = new PanelWithTitle("Etched Lowered"), er = new PanelWithT

8、itle("Etched Raised"); setLayout(new GridLayout(4,3,2,2)); ImageIcon icon = new ImageIcon(this.getClass().getResource("smiley.gif")); Dimension iconsz = new Dimension(icon.getIconWidth(), icon.getIconHeight()); bl.setBorder(BorderFactory.createLoweredBevelBorder());

9、 br.setBorder(BorderFactory.createRaisedBevelBorder()); sbr.setBorder(new SoftBevelBorder(BevelBorder.RAISED)); sbl.setBorder(new SoftBevelBorder(BevelBorder.LOWERED)); t.setBorder(BorderFactory.createTitledBorder("Titled")); l.setBorder( BorderFactory.createLineBorder(Color.black,2)

10、); c.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(Color.gray,10), BorderFactory.createRaisedBevelBorder()), BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(Color.blue,5),

11、 BorderFactory.createLoweredBevelBorder()))); el.setBorder(BorderFactory.createEtchedBorder( getBackground().brighter(), getBackground().darker())); er.setBorder(BorderFactory.createEtchedBorder( getBackground().darker(), getBackground().brighter())); m

12、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); } } class PanelWithTitle extends JPanel { private

13、String title; public PanelWithTitle(String title) { this.title = title; } public void paintComponent(Graphics g) { FontMetrics fm = g.getFontMetrics(); Dimension size = getSize(); int titleW = fm.stringWidth(title); g.setColor(Color.black); g.drawString(title, size.width/

14、2 - titleW/2, size.height/2); } } class PanelWithEmptyBorder extends PanelWithTitle { public PanelWithEmptyBorder(String title) { super(title); setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); } public void paintComponent(Graphics g) { Dimension size = getSize();

15、 Insets insets = 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 部分透明的边框的样例 import javax.swing.*; import javax.swing.border.*; impor

16、t java.awt.*; public class Test extends JApplet { JPanel panel = new RainPanel(); TitledBorder border = new TitledBorder("JPanel Border"); public void init() { panel.setBorder(border); getContentPane().add(panel, BorderLayout.CENTER); System.out.println("opaque = " + border.isB

17、orderOpaque()); System.out.println( "insets = " + border.getBorderInsets(panel)); } } class RainPanel extends JPanel { public void paintComponent(Graphics g) { Icon icon = new ImageIcon(getClass().getResource("rain.gif")); Dimension size = getSize(); int patchW = icon.

18、getIconWidth(), patchH = icon.getIconHeight(); for(int r=0; r < size.width; r += patchW) { for(int c=0; c < size.height; c += patchH) icon.paintIcon(this, g, r, c); } } } 5.1.4 边框包   5.1.5 边框接口   5.1.6 AbstractBoorder类   5.1.7 边框库——共享边框

19、   例5-4 从边框库中获得边框 import java.awt.*; import javax.swing.*; import javax.swing.border.*; public class Test extends JApplet { public void init() { Container contentPane = getContentPane(); JPanel panel = new JPanel(); JPanel panel2 = new JPanel(); Border border = BorderFact

20、ory.createRaisedBevelBorder(); Border border2 = BorderFactory.createRaisedBevelBorder(); panel.setBorder(border); panel2.setBorder(border2); contentPane.add(panel, BorderLayout.NORTH); contentPane.add(panel2, BorderLayout.SOUTH); if(border == border2) System.out.println(

21、"bevel borders are shared"); else System.out.println("bevel borders are NOT shared"); } } 5.1.8 替换内置边框   5.1.9 实现定制的边框   例5-5 HandleBorder类清单 import java.awt.*; import javax.swing.*; import javax.swing.border.*; public class HandleBorder extends AbstractBorder { protected

22、Color lineColor; protected int thick; public HandleBorder() { this(Color.black, 6); } public HandleBorder(Color lineColor, int thick) { this.lineColor = lineColor; this.thick = thick; } public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { G

23、raphics copy = g.create(); if(copy != null) { try { copy.translate(x,y); paintRectangle(c,copy,w,h); paintHandles(c,copy,w,h); } finally { copy.dispose(); } } } public Insets getBorderInsets() { return new Insets(thick,thick,thick,thick); } protec

24、ted void paintRectangle(Component c, Graphics g, int w, int h) { g.setColor(lineColor); g.drawRect(thick/2,thick/2,w-thick-1,h-thick-1); } protected void paintHandles(Component c, Graphics g, int w, int h) { g.setColor(lineColor); g.fillRect(0,0,thick,thick); //

25、 upper left g.fillRect(w-thick,0,thick,thick); // upper right g.fillRect(0,h-thick,thick,thick); // lower left g.fillRect(w-thick,h-thick,thick,thick); // lower right g.fillRect(w/2-thick/2,0,thick,thick); // mid top g.fillRect(0,h/2-thick/2,thick,thick); // mid left g.fillRect(w/2

26、thick/2,h-thick,thick,thick); // mid bottom g.fillRect(w-thick,h/2-thick/2,thick,thick); // mid right } } 例5-6 使用定制边框 import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class Test extends JApplet { public void init() { Conta

27、iner contentPane = getContentPane(); JPanel[] panels = { new JPanel(), new JPanel(), new JPanel() }; Border[] borders = { new HandleBorder(), new HandleBorder(Color.red, 8), new HandleBorder(Color.blue, 10) }; contentPane.setLayout( new FlowLayout(FlowLa

28、yout.CENTER,20,20)); for(int i=0; i < panels.length; ++i) { panels[i].setPreferredSize(new Dimension(100,100)); panels[i].setBorder(borders[i]); contentPane.add(panels[i]); } } }   5.2 图标 例5-7 一个绘制图标的小应用程序 import com.sun.java.swing.JApplet; import java.awt.Color; imp

29、ort java.awt.Graphics; public class IconTest extends JApplet { ColorIcon redIcon; ColorIcon blueIcon; ColorIcon yellowIcon; public void paint(Graphics g) { redIcon.paintIcon(this, g, 0, 0); blueIcon.paintIcon(this, g, redIcon.getIconWidth() + 10, 0);

30、 yellowIcon.paintIcon(this, g, redIcon.getIconWidth() + 10 + blueIcon.getIconWidth() + 10, 0); } public IconTest() { redIcon = new ColorIcon(Color.red, 50, 50); blueIcon = new ColorIcon(Color.blue, 60, 60); yellowIcon = new ColorIcon(Color.yellow, 70, 70);

31、 } } 例5-8 ColorIcon类清单 import java.awt.*; import javax.swing.*; class ColorIcon implements Icon { private Color fillColor; private int w, h; public ColorIcon(Color fillColor, int w, int h) { this.fillColor = fillColor; this.w = w; this.h = h; } public void paintIc

32、on(Component c, Graphics g, int x, int y) { 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); } public int getIconWidth() { return w; } public int getIconHeight() { return h; } }     5.2.1 把图标与组件相关联   例5-9

33、菜单项中的图标 import java.awt.*; import javax.swing.*; public class Test extends JApplet { ColorIcon redIcon = new ColorIcon(Color.red, 40, 15), blueIcon = new ColorIcon(Color.blue, 40, 15), yellowIcon = new ColorIcon(Color.yellow, 40, 15); public void init() { JMenuBar mb = new

34、 JMenuBar(); JMenu colors = new JMenu("Colors"); colors.add(new JMenuItem(redIcon)); colors.add(new JMenuItem(blueIcon)); colors.add(new JMenuItem(yellowIcon)); mb.add(colors); setJMenuBar(mb); } } 5.2.2 在组件中共享图标   例5-10 修改后的ColorIcon类清单 import java.awt.*; import ja

35、vax.swing.*; class ColorIcon implements Icon { private Color fillColor; private int w, h; public ColorIcon(Color fillColor, int w, int h) { this.fillColor = fillColor; this.w = w; this.h = h; } public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color

36、black); g.drawRect(x, y, w-1, h-1); g.setColor(fillColor); g.fillRect(x+1, y+1, w-2, h-2); } public int getIconWidth() { return w; } public int getIconHeight() { return h; } } 例5-11 在许多组件中共享单图标的小应用程序 import java.awt.*; import java.awt.event.*; import javax.swing.*;

37、 public class Test extends JApplet { private ColorIcon colorIcon = new ColorIcon(40, 15); private JPopupMenu popup = new JPopupMenu(); private JButton button = new JButton("select a color ...", colorIcon); public void init() { addPopupMenuItems(); button.putClientProp

38、erty("fill color", Color.red); Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Dimension buttonsz = button.getSize();

39、 popup.show(button,buttonsz.width, buttonsz.height); } }); } private void addPopupMenuItems() { JMenuItem redItem = new JMenuItem(colorIcon), blueItem = new JMenuItem(colorIcon), grayItem = new JMenuItem(colorIcon), yellowItem = new JMenuItem(colorIcon),

40、 blackItem = new JMenuItem(colorIcon), whiteItem = new JMenuItem(colorIcon), orangeItem = new JMenuItem(colorIcon); MenuItemListener listener = new MenuItemListener(); redItem.putClientProperty("fill color", Color.red); redItem.addActionListener(listener); popup.add(re

41、dItem); blueItem.putClientProperty("fill color", Color.blue); blueItem.addActionListener(listener); popup.add(blueItem); grayItem.putClientProperty("fill color", Color.gray); grayItem.addActionListener(listener); popup.add(grayItem); yellowItem.putClientProperty("fill colo

42、r", Color.yellow); yellowItem.addActionListener(listener); popup.add(yellowItem); blackItem.putClientProperty("fill color", Color.black); blackItem.addActionListener(listener); popup.add(blackItem); whiteItem.putClientProperty("fill color", Color.white); whiteItem.addActionL

43、istener(listener); popup.add(whiteItem); orangeItem.putClientProperty("fill color", Color.orange); orangeItem.addActionListener(listener); popup.add(orangeItem); } class MenuItemListener implements ActionListener { public void actionPerformed(ActionEvent e) { JComponent jc =

44、 (JComponent)e.getSource(); button.putClientProperty("fill color", jc.getClientProperty("fill color")); button.repaint(); } } }   5.2.3 图像图标   例5-12 有一个ImageIcon的小应用程序 import java.awt.*; import javax.swing.*; public class Test extends JApplet { ImageIcon icon

45、 = new ImageIcon(this.getClass().getResource("coffeeCup.jpg")); public void paint(Graphics g) { icon.paintIcon(this, g, 20, 15); } } 5.2.4 动画的图像图标   例5-13 带一个动画的图标的小应用程序 import java.awt.*; import javax.swing.*; public class Test extends JApplet { public void init() { JPane

46、l panel = new MyJPanel(); getContentPane().add(panel, "Center"); } } class MyJPanel extends JPanel { ImageIcon animatedIcon = new ImageIcon(getClass().getResource("globe.gif")); public void paintComponent(Graphics g) { super.paintComponent(g); animatedIcon.paintIcon(this, g, 20, 2

47、0); } } 5.3 动作   例5-14 带一个菜单条的小应用程序 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test extends JApplet { public void init() { JMenuBar mb = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenuItem exitItem = new JMenuItem("exit");

48、 exitItem.addActionListener(new ExitListener()); fileMenu.add(exitItem); mb.add(fileMenu); setJMenuBar(mb); } } class ExitListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } 例5-15 用一个动作创建一个菜单项 import java.awt.*; import j

49、ava.awt.event.*; import javax.swing.*; public class Test extends JApplet { public void init() { JMenuBar mb = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.add(new ExitAction()); mb.add(fileMenu); setJMenuBar(mb); } } class ExitAction extends AbstractAction

50、{ public ExitAction() { super("exit"); } public void actionPerformed(ActionEvent e) { System.exit(0); } }   5.3.1 作为控制中心点的动作   例5-16 与一个工具条按钮和一个菜单项相关联的动作 import java.awt.*; import java.awt.event.*; import java.beans.*; import javax.swing.*; public class Test extends JAp

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服