如何将JPanel绘制为Nimbus JButton? [英] How to draw a JPanel as a Nimbus JButton?

查看:146
本文介绍了如何将JPanel绘制为Nimbus JButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Nimbus的外观和感觉JButtons外观非常整洁,具有圆形边框和漂亮的背景。

我想渲染具有相同外观的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?

推荐答案

在JPanel上获取按钮外观的最简单方法可能是通过扩展 JPanel 并覆盖 paintComponent

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

这是Nimbus JButton 外观:

Here is the Nimbus JButton look:

这是我在 JPanel <上的类似外观的实现/ code>(为了显示这个例子,我添加了一个空边框,并且边角不是半透明的):

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):

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

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);
    }
}

更新

以下是 AgostinoX 的改进 paintComponent 方法解决了角点问题在我的代码中。

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);
    }

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

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.

这篇关于如何将JPanel绘制为Nimbus JButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆