如何在完全透明的JFrame上创建部分透明的JButton? [英] How to create partly transparent JButton on fully transparent JFrame?

查看:144
本文介绍了如何在完全透明的JFrame上创建部分透明的JButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使JFrame完全透明,JButton部分透明,直到我在按钮上移动鼠标(不要单击)并从按钮移开鼠标(通过MouseListener调用MouseExited)。会发生什么是JButton的背景再次被绘制,所以在按钮上打开和关闭几个鼠标后,按钮完全不透明。

I am able to make JFrame totally transparent and the JButton is partly transparent just fine until I move my mouse on the button ( do not click ) and move the mouse off from the button ( MouseExited called via MouseListener ). What happens is that the background of the JButton is drawn again, so after couple of mouse movements on and off the button the button is totally opaque.

public class ButtonExample extends JWindow
{
   public ButtonExample( )
   {
        JButton But = new JButton( "Testing" );
        But.setBackground( new Color( 0, 0, 0, 200 ) );
        But.setForeground( new Color( 70, 155, 255 ) );
        this.add( But );
        this.setBackground( new Color( 0, 0, 0, 0 ) );
        this.setMinimumSize( new Dimension( 200,100 ) );
        this.setVisible( true );
    }

    public static void main( String[ ] Args ) 
    {
        new ButtonExample( );
    }
}


推荐答案

问题是按钮报告是完全不透明的,实际上它不是(由于部分透明的颜色)

problem is that the button reports being fully opaque when in fact it isn't (due to the partially transparent color)

  but.setOpaque(false);

BTW:如你所见,我改变了字段名称以符合java命名约定:-)

BTW: as you see I changed the field name to conform to java naming conventions :-)

编辑

arggghh ..错过了,抱歉。需要检查我们在SwingX中做什么,从我的头脑中我会说你需要覆盖paintComponent并自己处理背景画,比如

arggghh .. missed that, sorry. Need to check what we do in SwingX, from the top of my head I would say you need to override paintComponent and handle the background painting yourself, like

        /** 
         * @inherited <p>
         */
        @Override
        protected void paintComponent(Graphics g) {
            if (!isOpaque() && getBackground().getAlpha() < 255) {
                g.setColor(getBackground());
                g.fillRect(0, 0, getWidth(), getHeight());
            }
            super.paintComponent(g);
        }

没有尝试,但是,变得更不透明可能会回来再这样做..将在明天回来

didn't try, though, maybe the "getting more opaque" is back again with doing so .. will come back tomorrow

编辑2

好的,检查 - 编辑的代码正常工作。总而言之:具有半透明背景的组件

okay, checked - the edited code works correctly. So in summary: components with translucent background


  • 必须报告它们不透明以免混淆默认绘制机制

  • 必须接管背景画并用背景颜色填充它(SwingX JXPanel fi通过显式支持alpha属性)

为了您的方便,这里是一个小的可运行的错误/正确的背景并排

for your convenience, here's a small runnable with incorrect/correct background side-by-side

public class TransparentButton  {

    public TransparentButton() {
        JWindow incorrectOpaque = createWindow("incorrect opaque", true);
        incorrectOpaque.setLocation(600, 600);
        incorrectOpaque.setVisible(true);
        JWindow correctOpaque = createWindow("correct opaque", false);
        correctOpaque.setLocation(800, 600);
        correctOpaque.setVisible(true);
    }


    private JButton createButton(final boolean opaque) {
        JButton but = new JButton("Testing") {

            /**
             * @inherited <p>
             * Overridden to take over background painting with 
             * transparent color.
             */
            @Override
            protected void paintComponent(Graphics g) {
                if (!isOpaque() && getBackground().getAlpha() < 255) {
                    g.setColor(getBackground());
                    g.fillRect(0, 0, getWidth(), getHeight());
                }
                super.paintComponent(g);
            }

        };
        but.setBackground(new Color(0, 0, 0, 100));
        but.setForeground(new Color(70, 155, 255));
        but.setOpaque(opaque);
        return but;
    }

    private JWindow createWindow(String text, boolean opaque) {
        JWindow window = new JWindow();
        JButton but = createButton(opaque);
        window.add(but);
        window.add(new JLabel(""), BorderLayout.SOUTH);
        window.setOpacity(0.5f);
        window.setBackground(new Color(0, 0, 0, 0));
        window.setSize(new Dimension(200, 100));
        return window;
    }

    public static void main(String[] Args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                new TransparentButton();
            }
        });
    }

    @SuppressWarnings("unused")
    private static final Logger LOG = Logger.getLogger(TransparentButton.class
            .getName());
}

这篇关于如何在完全透明的JFrame上创建部分透明的JButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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