如何使JFrame透明?

问题描述 投票:9回答:2

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

java swing jframe transparent
2个回答
12
投票

我找到了另一个解决方案。

将框架的背景色设置为

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

并且记住要关闭contentpane(您的JPanel或其他组件)的不透明度

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

9
投票

如果您对使用受限API类没有异议,则可以使用该类的AWTUtilities类和该方法的setWindowOpacity()方法来做到这一点。 Herehere是有关如何使用它的教程? here是使用Java本机访问的版本。

代码示例

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);
        }
    });
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.