10

这是示例

如果您TranslucentWindow在支持 jdk7 的 NetBeans IDE 7.0 中构建并运行,您将收到以下异常:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
    at java.awt.Frame.setOpacity(Frame.java:960)
    at main.TranslucentWindow.<init>(TranslucentWindow.java:23)
    at main.TranslucentWindow$1.run(TranslucentWindow.java:47)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
BUILD SUCCESSFUL (total time: 1 second)

根据此堆栈跟踪,有问题的行是tw.setOpacity(0.55f). 但是,正如错误所示,如果您setUndecorated(true)在框架上调用,那么它不会抛出异常并会创建一个半透明的窗口,尽管没有任何装饰(这很痛苦)。这是正常的吗?这段代码不应该“开箱即用”吗?我忽略了什么吗?

编辑

为什么他们的半透明窗口看起来装饰,或者这是自定义渲染?

这是什么...

在此处输入图像描述

应该是什么...

在此处输入图像描述

4

5 回答 5

17

直接来自JDK7 中 java.awt.frame.setOpacity() 的 JavaDocs

必须满足以下条件才能将不透明度值设置为小于 1.0f:

  • TRANSLUCENT 半透明必须得到底层系统的支持
  • 窗口必须是未装饰的(参见 setUndecorated(boolean) 和 Dialog.setUndecorated(boolean))
  • 窗口不得处于全屏模式(请参阅 GraphicsDevice.setFullScreenWindow(Window))

如果请求的 opacity 值小于 1.0f,并且不满足以上任一条件,则窗口不透明度不会改变,并且会抛出 IllegalComponentStateException。

您看到的行为已记录在案,并且是预期行为。

于 2011-06-06T23:51:31.250 回答
4

这是一个经过验证的错误。我已向 Oracle 发送了有关使用 JDK 1.7.0 或 JRE7 的默认安装失败的示例代码的信息。使用下面编译成 TranslucentWindow.java 的源代码,它会失败并产生上面最初指出的异常。

来自网页Oracle's Translucency / Shaped Windows Page

// Taken from http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html#uniform 
import java.awt.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;

public class TranslucentWindow extends JFrame {
public TranslucentWindow() {
    super("TranslucentWindow");
    setLayout(new GridBagLayout());

    setSize(300,200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Add a sample button.
    add(new JButton("I am a Button"));
}

public static void main(String[] args)  {
        // Determine if the GraphicsDevice supports translucency.
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        //If translucent windows aren't supported, exit.
        if (!gd.isWindowTranslucencySupported(TRANSLUCENT))
    {
        System.err.println("Translucency is not supported");
        System.exit(0);
    }

    // Create the GUI on the event-dispatching thread
    SwingUtilities.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            TranslucentWindow tw = new TranslucentWindow();
            // Set the window to 55% opaque (45% translucent).
            tw.setOpacity(0.55f);
            // Display the window.
            tw.setVisible(true);
        }
    });
}

}

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
    at java.awt.Frame.setOpacity(Frame.java:960)
    at TranslucentWindow$1.run(TranslucentWindow.java:38)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
于 2011-08-31T18:25:45.667 回答
2

com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.5f)在 JDK 7 中使用。

这里

于 2011-12-09T21:33:53.303 回答
2

我认为您需要在 setBackground 之前 setUndecorated 这将解决问题

于 2015-12-11T17:52:30.527 回答
0

嗨,这段代码的问题是它在 main() 方法中缺少以下代码行:

JFrame.setDefaultLookAndFeelDecorated(true);

它应该紧跟在检查是否不支持半透明窗口并退出的代码之后:

    //If translucent windows aren't supported, exit.
    if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
        System.err.println(
            "Translucency is not supported");
            System.exit(0);
    }

    JFrame.setDefaultLookAndFeelDecorated(true);

    // Create the GUI on the event-dispatching thread
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            TranslucentWindow tw = new TranslucentWindow();

            // Set the window to 55% opaque (45% translucent).
            tw.setOpacity(0.55f);

            // Display the window.
            tw.setVisible(true);
        }
    });

此外,统一半透明图像的图像具有误导性,因为它使用 java 外观。相反,图像应该使用 Windows 系统的外观和感觉(假设您使用的是 Windows)。如果您尝试使用 Java 外观(即JFrame.setDefaultLookAndFeelDecorated(false);),那么它将抛出与以前相同的错误。我希望半透明窗口可以与 Java 外观和感觉一起使用,但我认为这是不可能的。

于 2011-12-14T05:19:04.980 回答