如何将 JPanel 绘制为 Nimbus JButton?

发布于 2024-12-20 18:23:30 字数 108 浏览 1 评论 0原文

在 Nimbus 中,JButton 的外观和感觉非常整洁和准确,具有圆形边框和漂亮的背景。
我想渲染一个具有完全相同外观的 JPanel(显然它不会有按下状态等)。
我有什么选择?

In Nimbus look and feel JButtons have a very tidy and accurate look, with rounded border and nice background.
I'd like to render a JPanel with the very same look (obviously it won't have pressed state etc).
What are my options?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半﹌身腐败 2024-12-27 18:23:30

在 JPanel 上获得“按钮外观”的最简单方法可能是扩展 JPanel 并覆盖 paintComponent

这是 Nimbus JButton 的外观:

在此处输入图像描述

这是我的类似实现查看 JPanel (我在周围添加了一个空边框来显示此示例,并且角不是半透明的):

“输入”此处的图像描述"

这是我的代码(使用 渐变 ):

public class ColorDemo extends JPanel {

    private final int gradientSize = 18;
    private final Color lighterColor = new Color(250, 250, 250);
    private final Color darkerColor = new Color(225, 225, 230);
    private final Color edgeColor = new Color(140, 145, 145);
    private final Stroke edgeStroke = new BasicStroke(1);
    private final GradientPaint upperGradient = new GradientPaint(
            0, 0, lighterColor,
            0, gradientSize, darkerColor);


    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                            RenderingHints.VALUE_ANTIALIAS_ON);
        GradientPaint lowerGradient = new GradientPaint(
                0, getHeight()-gradientSize-1, darkerColor,
                0, getHeight(), lighterColor);
        g2.setPaint(upperGradient);
        g2.fillRect(0, 0, getWidth()-1 , gradientSize);
        g2.setPaint(darkerColor);
        g2.fillRect(0, gradientSize, getWidth()-1, getHeight()-gradientSize-1);
        g2.setPaint(lowerGradient);
        g2.fillRect(0, getHeight()-gradientSize, getWidth()-1, getHeight()-1);
        g2.setStroke(edgeStroke);
        g2.setPaint(edgeColor);
        g2.drawRoundRect(0, 0, getWidth()-1, getHeight()-1,
                               gradientSize/2, gradientSize/2);
    }
}

更新

这是 AgostinoX 改进的 paintComponent 方法,它解决了我代码中的角落问题

    @Override
    public void paintComponent(Graphics g) {

        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALI‌​AS_ON);
        float gradientPerc = (float)gradientSize/getHeight();
        LinearGradientPaint lgp = new LinearGradientPaint(0,0,0,getHeight()-1,
           new float[] {0, gradientPerc, 1-gradientPerc, 1f},
           new Color[] {lighterColor, darkerColor, darkerColor, lighterColor});
        g2.setPaint(lgp);
        g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1,
            gradientSize, gradientSize);
        g2.setColor(edgeColor);
        g2.setStroke(edgeStroke);
        g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1,
            gradientSize, gradientSize);
    }

另请参阅我对 如何隐藏箭头的回答JScrollBar 中的按钮,了解如何自定义 Nimbus 的外观。并查看 Nimbus 默认值了解颜色和画家。

The easiest way to get "Button look" on a JPanel is probably by extending the JPanel and override paintComponent.

Here is the Nimbus JButton look:

enter image description here

And here is my implementation of a similar look on a JPanel (I added an empty border around for showing this example, and the corners are not translucent):

enter image description here

Here is my code (using gradients):

public class ColorDemo extends JPanel {

    private final int gradientSize = 18;
    private final Color lighterColor = new Color(250, 250, 250);
    private final Color darkerColor = new Color(225, 225, 230);
    private final Color edgeColor = new Color(140, 145, 145);
    private final Stroke edgeStroke = new BasicStroke(1);
    private final GradientPaint upperGradient = new GradientPaint(
            0, 0, lighterColor,
            0, gradientSize, darkerColor);


    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                            RenderingHints.VALUE_ANTIALIAS_ON);
        GradientPaint lowerGradient = new GradientPaint(
                0, getHeight()-gradientSize-1, darkerColor,
                0, getHeight(), lighterColor);
        g2.setPaint(upperGradient);
        g2.fillRect(0, 0, getWidth()-1 , gradientSize);
        g2.setPaint(darkerColor);
        g2.fillRect(0, gradientSize, getWidth()-1, getHeight()-gradientSize-1);
        g2.setPaint(lowerGradient);
        g2.fillRect(0, getHeight()-gradientSize, getWidth()-1, getHeight()-1);
        g2.setStroke(edgeStroke);
        g2.setPaint(edgeColor);
        g2.drawRoundRect(0, 0, getWidth()-1, getHeight()-1,
                               gradientSize/2, gradientSize/2);
    }
}

UPDATE

Here is an improved paintComponent method by AgostinoX that solved the corner issue in my code.

    @Override
    public void paintComponent(Graphics g) {

        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALI‌​AS_ON);
        float gradientPerc = (float)gradientSize/getHeight();
        LinearGradientPaint lgp = new LinearGradientPaint(0,0,0,getHeight()-1,
           new float[] {0, gradientPerc, 1-gradientPerc, 1f},
           new Color[] {lighterColor, darkerColor, darkerColor, lighterColor});
        g2.setPaint(lgp);
        g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1,
            gradientSize, gradientSize);
        g2.setColor(edgeColor);
        g2.setStroke(edgeStroke);
        g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1,
            gradientSize, gradientSize);
    }

See also my answer to How to hide the arrow buttons in a JScrollBar on how you can customize the look and feel for Nimbus. And see the Nimbus defaults for colors and painters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文