文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>探秘JDK 7之三:JLayer装饰Swing组件(2)

探秘JDK 7之三:JLayer装饰Swing组件(2)

时间:2010-08-31  来源:mydear

其中createLayer()方法是最重要的代码,它创建了一个匿名LayerUI子类的实例,绘制了墙纸和JPanel视图,创建了UI,在实例中包含了UI的面板容器。

绘制操作是由LayerUI's public void paint(Graphics g,JComponent c)方法实现的,第二个参数引用了视图(被装饰的组件)中的JLayer实例,不是引用的视图。

在视图后创建了渐变渲染墙纸后,调用paint()方法确保视图(没有嵌套面板的单一面板)是透明的,它将会隐藏墙纸,然后绘制视图。

paint()对比paintLayer()

JDK文档中除了提到paint()方法外,还提到了paintLayer()方法,我这里之所以选择paint()方法,是因为LayerUI中 不存在paintLayer(),此外,文档还错误地引用paintLayer() doesn't exist in LayerUI. Furthermore,the documentation incorrectly refers to addPropertyChange(),configureGraphics(),processComponentEvent(),processFocusEvent(),processHierarchyBoundsEvent(),processHierarchyEvent(),processKeyEvent(),processMouseEvent(),processMouseMotionEvent(),processMouseWheelEvent(),and repaintLayer()这些在LayerUI中根本不存在的方法,当然这些方法也可能在JDK 7最终发布时会包含进来。

图1显示了有墙纸背景的UI。

498)this.style.width=498;" class="fit-image" />
图1 在用户界面后加了一层墙纸背景

ReverseText程序演示了自定义绘制,避开了事件触发,不需要检测事件,因为程序只关心墙纸的绘制效果。相反,清单2显示了一个需要响应鼠标移动事件的程序代码。

清单2. BrandedUI.java

  1. // BrandedUI.java
  2. import java.awt.AWTEvent;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.EventQueue;
  6. import java.awt.Font;
  7. import java.awt.GradientPaint;
  8. import java.awt.Graphics;
  9. import java.awt.Graphics2D;
  10. import java.awt.GridLayout;
  11. import java.awt.Point;
  12. import java.awt.event.MouseEvent;
  13. import javax.swing.JComponent;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JLayer;
  17. import javax.swing.JPanel;
  18. import javax.swing.JTextField;
  19. import javax.swing.SwingUtilities;
  20. import javax.swing.plaf.LayerUI;
  21. public class BrandedUI
  22. {
  23. private static Color PALE_BLUE = new Color (0.0f, 0.0f, 1.0f, 0.3f);
  24. private static Color PALE_RED = new Color (1.0f, 0.0f, 0.0f, 0.3f);
  25. private static Font BRAND_FONT = new Font ("Arial", Font.BOLD, 18);
  26. private static String MSG = "My brand";
  27. private static JLayer<JPanel> createLayer ()
  28. {
  29. LayerUI<JPanel> layerUI;
  30. layerUI = new LayerUI<JPanel> ()
  31. {
  32. private Color color = PALE_BLUE;
  33. public void installUI (JComponent c)
  34. {
  35. super.installUI (c);
  36. ((JLayer) c).setLayerEventMask (AWTEvent.MOUSE_MOTION_EVENT_MASK);
  37. }
  38. public void eventDispatched (AWTEvent e,
  39. JLayer <? extends JPanel> l)
  40. {
  41. MouseEvent me = (MouseEvent) e;
  42. Point pt = SwingUtilities.convertPoint ((Component) me.getSource (),
  43. me.getX (), me.getY (), l);
  44. int cx = l.getWidth ()/2;
  45. int cy = l.getHeight ()/2;
  46. if (pt.x > cx-45 && pt.x < cx+45 && pt.y > cy-10 && pt.y < cy+10)
  47. color = PALE_RED;
  48. else
  49. color = PALE_BLUE;
  50. l.repaint ();
  51. }
  52. public void paint (Graphics g, JComponent c)
  53. {
  54. // Paint the view.
  55. super.paint (g, c);
  56. // Paint the brand.
  57. g.setColor (color);
  58. g.setFont (BRAND_FONT);
  59. int width = g.getFontMetrics ().stringWidth (MSG);
  60. int height = g.getFontMetrics ().getHeight ();
  61. g.drawString (MSG, (c.getWidth ()-width)/2,
  62. c.getHeight ()/2+height/4);
  63. }
  64. public void uninstallUI (JComponent c)
  65. {
  66. super.uninstallUI (c);
  67. ((JLayer) c).setLayerEventMask (0);
  68. }
  69. };
  70. // Create a user interface to be decorated.
  71. JPanel pnlMain = new JPanel ();
  72. pnlMain.setLayout (new GridLayout (2, 1));
  73. JPanel pnlTemp = new JPanel ();
  74. JLabel lblName = new JLabel ("Name:");
  75. pnlTemp.add (lblName);
  76. JTextField txtName = new JTextField (20);
  77. pnlTemp.add (txtName);
  78. pnlMain.add (pnlTemp);
  79. pnlTemp = new JPanel ();
  80. JLabel lblAddr = new JLabel ("Address:");
  81. pnlTemp.add (lblAddr);
  82. JTextField txtAddr = new JTextField (20);
  83. pnlTemp.add (txtAddr);
  84. pnlMain.add (pnlTemp);
  85. // Create the layer for the main panel using our custom layerUI.
  86. return new JLayer<JPanel> (pnlMain, layerUI);
  87. }
  88. private static void createAndShowUI ()
  89. {
  90. JFrame frame = new JFrame ("Branded UI");
  91. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  92. frame.add (createLayer ());
  93. frame.pack ();
  94. frame.setLocationRelativeTo (null);
  95. frame.setVisible (true);
  96. }
  97. public static void main (String [] args)
  98. {
  99. Runnable r = new Runnable ()
  100. {
  101. public void run ()
  102. {
  103. createAndShowUI ();
  104. }
  105. };
  106. EventQueue.invokeLater (r);
  107. }
  108. }

上面的代码渲染UI上的文本印记,我们通常使用印记提醒用户使用的是试用软件,印记文本是半透明的,以便背景可以全部显示,我们不希望这个印记给用户造成太大的干扰。

另一方面,我们希望用户能注意到这个印记,让他们下定决心购买这款软件,清单2中的代码通过改变印记的颜色(改成淡红色)来达到这个目的,当鼠标移到初始值是绿色的印记面板上时,颜色就变成淡红色。


排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载