Java 如何在完全透明的JFrame上创建部分透明的JButton?

Java 如何在完全透明的JFrame上创建部分透明的JButton?,java,swing,transparency,jframe,jbutton,Java,Swing,Transparency,Jframe,Jbutton,我能够使JFrame完全透明,JButton部分透明,直到我将鼠标移到按钮上(不要单击)并将鼠标从按钮上移开(MouseExit称为via MouseListener)。发生的情况是,JButton的背景被再次绘制,因此,在打开和关闭按钮的几次鼠标移动之后,按钮是完全不透明的 public class ButtonExample extends JWindow { public ButtonExample( ) { JButton But = new JButton(

我能够使JFrame完全透明,JButton部分透明,直到我将鼠标移到按钮上(不要单击)并将鼠标从按钮上移开(MouseExit称为via MouseListener)。发生的情况是,JButton的背景被再次绘制,因此,在打开和关闭按钮的几次鼠标移动之后,按钮是完全不透明的

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( );
    }
}
你试过了吗

最早是作为私有API添加到JavaSE6Update10发行版中的。这个功能在JDK 7版本中被移到了公共AWT包中


我认为上面的链接可能会对您有所帮助。

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

顺便说一句:如您所见,我更改了字段名以符合java命名约定:-)

编辑

啊。。错过了,对不起。需要检查我们在SwingX中做了什么,从我的头顶上看,我想说你需要覆盖paintComponent,自己处理背景绘画,比如

        /** 
         * @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);
        }
/**
*@遗传
*/
@凌驾
受保护组件(图形g){
如果(!isOpaque()&&getBackground().getAlpha()<255){
g、 setColor(getBackground());
g、 fillRect(0,0,getWidth(),getHeight());
}
超级组件(g);
}
但是,我没有尝试,也许“变得更加不透明”又回来了。。我明天回来

编辑2

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

  • 必须报告它们不是不透明的,以免混淆默认的绘制机制
  • 必须接管背景绘画并用背景颜色填充(SwingX JXPanel f.i.通过明确支持alpha属性完成)
为了方便起见,这里有一个小runnable,背景不正确/正确,并排显示

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());
}
公共类透明按钮{
公共透明按钮(){
JWindow incorrectOpaque=createWindow(“不正确的不透明”,true);
设置位置不正确(600600);
不正确。设置可见(真);
JWindow correctOpaque=createWindow(“更正不透明”,false);
设置位置(800600);
校正不透明。设置可见(真);
}
私有JButton createButton(最终布尔不透明){
JButton but=新JButton(“测试”){
/**
*@遗传
*覆盖以接管背景绘制
*透明的颜色。
*/
@凌驾
受保护组件(图形g){
如果(!isOpaque()&&getBackground().getAlpha()<255){
g、 setColor(getBackground());
g、 fillRect(0,0,getWidth(),getHeight());
}
超级组件(g);
}
};
但是.挫折(新颜色(0,0,0,100));
但是,设置前景(新颜色(70155255));
但是,设置不透明(不透明);
返回但是;
}
私有JWindow createWindow(字符串文本,布尔不透明){
JWindow window=新的JWindow();
JButton but=createButton(不透明);
窗口。添加(但);
添加(新的JLabel(“”),BorderLayout.SOUTH);
设置不透明度(0.5f);
窗口背景(新颜色(0,0,0,0));
设置窗口大小(新尺寸(200100));
返回窗口;
}
公共静态void main(字符串[]Args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
新的透明按钮();
}
});
}
@抑制警告(“未使用”)
私有静态最终记录器LOG=Logger.getLogger(TransparentButton.class
.getName());
}

要更快地获得更好的帮助,请发布一个由Haase和Guy提供的.Buy。它涉及到所有你需要做透明组件的细节,然后是一些。总之,这是一本适合Java Swing程序员的好书。使窗口透明是没有问题的。修复方法可能包括修改组件(本例中为JButton)本身的绘制,并且与窗口直线无关。通过将不透明设置为false,按钮背景变得完全透明(未绘制),这根本不是我想要的。当不透明为假时,您能否进一步帮助我获取背景以显示?
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());
}