ALL is Well!

          敏捷是一條很長的路,摸索著前進著

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            30 隨筆 :: 23 文章 :: 71 評論 :: 0 Trackbacks

          繼承JButton,做一個圓形的按鈕。

          這是一個例子,根據這個,我們還可以描畫出很多特別的UI。

           1/**  
           2 * @author bzwm  
           3 *   
           4 */
            
           5import java.awt.Color;   
           6import java.awt.Cursor;   
           7import java.awt.Dimension;   
           8import java.awt.FlowLayout;   
           9import java.awt.Graphics;   
          10import java.awt.Shape;   
          11import java.awt.event.MouseEvent;   
          12import java.awt.geom.Ellipse2D;   
          13import javax.swing.JButton;   
          14import javax.swing.JFrame;   
          15public class CircleButton extends JButton {   
          16    private Shape shape = null;// 用于保存按鈕的形狀,有助于偵聽單擊按鈕事件   
          17       
          18    public CircleButton(String label) {   
          19        super(label);   
          20        this.addMouseListener(new java.awt.event.MouseAdapter(){   
          21            /**  
          22             * {@inheritDoc}  
          23             */
            
          24            public void mouseEntered(MouseEvent e) {   
          25                ((JButton)e.getSource()).setCursor(new Cursor(Cursor.HAND_CURSOR));   
          26            }
             
          27            /**  
          28             * {@inheritDoc}  
          29             */
            
          30            public void mouseExited(MouseEvent e) {   
          31                ((JButton)e.getSource()).setCursor(new Cursor(Cursor.MOVE_CURSOR));   
          32            }
             
          33        }
          );   
          34        Dimension size = getPreferredSize();// 獲取按鈕的最佳大小   
          35        // 調整按鈕的大小,使之變成一個方形   
          36        size.width = size.height = Math.max(size.width, size.height);   
          37        setPreferredSize(size);   
          38        // 使jbutton不畫背景,即不顯示方形背景,而允許我們畫一個圓的背景   
          39        setContentAreaFilled(false);   
          40    }
             
          41    // 畫圖的按鈕的背景和標簽   
          42    protected void paintComponent(Graphics g) {   
          43        if (getModel().isArmed()) {   
          44            // getModel方法返回鼠標的模型ButtonModel   
          45            // 如果鼠標按下按鈕,則buttonModel的armed屬性為真   
          46            g.setColor(Color.LIGHT_GRAY);   
          47        }
           else {   
          48            // 其他事件用默認的背景色顯示按鈕   
          49            g.setColor(getBackground());   
          50        }
             
          51        // fillOval方法畫一個矩形的內切橢圓,并且填充這個橢圓   
          52        // 當矩形為正方形時,畫出的橢圓便是圓   
          53        g.fillOval(00, getSize().width - 1, getSize().height - 1);   
          54        // 調用父類的paintComponent畫按鈕的標簽和焦點所在的小矩形   
          55        super.paintComponents(g);   
          56    }
             
          57    // 用簡單的弧充當按鈕的邊界   
          58    protected void paintBorder(Graphics g) {   
          59        g.setColor(getForeground());   
          60        // drawOval方法畫矩形的內切橢圓,但不填充,只畫出一個邊界   
          61        g.drawOval(00, getSize().width - 1, getSize().height - 1);   
          62    }
             
          63    // 判斷鼠標是否點在按鈕上   
          64    public boolean contains(int x, int y) {   
          65        // 如果按鈕邊框,位置發生改變,則產生一個新的形狀對象   
          66        if ((shape == null|| (!shape.getBounds().equals(getBounds()))) {   
          67            // 構造橢圓型對象   
          68            shape = new Ellipse2D.Float(00, getWidth(), getHeight());   
          69        }
             
          70        // 判斷鼠標的x,y坐標是否落在按鈕形狀內   
          71        return shape.contains(x, y);   
          72    }
             
          73    public static void main(String[] args) {   
          74        JButton button = new CircleButton("Click me");// 產生一個圓形按鈕   
          75        //button.setBackground(Color.green);// 設置背景色為綠色   
          76        // 產生一個框架顯示這個按鈕   
          77        JFrame frame = new JFrame("圖形按鈕");   
          78        frame.getContentPane().setBackground(Color.yellow);   
          79        frame.getContentPane().setLayout(new FlowLayout());   
          80        frame.getContentPane().add(button);   
          81        frame.setSize(200200);   
          82        frame.setVisible(true);   
          83        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
          84    }
             
          85}


          ----2009年02月02日
          posted on 2010-09-01 11:33 李 明 閱讀(2143) 評論(0)  編輯  收藏 所屬分類: J2SE
          主站蜘蛛池模板: 长阳| 靖西县| 桂平市| 龙川县| 乐安县| 舞钢市| 确山县| 息烽县| 双柏县| 图们市| 贺州市| 深水埗区| 鹿泉市| 卢龙县| 水富县| 乌什县| 类乌齐县| 商南县| 稻城县| 澄城县| 临汾市| 黄梅县| 永年县| 中阳县| 岐山县| 马公市| 台前县| 阿克苏市| 若尔盖县| 商南县| 乃东县| 侯马市| 专栏| 唐河县| 佛教| 无锡市| 寿宁县| 霍州市| 金阳县| 隆昌县| 大余县|