开发者

Transparency in Java

开发者 https://www.devze.com 2023-01-24 21:16 出处:网络
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 th

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

精彩评论

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