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 
          posted on 2007-02-06 16:58 -274°C 閱讀(387) 評論(0)  編輯  收藏 所屬分類: JAVA

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 914107
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 肥乡县| 怀安县| 沧源| 红原县| 江北区| 皋兰县| 阳新县| 鄂尔多斯市| 延寿县| 清涧县| 濉溪县| 保康县| 文山县| 精河县| 彩票| 林口县| 宜阳县| 新野县| 昭平县| 上栗县| 襄城县| 汕尾市| 德庆县| 阜阳市| 云梦县| 灵璧县| 东乌| 南宫市| 田林县| 阜阳市| 霸州市| 南溪县| 卢湾区| 甘孜县| 鸡东县| 靖安县| 奎屯市| 乌拉特后旗| 五指山市| 井陉县| 东山县|