`

扩展按钮

阅读更多
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->   1 ////////////////////////////////////////////////////////////////////////////////////
  2 //
  3 // 文件名: nButton.java
  4 //
  5 // 功  能: 自定义按钮
  6 //
  7 // 备  注: 按钮设置大小时,应比实际图片大小宽1、高1象素。
  8 //
  9 /////////////////////////////////////////////////////////////////////////////////////
 10 import java.awt.*;
 11 import java.awt.geom.*;
 12 import javax.swing.*;
 13 import java.io.*;
 14 
 15 public class nButton extends JButton
 16 {
 17     
 18     Image img;
 19     public Color back;
 20     ImageIcon icon = new ImageIcon ();
 21     public nButton()
 22     {
 23         super ();
 24         Dimension size = getPreferredSize ();
 25         size.width = size.height = Math.max (size.width, size.height);
 26         setPreferredSize (size);
 27         //这个调用使JButton不画背景,而允许画一个圆的背景。
 28         setContentAreaFilled (false);
 29     }
 30     
 31     
 32     public nButton(String label)
 33     {
 34         super (label);
 35         // 这些声明把按钮扩展为一个圆而不是一个椭圆。
 36         Dimension size = getPreferredSize ();
 37         size.width = size.height = Math.max (size.width, size.height);
 38         setPreferredSize (size);
 39         
 40         //这个调用使JButton不画背景,而允许我们画一个圆的背景。
 41         setContentAreaFilled (false);
 42     }
 43     
 44     // 画圆的背景和标签
 45     protected void paintComponent (Graphics g)
 46     {
 47         if (getModel ().isArmed ())
 48         {
 49             
 50             // 可以选一个高亮的颜色作为圆形按钮类的属性
 51             g.setColor (back);
 52         }
 53         else
 54         {
 55             g.setColor (back);
 56         }
 57         g.fillRect (00, getSize ().width, getSize ().height);
 58         
 59         //这个调用会画一个标签和焦点矩形。
 60         super.paintComponent (g);
 61     }
 62     
 63     
 64     public void setImageIcon (Image img)
 65     {
 66         this.img = img;
 67     }
 68     
 69     // 用简单的弧画按钮的边界。
 70     protected void paintBorder (Graphics g)
 71     {
 72         //g.setColor(back);
 73         //g.drawRect(-1, -1, getSize().width + 1 , getSize().height + 1);
 74     }
 75     
 76     
 77     // 侦测点击事件
 78     Shape shape;
 79     public boolean contains (int x, int y)
 80     {
 81         // 如果按钮改变大小,产生一个新的形状对象。
 82         if (shape == null ||
 83                 !shape.getBounds ().equals (getBounds ()))
 84         {
 85             shape = new Ellipse2D.Float (00, getWidth (), getHeight ());
 86         }
 87         return shape.contains (x, y);
 88     }
 89     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 90     //下面都是测试代码
 91     public void init ()
 92     {
 93        icon = new ImageIcon (getClass ().getResource ("clear1.jpg"));
 94        this.setIcon((Icon) icon) ;
 95        icon = new ImageIcon (getClass ().getResource ("clear2.jpg"));
 96        this.setRolloverIcon((Icon) icon);
 97        this.setPressedIcon ((Icon) icon);
 98         //return icon;
 99     }
100     public static void main (String args[])
101     {
102         JFrame frame = new JFrame ("test");
103         frame.setLayout(null);
104         nButton btn = new nButton();
105         btn.init();
106        btn.back = frame.getContentPane ().getBackground () ;
107         btn.setToolTipText ("test");
108         btn.setBounds (100,100 ,62,21) ;
109         frame.add (btn);
110         frame.setSize (300,300);
111         frame.setVisible (true);
112         frame.setDefaultCloseOperation (3);
113     }
114     
115 }
116 
117 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics