roundbutton.java

来自「connectN 网络游戏。内含server and client端源程序」· Java 代码 · 共 41 行

JAVA
41
字号
package client;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;

import javax.swing.*;

class RoundButton extends JButton {
	private static final long serialVersionUID = 1L;

	//     draw backgrond and label
	public RoundButton() {
		super();
		Dimension size = getPreferredSize();
		size.width = Math.max(size.width, size.height);
		size.height = size.width / 3;
		setPreferredSize(size);
		setContentAreaFilled(false);
	}

	protected void paintComponent(Graphics g) {
		g.setColor(this.getBackground());
		g.fillOval(2, 2, getSize().width - 4, getSize().height - 4);
	}

	//    draw the boundary of button
	protected void paintBorder(Graphics g) {
		g.setPaintMode();
	}
	
	Shape shape;
	public boolean contains(int x, int y) {
		//     change the size of button if possible
		if (shape == null || !shape.getBounds().equals(getBounds())) {
			shape = new Ellipse2D.Float(2, 2, getWidth() - 4, getHeight() - 4);
		}
		return shape.contains(x, y);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?