透明 JButton 仍在绘制其背景

发布于 2024-11-06 10:23:35 字数 367 浏览 3 评论 0原文

我有一个半透明 JPanel。我通过扩展 JButton 创建了一个自定义 JButton,因为我需要一个带有圆角的按钮并希望为其添加一些效果。我已将按钮设置为非透明。当我将此按钮添加到半透明 JPanel 时,它看起来很好。但在翻转时,按钮后面会涂上一个黑色补丁,看起来真的很糟糕。我在网上搜索了解决方案,但找不到有用的解决方案。 http://www.java.net/node/661798 也描述了此问题,但我无法真正使 kirillcool 的建议得以实现......任何帮助将不胜感激

I have a translucent JPanel. I have created a custom JButton by extending JButton as I required a button with rounded corners and wanted to add some effects to it. I have made the button non-opaque. When I add this button to my translucent JPanel it apears fine. But on rollover a black patch is painted behind the button which looks really crappy. I searched the net for a solution but could'nt find a useful one . This problem is also described at http://www.java.net/node/661798 but i was not able really make kirillcool's suggestion work out.....Any help will be appreciated

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

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

发布评论

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

评论(2

嗫嚅 2024-11-13 10:23:35

我相信您需要添加:

button.setContentAreaFilled( false );

I believe you need to add:

button.setContentAreaFilled( false );
待天淡蓝洁白时 2024-11-13 10:23:35

不确定是否有人仍然感兴趣...
您可以通过重写 paintComponent() 方法来解决该问题,让 Java 以您喜欢的任何形状绘制 JButton。您只需使用 setBackground() 方法将 Graphics 对象的背景设置为透明即可。您还需要在使用 clearRect() 方法绘制之前清除 Graphics 对象,然后使用 JButton 背景的 alpha 级别再次填充它。这是我的一段代码..它显示了重写的 paintComponent()。通过将其粘贴到您的 JButton 中,您应该得到一个带有圆角边缘的 JButton,即使它位于半透明背景上

private int outerRoundRectSize = 10;
private int innerRoundRectSize = 8;

public void paintComponent(Graphics g) 
{
    int h = getHeight();
    int w = getWidth();

    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

    Color GP = null;

    //////////////get rid of the black background////////////////////////
    g2d.setBackground(new Color(0,0,0,0.0f));
    g2d.clearRect(0, 0, w, h);
    g2d.setPaint(new Color(0,0,0,0.3f));
    g2d.fillRect(0, 0, w, h);
    //////////////get rid of the black background////////////////////////

    ButtonModel model = getModel();
    if(!model.isEnabled()) 
    {
        setForeground(Color.GRAY);
        GP = new Color(0.5f,0.2f,0.6f);
    }
    else
    {
        setForeground(Color.WHITE);
        if(model.isRollover()) 
        {
            GP = new Color(0.5f,0.2f,0.6f);
        } 
        else 
        {
            GP = new Color(0.0f,1.0f,0.0f);
        }   
    }
    g2d.setPaint(GP);
    Color p1 = null;
    Color p2 = null;

    if(getModel().isPressed()) 
    {
        GP = new Color(1.0f,0.0f,0.0f);
        g2d.setPaint(GP);
        p1=new Color(0.12f,0.7f,0.3f);
        p2=new Color(0.7f,0.5f,0.6f);
    } 
    else 
    {
        p1=new Color(0.0f,0.5f,0.7f);
        p2=new Color(0.0f,1.0f,1.0f);
        GP = new Color(0.0f,0.0f,1.0f);
    }

    RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, w - 1, h - 1, outerRoundRectSize, outerRoundRectSize);
    Shape clip = g2d.getClip();
    g2d.clip(r2d);
    //g2d.fillRect(0, 0, w, h);
    g2d.fillRoundRect(0, 0, w, h, outerRoundRectSize, outerRoundRectSize);
    g2d.setClip(clip);
    g2d.setPaint(p1);
    g2d.drawRoundRect(0, 0, w - 1, h - 1, outerRoundRectSize,outerRoundRectSize);
    g2d.setPaint(p2);
    g2d.drawRoundRect(1, 1, w - 3, h - 3, innerRoundRectSize,innerRoundRectSize);

    g2d.dispose();
    super.paintComponent(g);
}

not sure if someone is still interested...
you can fix the problem by overriding the paintComponent() method to let Java draw the JButton in any Shape you like. you just need to set the background of the Graphics object to transparent with setBackground() method. also you need to clear the Graphics object BEFORE drawing on it with clearRect() method and then fill it again with the alpha level of the background of your JButton. here is my piece of code.. it shows the overriden paintComponent(). by pasting it into your JButton you should get a JButton with rounded edges even if its on semi-transparant background

private int outerRoundRectSize = 10;
private int innerRoundRectSize = 8;

public void paintComponent(Graphics g) 
{
    int h = getHeight();
    int w = getWidth();

    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

    Color GP = null;

    //////////////get rid of the black background////////////////////////
    g2d.setBackground(new Color(0,0,0,0.0f));
    g2d.clearRect(0, 0, w, h);
    g2d.setPaint(new Color(0,0,0,0.3f));
    g2d.fillRect(0, 0, w, h);
    //////////////get rid of the black background////////////////////////

    ButtonModel model = getModel();
    if(!model.isEnabled()) 
    {
        setForeground(Color.GRAY);
        GP = new Color(0.5f,0.2f,0.6f);
    }
    else
    {
        setForeground(Color.WHITE);
        if(model.isRollover()) 
        {
            GP = new Color(0.5f,0.2f,0.6f);
        } 
        else 
        {
            GP = new Color(0.0f,1.0f,0.0f);
        }   
    }
    g2d.setPaint(GP);
    Color p1 = null;
    Color p2 = null;

    if(getModel().isPressed()) 
    {
        GP = new Color(1.0f,0.0f,0.0f);
        g2d.setPaint(GP);
        p1=new Color(0.12f,0.7f,0.3f);
        p2=new Color(0.7f,0.5f,0.6f);
    } 
    else 
    {
        p1=new Color(0.0f,0.5f,0.7f);
        p2=new Color(0.0f,1.0f,1.0f);
        GP = new Color(0.0f,0.0f,1.0f);
    }

    RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, w - 1, h - 1, outerRoundRectSize, outerRoundRectSize);
    Shape clip = g2d.getClip();
    g2d.clip(r2d);
    //g2d.fillRect(0, 0, w, h);
    g2d.fillRoundRect(0, 0, w, h, outerRoundRectSize, outerRoundRectSize);
    g2d.setClip(clip);
    g2d.setPaint(p1);
    g2d.drawRoundRect(0, 0, w - 1, h - 1, outerRoundRectSize,outerRoundRectSize);
    g2d.setPaint(p2);
    g2d.drawRoundRect(1, 1, w - 3, h - 3, innerRoundRectSize,innerRoundRectSize);

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