Java教程半透明窗口示例是否给玩jdk7的人带来麻烦?

发布于 2024-11-14 07:46:54 字数 2066 浏览 0 评论 0原文

这是示例

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

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) ,那么它不会抛出异常,并且会创建一个半透明的窗口,尽管没有任何装饰(这是一个痛苦) 。这是正常的吗?这段代码不应该“开箱即用”正确运行吗?我是否忽略了什么?

编辑

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

这是什么...

在此处输入图像描述

它应该是什么...

在此处输入图像描述

Here's the example.

If you build and run TranslucentWindow in let's say, NetBeans IDE 7.0, which supports jdk7, you'll get the following exception:

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)

According to this stack trace, the offending line is tw.setOpacity(0.55f). But, as the error indicates, if you invoke setUndecorated(true) on the frame, then it won't throw the exception and will create a translucent window, albeit without any decoration (which is a pain). Is this normal? Shouldn't this code run right "out-of-the-box"? Am I overlooking something?

EDIT

Why does their translucent window appear decorated, or is this custom rendering?

What it is...

enter image description here

What it should be...

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白芷 2024-11-21 07:46:54

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

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

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

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

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

Right from the JavaDocs for java.awt.frame.setOpacity() in JDK7:

The following conditions must be met in order to set the opacity value less than 1.0f:

  • The TRANSLUCENT translucency must be supported by the underlying system
  • The window must be undecorated (see setUndecorated(boolean) and Dialog.setUndecorated(boolean))
  • The window must not be in full-screen mode (see GraphicsDevice.setFullScreenWindow(Window))

If the requested opacity value is less than 1.0f, and any of the above conditions are not met, the window opacity will not change, and the IllegalComponentStateException will be thrown.

The behavior that you are seeing is documented and is expected behavior.

风向决定发型 2024-11-21 07:46:54

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

来自网页 Oracle 的半透明/异形 Windows 页面

// 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)

This is a verified bug. I have sent Oracle information on their sample code failing using the default install of JDK 1.7.0 or JRE7. Using the source code below compiled into TranslucentWindow.java, it fails and produces the exception initially indicated above.

From web page 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)
无语# 2024-11-21 07:46:54

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

请参阅 此处

Use com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.5f) in JDK 7.

See here.

摘星┃星的人 2024-11-21 07:46:54

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

I think you need to setUndecorated before setBackground this will fix the problem

哆兒滾 2024-11-21 07:46:54

您好,此代码的问题是它在 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.setDefaultLookAndFeelDecolated(false);),那么它将抛出与以前相同的错误。我希望半透明窗口能够与 Java 的外观和感觉配合使用,但我认为这是不可能的。

Hi the problem with this code is that it is missing the following line of code in the main() method:

JFrame.setDefaultLookAndFeelDecorated(true);

It should go right after the code that checks if translucent windows aren't supported and exits:

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

Also, the image of the uniform translucent image is misleading because it uses the java look and feel. Instead the image should use the Windows system look and feel (assuming you're on Windows). If you try to use the Java look and feel (i.e., JFrame.setDefaultLookAndFeelDecorated(false);), then it will throw the same error as before. I was hoping that the translucent window would work with the Java look and feel, but I don't think this is possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文