jbutton设置边框为圆角矩形
时间: 2024-05-03 18:20:28 浏览: 100
jquery实现圆角边框
4星 · 用户满意度95%
您可以使用下面这段代码来设置JButton的边框为圆角矩形:
```
jbutton.setBorder(new RoundBorder(20));
```
其中,RoundBorder是自定义的一个类,代码如下:
```
import java.awt.*;
import java.awt.geom.*;
import javax.swing.border.*;
public class RoundBorder implements Border {
private int radius;
public RoundBorder(int radius) {
this.radius = radius;
}
public Insets getBorderInsets(Component c) {
return new Insets(this.radius + 1, this.radius + 1, this.radius + 2, this.radius);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(c.getBackground());
g2.fill(new RoundRectangle2D.Double(x, y, width - 1, height - 1, radius, radius));
g2.setColor(c.getForeground());
g2.draw(new RoundRectangle2D.Double(x, y, width - 1, height - 1, radius, radius));
}
}
```
这样,您就可以把JButton的边框设置为圆角矩形了。
阅读全文