【问题标题】:How to make a transparent JFrame but keep everything else the same?如何制作透明的 JFrame 但保持其他一切不变?
【发布时间】:2013-02-02 09:25:06
【问题描述】:

我想让JFrame 透明,但它上面的图像不透明。这就是我现在拥有的:

有谁知道只有JFrame 透明的方法?

这是我的代码:

import javax.swing.*;
import java.awt.*;
import com.sun.awt.AWTUtilities;
import static java.awt.GraphicsDevice.WindowTranslucency.*;

public class SplashDemo extends JFrame
{
    public SplashDemo()
    {
        setUndecorated(true);
        setSize(200, 200);

        add(new JLabel(new ImageIcon("puppy2.png"))); 
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

        setOpacity(0.85f);      
    }

     public static void main(String[] args) 
     {
        new SplashDemo();
     }
}

【问题讨论】:

  • 使框架透明。允许内容窗格半透明,内容不透明

标签: java swing jframe transparency translucency


【解决方案1】:

基本上,您需要制作一个透明的窗口和一个半透明的内容窗格。这意味着添加到内容窗格的任何内容都将继续呈现,而无需额外的 alphering...

public class TranscluentWindow {

    public static void main(String[] args) {
        new TranscluentWindow();
    }

    public TranscluentWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }

                    JWindow frame = new JWindow();
                    frame.setAlwaysOnTop(true);
                    frame.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            if (e.getClickCount() == 2) {
                                SwingUtilities.getWindowAncestor(e.getComponent()).dispose();
                            }
                        }
                    });
                    frame.setBackground(new Color(0,0,0,0));
                    frame.setContentPane(new TranslucentPane());
                    frame.add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/Puppy.png")))));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
        });
    }

    public class TranslucentPane extends JPanel {

        public TranslucentPane() {
            setOpaque(false);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 

            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.SrcOver.derive(0.85f));
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());

        }

    }

}

【讨论】:

  • +1,很酷。 JDK7玩的不多。我没有意识到让顶层窗口透明变得如此容易。看起来您需要做的就是使用透明背景颜色。
  • @camickr 这是基础(我没有检查与 GraphicsConfiguration 的兼容性,但它比 Java 6 中的 AWTUtilities 方法简单得多)
  • 我对这段代码有一个问题:如果我尝试在小狗周围移动,框架没有正确重新绘制,这意味着在绘制新的之前没有清除以前的小狗。有什么提示可以避免这种行为吗?
  • @Sharcoux 什么操作系统?什么 JRE?
  • @Sharcoux 你想移动图像还是框架?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
  • 2017-02-21
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多