无法创建半透明窗口

时间:2014-04-19 19:53:00

标签: java swing jframe transparency

我试图制作半透明的窗口,我读了很多帖子,说它根本不在Java 7中工作,有些人解决了这个问题,但是他们的方法并不是这样。为我工作。如果我尝试使面板半透明,背景只是bacomes更暗。这是我的班级:

public class MainCycle {
    public static DrawPanel drawPanel = new DrawPanel();
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
              JFrame frame = new JFrame();
              JFrame.setDefaultLookAndFeelDecorated(true);

              GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
              GraphicsDevice gd = ge.getDefaultScreenDevice();
              System.out.println(gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT));
              frame.setOpacity(0.75f);

              frame.setUndecorated(true);
              frame.setBounds(0, 0, (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
              frame.setContentPane(drawPanel);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
            }
        });
    }

}

输出:

true
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
at java.awt.Frame.setOpacity(Unknown Source)
at com.bg.my_paint_program.MainCycle$1.run(MainCycle.java:31)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

你能帮我一点吗?

1 个答案:

答案 0 :(得分:1)

在调用frame.setOpacity(0.75f);

之前,只需移动以下行
frame.setUndecorated(true);