扩展按钮

<!--<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 ( 0 0 , 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 ( 0 0 , 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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值