半透明窗口错误让我头疼

时间:2013-08-26 00:45:34

标签: java swing awt transparency

我的朋友和我整个下午都在对这个程序进行故障排除。我可以在我的Mac上编译好。但是,他无法在Windows 7机器上编译它。我们都在运行Netbeans。有人可以指出可能导致他这个错误的原因。它不喜欢setBackgroundColor的声明。

显示的错误如下....

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
    at java.awt.Frame.setBackground(Frame.java:986)
    at FadeOut.<init>(FadeOut.java:24)
    at FadeOut$2.run(FadeOut.java:70)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

代码

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Paint;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class FadeOut extends javax.swing.JFrame
{
    public javax.swing.JPanel panel1;

 public FadeOut() 
{

        super("FadeOut");
        setBackground(new Color(0,0,0,0));//////////////Compiler doesn't like me
        setSize(new Dimension(800,800));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel()
        {
            @Override
            protected void paintComponent(Graphics g)
            {
                if (g instanceof Graphics2D)
                {
                    final int R = 100;
                    final int G = 100;
                    final int B = 100;
                    Paint p = new GradientPaint(0.f,0.f,new Color(R, G, B, 0),0.f, getHeight(), new Color(R, G, B, 0), true);
                    Graphics2D g2d = (Graphics2D)g;
                    g2d.setPaint(p);
                    g2d.fillRect(0,0,getWidth(),getHeight());
                }//END GRAPHICS2D
            }//END PAINTCOMPONENT
        };//END FRAME & PANEL TRANSLUCENT DEFINITION
        setContentPane(panel);
        init();
    }//END FadeOut CONSTRUCTOR

 public void init()
 {
     setLayout(new GridLayout(1,1));
     Border blueLine;
     blueLine = BorderFactory.createLineBorder(Color.decode("0xFF00FF"));

     panel1 = new JPanel(new GridLayout(1,1));
     panel1.setBorder(BorderFactory.createTitledBorder(blueLine,"I'm translucent bitches!!", TitledBorder.CENTER,2,null,Color.decode("0x007FF")));
     add(panel1);
 }




  public static void main(String args[])
    {
         SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                FadeOut gtw = new
                    FadeOut();
                // Display the window.
                gtw.setVisible(true);
            }//END RUN
        });//SWINGUTILITIES.INVOKELATER  
    }//END MAIN

}//END FadeOut

1 个答案:

答案 0 :(得分:2)

不要忘记当你得到一个奇怪的例外,你无法诊断总是在互联网上寻找它很可能其他人也有同样的问题。

我搜索了“java.awt.IllegalComponentStateException:框架被装饰”并找到了 Is The Java Tutorials Translucent Window example giving trouble to those playing with jdk7?