1 ////////////////////////////////////////////////////////////////////////////////////
            2 //
            3 // 文件名: nButton.java
            4 //
            5 // 功  能: 自定義按鈕
            6 //
            7 // 備  注: 按鈕設(shè)置大小時(shí),應(yīng)比實(shí)際圖片大小寬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         //這個(gè)調(diào)用使JButton不畫(huà)背景,而允許畫(huà)一個(gè)圓的背景。
           28         setContentAreaFilled (false);
           29     }
           30     
           31     
           32     public nButton(String label)
           33     {
           34         super (label);
           35         // 這些聲明把按鈕擴(kuò)展為一個(gè)圓而不是一個(gè)橢圓。
           36         Dimension size = getPreferredSize ();
           37         size.width = size.height = Math.max (size.width, size.height);
           38         setPreferredSize (size);
           39         
           40         //這個(gè)調(diào)用使JButton不畫(huà)背景,而允許我們畫(huà)一個(gè)圓的背景。
           41         setContentAreaFilled (false);
           42     }
           43     
           44     // 畫(huà)圓的背景和標(biāo)簽
           45     protected void paintComponent (Graphics g)
           46     {
           47         if (getModel ().isArmed ())
           48         {
           49             
           50             // 可以選一個(gè)高亮的顏色作為圓形按鈕類(lèi)的屬性
           51             g.setColor (back);
           52         }
           53         else
           54         {
           55             g.setColor (back);
           56         }
           57         g.fillRect (00, getSize ().width, getSize ().height);
           58         
           59         //這個(gè)調(diào)用會(huì)畫(huà)一個(gè)標(biāo)簽和焦點(diǎn)矩形。
           60         super.paintComponent (g);
           61     }
           62     
           63     
           64     public void setImageIcon (Image img)
           65     {
           66         this.img = img;
           67     }
           68     
           69     // 用簡(jiǎn)單的弧畫(huà)按鈕的邊界。
           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     // 偵測(cè)點(diǎn)擊事件
           78     Shape shape;
           79     public boolean contains (int x, int y)
           80     {
           81         // 如果按鈕改變大小,產(chǎn)生一個(gè)新的形狀對(duì)象。
           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     //下面都是測(cè)試代碼
           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 閱讀(395) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): JAVA

          常用鏈接

          留言簿(21)

          隨筆分類(lèi)(265)

          隨筆檔案(242)

          相冊(cè)

          JAVA網(wǎng)站

          關(guān)注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 916114
          • 排名 - 40

          最新評(píng)論

          主站蜘蛛池模板: 蕲春县| 峨眉山市| 福州市| 永登县| 宜春市| 沾化县| 永顺县| 东丽区| 通江县| 嵊州市| 英德市| 德江县| 华宁县| 霍州市| 乾安县| 山东省| 寿光市| 德化县| 泽州县| 探索| 江永县| 新沂市| 东城区| 望谟县| 修武县| 龙南县| 新建县| 石柱| 桃源县| 离岛区| 来宾市| 陈巴尔虎旗| 壶关县| 清镇市| 沙湾县| 宝坻区| 措美县| 邢台市| 阜新市| 依安县| 汝南县|