我正在尝试使用边框的组合来创建一个如下所示的按钮。

在使用BorderFactory或Bevel类时,没有选择提供宽度的选项。

是否可以在Java Swing中给BevelBorder宽度?

java - 在Java Swing中创建一定宽度的BevelBorder-LMLPHP

最佳答案

不,您不能设置BevelBorder的宽度。 BevelBorder仅在每个边缘绘制两条1px线-一条用于外部阴影,另一条用于内部阴影。不幸的是,仅调用setStroke是行不通的,因为在每个角上,一种颜色只会与另一种颜色重叠,并且会出现其他不愉快的视觉效果。

另外,您似乎要求的实际上不是BevelBorder。它是具有5种颜色规格的边框:顶部,右侧,底部,左侧和线条颜色。 BevelBorder没有线条颜色,也没有这样的颜色规范。

我制作了一个扩展AbstractBorder的类,该类应该适合您的要求:

java - 在Java Swing中创建一定宽度的BevelBorder-LMLPHP

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;

import javax.swing.border.AbstractBorder;

public class AdvancedBevelBorder extends AbstractBorder {

    private Color topColor, rightColor, bottomColor, leftColor, lineColor;
    private int borderWidth;

    public AdvancedBevelBorder(Color topColor, Color rightColor, Color bottomColor, Color leftColor, Color lineColor,
            int borderWidth) {
        setTopColor(topColor);
        setRightColor(rightColor);
        setBottomColor(bottomColor);
        setLeftColor(leftColor);
        setLineColor(lineColor);
        setBorderWidth(borderWidth);
    }

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        super.paintBorder(c, g, x, y, width, height);

        int h = height;
        int w = width;
        int bw = getBorderWidth();
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.translate(x, y);

        Polygon topPolygon = createPolygon(new Point(0, 0), new Point(w, 0), new Point(w - bw, bw), new Point(bw, bw),
                new Point(0, 0));
        g2.setColor(getTopColor());
        g2.fill(topPolygon);
        g2.setColor(getLineColor());
        g2.draw(topPolygon);

        Polygon rightPolygon = createPolygon(new Point(w - 1, 0), new Point(w - 1, h), new Point(w - bw - 1, h - bw),
                new Point(w - bw - 1, bw), new Point(w - 1, 0));
        g2.setColor(getRightColor());
        g2.fill(rightPolygon);
        g2.setColor(getLineColor());
        g2.draw(rightPolygon);

        Polygon bottomPolygon = createPolygon(new Point(0, h - 1), new Point(w, h - 1), new Point(w - bw, h - bw - 1),
                new Point(bw, h - bw - 1), new Point(0, h - 1));
        g2.setColor(getBottomColor());
        g2.fill(bottomPolygon);
        g2.setColor(getLineColor());
        g2.draw(bottomPolygon);

        Polygon leftPolygon = createPolygon(new Point(0, 0), new Point(0, h), new Point(bw, h - bw), new Point(bw, bw),
                new Point(0, 0));
        g2.setColor(getLeftColor());
        g2.fill(leftPolygon);
        g2.setColor(getLineColor());
        g2.draw(leftPolygon);

        g2.dispose();
    }

    @Override
    public Insets getBorderInsets(Component c) {
        return new Insets(getBorderWidth(), getBorderWidth(), getBorderWidth() + 1, getBorderWidth() + 1);
    }

    private Polygon createPolygon(Point... points) {
        Polygon polygon = new Polygon();
        for (Point point : points) {
            polygon.addPoint(point.x, point.y);
        }
        return polygon;
    }

    public Color getTopColor() {
        return topColor;
    }

    public void setTopColor(Color topColor) {
        this.topColor = topColor;
    }

    public Color getRightColor() {
        return rightColor;
    }

    public void setRightColor(Color rightColor) {
        this.rightColor = rightColor;
    }

    public Color getBottomColor() {
        return bottomColor;
    }

    public void setBottomColor(Color bottomColor) {
        this.bottomColor = bottomColor;
    }

    public Color getLeftColor() {
        return leftColor;
    }

    public void setLeftColor(Color leftColor) {
        this.leftColor = leftColor;
    }

    public Color getLineColor() {
        return lineColor;
    }

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }

    public int getBorderWidth() {
        return borderWidth;
    }

    public void setBorderWidth(int borderWidth) {
        this.borderWidth = borderWidth;
    }

}

顺便说一句,我用以下代码制作了可以在图像中看到的组件:
AdvancedBevelBorder border = new AdvancedBevelBorder(new Color(120, 172, 220), new Color(55, 93, 128),
        new Color(73, 124, 169), new Color(150, 191, 229), new Color(36, 83, 126), 10);
JPanel panel = new JPanel() {
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 60);
    }
};
panel.setBackground(new Color(91, 154, 212));
panel.setBorder(border);

07-24 09:32