[Java] java 圆形按钮类 →→→→→进入此内容的聊天室

来自 , 2019-05-07, 写在 Java, 查看 115 次.
URL http://www.code666.cn/view/1728efbd
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.FlowLayout;
  4. import java.awt.Graphics;
  5. import java.awt.Shape;
  6. import java.awt.geom.Ellipse2D;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11.  
  12. /**
  13.  * 制作一个圆形的按钮时,需要做两件事: 第一件事是重载一个适当的绘画方法以画出一个圆形。
  14.  * 第二件事是设置一些事件使得只有当你点击圆形按钮的范围中的时侯按钮才会作出响应
  15.  */
  16. public class CircleButton extends JButton {
  17.  
  18.         public CircleButton(String label) {
  19.                 super(label);
  20.  
  21.                 // 获取按钮的最佳大小
  22.                 Dimension size = getPreferredSize();
  23.                 size.width = size.height = Math.max(size.width, size.height);
  24.                 setPreferredSize(size);
  25.  
  26.                 setContentAreaFilled(false);
  27.         }
  28.  
  29.         // 画圆的按钮的背景和标签
  30.         protected void paintComponent(Graphics g) {
  31.  
  32.                 if (getModel().isArmed()) {
  33.                         g.setColor(Color.lightGray); // 点击时高亮
  34.                 } else {
  35.                         g.setColor(getBackground());
  36.                 }
  37.                 // fillOval方法画一个矩形的内切椭圆,并且填充这个椭圆,
  38.                 // 当矩形为正方形时,画出的椭圆便是圆
  39.                 g.fillOval(0, 0, getSize().width - 1, getSize().height - 1);
  40.  
  41.                 super.paintComponent(g);
  42.         }
  43.  
  44.         // 用简单的弧画按钮的边界。
  45.         protected void paintBorder(Graphics g) {
  46.                 g.setColor(Color.white);
  47.                 // drawOval方法画矩形的内切椭圆,但不填充。只画出一个边界
  48.                 g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);
  49.         }
  50.  
  51.         // shape对象用于保存按钮的形状,有助于侦听点击按钮事件
  52.         Shape shape;
  53.  
  54.         public boolean contains(int x, int y) {
  55.  
  56.                 if ((shape == null) || (!shape.getBounds().equals(getBounds()))) {
  57.                         // 构造一个椭圆形对象
  58.                         shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
  59.                 }
  60.                 // 判断鼠标的x、y坐标是否落在按钮形状内。
  61.                 return shape.contains(x, y);
  62.         }
  63.  
  64.         public static void main(String[] args) {
  65.                 JButton button = new CircleButton("");
  66.                 button.setBackground(Color.orange);
  67.  
  68.                 JFrame frame = new JFrame("圆形按钮");
  69.                 frame.getContentPane().setBackground(Color.pink);
  70.                 frame.getContentPane().setLayout(new FlowLayout());
  71.                 frame.getContentPane().add(button);
  72.                 frame.setSize(200, 200);
  73.                 frame.setVisible(true);
  74.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  75.         }
  76.  
  77. }

回复 "java 圆形按钮类"

这儿你可以回复上面这条便签

captcha