第5章+边框.doc

上传人:本田雅阁 文档编号:2533933 上传时间:2019-04-05 格式:DOC 页数:21 大小:127.02KB
返回 下载 相关 举报
第5章+边框.doc_第1页
第1页 / 共21页
第5章+边框.doc_第2页
第2页 / 共21页
第5章+边框.doc_第3页
第3页 / 共21页
第5章+边框.doc_第4页
第4页 / 共21页
第5章+边框.doc_第5页
第5页 / 共21页
点击查看更多>>
资源描述

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

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 public void init() JPanel panel = new JPanel();panel.setBorder(new Title

4、dBorder(JPanel Border);getContentPane().add(panel, BorderLayout.CENTER); 这个小应用程序创建一个带标题的边框,这个边框传递给面板的setBorder方法。 5.1.1 边框和边衬AWT容器有一个insets属性,它定义容器的边衬。布局管理器仔细地布局一个容器中的各个组件,以便这些组件不会侵占这个容器的边衬区。容器的insets属性是一个只读属性,修改AWT容器insets属性唯一的方法是子类化一个容器并重载它的getInsets方法。5.1.2 Swing的边框类型例5-2 显示所有Swing边框类型的小应用程序impor

5、t java.awt.*;import javax.swing.*;import javax.swing.border.*;public class Test extends JApplet public void init() JPanel jpanel = new AllBordersPanel();getContentPane().add(jpanel, BorderLayout.CENTER);class AllBordersPanel extends JPanel public AllBordersPanel() JPanel bl = new PanelWithTitle(Beve

6、l Lowered),br = new PanelWithTitle(Bevel Raised),c = new PanelWithTitle(Compound),l = new PanelWithTitle(Line),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

7、= new PanelWithTitle(Etched Lowered),er = new PanelWithTitle(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.createLoweredBevel

8、Border();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);c.setBorder(Border

9、Factory.createCompoundBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.gray,10),BorderFactory.createRaisedBevelBorder(),BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.blue,5),BorderFactory.createLoweredBevelBorder();el.setBorder(BorderFactory.cr

10、eateEtchedBorder(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);

11、add(sbl); add(c); add(el);add(er); add(e); add(l);add(m); add(t);class PanelWithTitle extends JPanel private String title;public PanelWithTitle(String title) this.title = title;public void paintComponent(Graphics g) FontMetrics fm = g.getFontMetrics();Dimension size = getSize();int titleW = fm.strin

12、gWidth(title);g.setColor(Color.black);g.drawString(title, size.width/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) Dim

13、ension size = getSize();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.*;import java.awt.*;public class Test

14、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.isBorderOpaque();System.out.println(insets = + border.getBorderInsets(

15、panel);class RainPanel extends JPanel public void paintComponent(Graphics g) Icon icon = new ImageIcon(getClass().getResource(rain.gif);Dimension size = getSize(); int patchW = icon.getIconWidth(), patchH = icon.getIconHeight(); for(int r=0; r size.width; r += patchW) for(int c=0; c size.height; c +

16、= patchH)icon.paintIcon(this, g, r, c); 5.1.4 边框包5.1.5 边框接口5.1.6 AbstractBoorder类5.1.7 边框库共享边框例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();JPa

17、nel panel2 = new JPanel();Border border = BorderFactory.createRaisedBevelBorder();Border border2 = BorderFactory.createRaisedBevelBorder();panel.setBorder(border);panel2.setBorder(border2);contentPane.add(panel, BorderLayout.NORTH);contentPane.add(panel2, BorderLayout.SOUTH);if(border = border2)Syst

18、em.out.println(bevel borders are shared);elseSystem.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 Color lineColor;protected int thick

19、;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) Graphics copy = g.create();if(copy != null) try copy.translate(x,y);paintRectangle(c,c

20、opy,w,h);paintHandles(c,copy,w,h);finally copy.dispose();public Insets getBorderInsets() return new Insets(thick,thick,thick,thick);protected 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(Co

21、mponent c, Graphics g,int w, int h) g.setColor(lineColor);g.fillRect(0,0,thick,thick); / upper leftg.fillRect(w-thick,0,thick,thick); / upper rightg.fillRect(0,h-thick,thick,thick); / lower leftg.fillRect(w-thick,h-thick,thick,thick); / lower rightg.fillRect(w/2-thick/2,0,thick,thick); / mid topg.fi

22、llRect(0,h/2-thick/2,thick,thick); / mid leftg.fillRect(w/2-thick/2,h-thick,thick,thick); / mid bottomg.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

23、 void init() Container 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(FlowLayout.CENTER,20,20);for(int i=0; i panels.len

24、gth; +i) panelsi.setPreferredSize(new Dimension(100,100);panelsi.setBorder(bordersi);contentPane.add(panelsi);5.2 图标例5-7 一个绘制图标的小应用程序import com.sun.java.swing.JApplet;import java.awt.Color;import java.awt.Graphics;public class IconTest extends JApplet ColorIcon redIcon; ColorIcon blueIcon; ColorIcon

25、 yellowIcon; public void paint(Graphics g) 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); public IconTest() redIcon = new ColorIcon(Color.red, 50, 50); blueIc

26、on = new ColorIcon(Color.blue, 60, 60); yellowIcon = new ColorIcon(Color.yellow, 70, 70); 例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

27、 = w;this.h = h;public void paintIcon(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 菜单项中的图标import java.awt.*;imp

28、ort 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 JMenuBar();JMenu colors = new JMenu(Colors);colors.add(new JMenu

29、Item(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 javax.swing.*;class ColorIcon implements Icon private Color fillColor;private int w, h;public ColorIcon(Color fillColor, i

30、nt 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.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

31、-11 在许多组件中共享单图标的小应用程序import java.awt.*;import java.awt.event.*;import javax.swing.*;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() a

32、ddPopupMenuItems();button.putClientProperty(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();po

33、pup.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),blackItem = new JMenuItem(colorIcon),whiteItem = new JMenuItem

34、(colorIcon),orangeItem = new JMenuItem(colorIcon);MenuItemListener listener = new MenuItemListener();redItem.putClientProperty(fill color, Color.red);redItem.addActionListener(listener);popup.add(redItem);blueItem.putClientProperty(fill color, Color.blue);blueItem.addActionListener(listener);popup.a

35、dd(blueItem);grayItem.putClientProperty(fill color, Color.gray);grayItem.addActionListener(listener);popup.add(grayItem);yellowItem.putClientProperty(fill color, Color.yellow);yellowItem.addActionListener(listener);popup.add(yellowItem);blackItem.putClientProperty(fill color, Color.black);blackItem.

36、addActionListener(listener);popup.add(blackItem);whiteItem.putClientProperty(fill color, Color.white);whiteItem.addActionListener(listener);popup.add(whiteItem);orangeItem.putClientProperty(fill color, Color.orange);orangeItem.addActionListener(listener);popup.add(orangeItem);class MenuItemListener

37、implements ActionListener public void actionPerformed(ActionEvent e) JComponent jc = (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 JApp

38、let ImageIcon icon = 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() JPanel panel = new MyJPanel();getCo

39、ntentPane().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, 20);5.3 动作例5-14 带一个菜单条的小应用程序import java.awt.*;import java.awt.event

40、.*;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);exitItem.addActionListener(new ExitListener();fileMenu.add(exitItem);mb.add(fileMenu);setJMenuBar(mb);class ExitListener

41、 implements ActionListener public void actionPerformed(ActionEvent e) System.exit(0);例5-15 用一个动作创建一个菜单项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);fileMenu.add(new Ex

42、itAction();mb.add(fileMenu);setJMenuBar(mb);class ExitAction extends AbstractAction 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

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

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1