我用class来制作圆形边框
班级是:
public class RoundedBorder implements Border {
int radius;
public RoundedBorder(int radius) {
this.radius = radius;
}
@Override
public Insets getBorderInsets(Component c) {
return new Insets(this.radius/2, this.radius, this.radius/2, this.radius);
}
@Override
public boolean isBorderOpaque() {
return true;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g.drawRoundRect(x,y,width-1,height-1,radius,radius);
}
}
和我使用的按钮:
JTextField login_nickname = new JTextField();
login_nickname.setBorder(new RoundedBorder(10));
login_nickname.setPreferredSize(new Dimension(150, 25));
并且它工作正常,但我想删除角落外圆角边框未使用的背景,我附加图像来解释更多我的意思,
谢谢
答案 0 :(得分:2)
我会在paintBorder()中做类似的事情:
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (c.getParent() != null) {
Color bc = g.getColor();
g.setColor(c.getParent().getBackground());
for (int r = 0; r<radius;r++){
g.drawRoundRect(x, y, width - 1, height - 1, r, r);
}
g.setColor(bc);
}
g.drawRoundRect(x, y, width - 1, height - 1, radius, radius);
}
如果组件有一些父容器,我会先用背景颜色绘制边框,然后在它上面绘制 - 我的圆边框。
答案 1 :(得分:1)
?
返回了什么?boolean isBorderOpaque();
不应该是'假'吗?