如何使 JFrame 透明?

2022-01-24 00:00:00 transparent java swing jframe

如何让JFrame透明?我想让我的 JFrame 透明.当我的 JFrame 位于其顶部时,用户应该会看到背景.

How to make JFrame transparent? I want to make my JFrame transparent. User should see the background when my JFrame is on top of it.

推荐答案

如果您对使用受限 API 类没有任何异议,那么您可以使用 AWTUtilities 类和 setWindowOpacity() 该类的方法.这里 和 这里是关于如何使用它的教程?而这里是使用Java原生访问的版本.

If you do not have any objection in using restricted API classes then you can do this with AWTUtilities class and setWindowOpacity() method of that class. Here and here is a tutorial on how to use it? And here is the version using Java native access.

代码示例

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            javax.swing.JFrame fr = new NewJFrame();
            com.sun.awt.AWTUtilities.setWindowOpacity(fr, 0.7 f);
            fr.setVisible(true);
        }
    });
}

相关文章