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 李 明 阅读(2097) 评论(0)  编辑  收藏 所属分类: J2SE

只有注册用户登录后才能发表评论。


网站导航: