简体   繁体   English

如何删除圆形 JButton 的“阴影”背景/边框?

[英]How to remove "shaded" background/border of my rounded JButton?

I have a rounded JButton in my JFrame but it has the sharp corners of which I assume is the default background/border for the JButton itself.我的 JFrame 中有一个圆形 JButton,但我认为它的尖角是 JButton 本身的默认背景/边框。 I would like to remove that "shaded" tint so that it blends well with the frame color.我想去除那种“阴影”色调,使其与框架颜色很好地融合在一起。

For example, I've changed the background color of the button to RED and the corners of the borders are still visible.例如,我已将按钮的背景颜色更改为红色,并且边框的角仍然可见。

Red button with visible background/border带有可见背景/边框的红色按钮

Anyway to go about this?无论如何,go 关于这个?

startButton = new JButton("Start!");
startButton.setPreferredSize(new Dimension(150,80));
startButton.setBorder(new RoundedButton(20));
startButton.setBackground(Color.RED);
startButton.setFont(new Font(null, Font.BOLD, 20));
startButton.addActionListener(this);

class RoundedButton implements Border 
{
    private int roundRadius;
    
    RoundedButton(int roundRadius) 
    {
        this.roundRadius = roundRadius;
    }

    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
    {
        g.drawRoundRect(x, y, width-1, height-1, roundRadius, roundRadius);
    }

    public Insets getBorderInsets(Component c) 
    {
        // TODO Auto-generated method stub
        return new Insets(this.roundRadius+1, this.roundRadius+1, this.roundRadius+2, this.roundRadius+2);
    }

    public boolean isBorderOpaque()
    {
        return true;
    }
}

Please see below.请看下文。 Use it as you normally do in your provided example, ie, startButton.setBorder(new RoundedButton(20));在您提供的示例中像往常一样使用它,即startButton.setBorder(new RoundedButton(20)); passing the radius parameter.传递半径参数。

Also, optionally, you can add the next line of code, ie, startButton.setFocusPainted(false);此外,您还可以选择添加下一行代码,即startButton.setFocusPainted(false); to disable the focus border of the JButton.禁用 JButton 的焦点边框。

class RoundedButton implements Border {
    private int roundRadius;

    RoundedButton(int roundRadius) {
        this.roundRadius = roundRadius;
    }

    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int r = roundRadius;
        int w = width - 1;
        int h = height - 1;

        Area round = new Area(new RoundRectangle2D.Float(x, y, w, h, r, r));
        Container parent = c.getParent();
        if (Objects.nonNull(parent)) {
            g2.setPaint(parent.getBackground());
            Area corner = new Area(new Rectangle2D.Float(x, y, width, height));
            corner.subtract(round);
            g2.fill(corner);
        }
        g2.draw(round);
        g2.dispose();
    }

    public Insets getBorderInsets(Component c) {
        return new Insets(this.roundRadius + 1, this.roundRadius + 1, this.roundRadius + 2, this.roundRadius + 2);
    }

    public boolean isBorderOpaque() {
        return true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM