开发者

Transparency in Java

I've been using the new com.sun.awt.AWTUtilities class and am intrigued. I got com.sun.awt.AWTUtilities/setWindowOpacity(java.awt.Window window, float f) to work perfectly, but am now wondering if there is any way to change the opacity of an individual component, such as 开发者_JAVA技巧a javax.swing.JInternalFrame or javax.swing.JButton.


Try this:

class TransparentButton extends JButton {
        public TransparentButton(String text) { 
            super(text);
            setOpaque(false); 
        } 

        public void paint(Graphics g) { 
            Graphics2D g2 = (Graphics2D) g.create(); 
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); 
            super.paint(g2); 
            g2.dispose(); 
        } 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜