简体   繁体   English

如何让JFrame透明?

[英]How to make JFrame transparent?

How to make JFrame transparent?如何让JFrame透明? I want to make my JFrame transparent.我想让我的 JFrame 透明。 User should see the background when my JFrame is on top of it.当我的 JFrame 在它上面时,用户应该会看到背景。

I found another solution.我找到了另一个解决方案。

Set the background color of your frame to将框架的背景颜色设置为

// Set the frame background color to a transparent color
yourFrameHere.setBackground(new Color(0, 0, 0, 0));

And remember to set the opacity off of the contentpane (your JPanel or other component)并记住设置内容窗格(您的 JPanel 或其他组件)的不透明度

// turn off opacity of the content pane
yourContentPaneHere.setOpaque(false);

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.如果您对使用受限的 API 类没有任何异议,那么您可以使用AWTUtilities class 和该 class 的setWindowOpacity()方法来执行此操作。 Here and here is a tutorial on how to use it? 这里这里是关于如何使用它的教程? And here is the version using Java native access. 这里是使用 Java 原生访问的版本。

code example代码示例

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);
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM