启用nimbus时,JDK7中无法透明且未修饰的JFrame [英] Can't transparent and undecorated JFrame in JDK7 when enabling nimbus

查看:125
本文介绍了启用nimbus时,JDK7中无法透明且未修饰的JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这张图片:

Look at this picture :

这里是透明框架的代码:

here is the code that transparent's the frame:

GraphicsEnvironment ge = 
        GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
            System.err.println(
                "Translucency is not supported");
                System.exit(0);
        }

        JFrame.setDefaultLookAndFeelDecorated(true);

这很有效,但在尝试添加

this works good but when trying to enable LookAndFeel by adding

    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
         if ("Nimbus".equals(info.getName())) {
            javax.swing.UIManager.setLookAndFeel(info.getClassName());
            break;
          }
    }
}catch(.......)

它给了我这个错误


线程中的异常AWT-EventQueue-0java.awt.IllegalComponentStateException:框架是装饰

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated

这是什么错误?以及如何解决?

What's this error ? and how to solve it ?

感谢您的回答和建议。

编辑

提问/ CrossPosted

Question Asked/CrossPosted

Daniweb

CodeRanch

推荐答案


  • 接受 @JamesCherrill on Daniweb

    1st。在InitialThread上创建的顶级容器必须进行修饰并且isDisplayable(),然后可以使用其余的

    1st. Top-Level Container created on InitialThread must be decorated and isDisplayable(), then after is possible whatever with rest of

    可能需要通过Swing Timer进行短延迟

    problably required short delaying by Swing Timer

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Shape;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Ellipse2D;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class DemoWindows implements ActionListener {
    
        public static void main(String[] args) {
            // create a new demo, and update it every 50 mSec
            new Timer(30, new DemoWindows()).start();
        }
        int phase = 0; // demo runs a number of consecutive phases
        int count = 0; // each of which takes a number of timesteps
        JFrame window1 = new JFrame("Java windows demo");
        JLabel text1 = new JLabel("<HTML><H1>Hello" + "<BR>Everyone");
        // "<HTML><H1>This is a demo of some of the effects"
        // + "<BR>that can be achieved with the new Java"
        // + "<BR>transparent window methods</H1>"
        // + "<BR>(requires latest version of Java)");
        JFrame window2 = new JFrame("Java windows demo");
        JLabel text2 = new JLabel("<HTML><center>Java<BR>rocks");
        JButton button = new JButton("Whatever");
        int w, h, r, x, y; // parameters of iris circle
    
        DemoWindows() {
            // build and diplay the windows
            window1.add(text1);
            window1.pack();
            window1.setLocationRelativeTo(null);
            window1.setVisible(true);
            window2.setUndecorated(true);
            window2.setBackground(new Color(0, 0, 0, 0)); // alpha <1 = transparent
            window2.setOpacity(0.0f);
            text2.setFont(new Font("Arial", 1, 60));
            text2.setForeground(Color.red);
            window2.add(text2);
            window2.add(button, BorderLayout.SOUTH);
            window2.pack();
            window2.setLocationRelativeTo(null);
            window2.setVisible(true);
            // parameters of the smallest circle that encloses window2
            // this is the starting pouint for the "iris out" effect
            w = window2.getWidth();
            h = window2.getHeight();
            r = (int) Math.sqrt(w * w + h * h) / 2; // radius
            x = w / 2 - r; // top left coordinates of circle
            y = h / 2 - r;
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            try {// L&F changed on Runtime, repeatly fired from Swing Timer 
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(DemoWindows.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(DemoWindows.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(DemoWindows.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(DemoWindows.class.getName()).log(Level.SEVERE, null, ex);
            }
            SwingUtilities.updateComponentTreeUI(window2);
            // called by timer 20 times per sec
            // goes thru a number of phases, each a few seconds long
            switch (phase) {
                case 0: { // initial pause               
                    if (++count > 50) {
                        phase = 1; // go to next phase
                        count = 0;
                    }
                    break;
                }
                case 1: { // fade in               
                    if (++count < 100) {
                        window2.setOpacity(0.01f * count);
                    } else {
                        phase = 2; // go to next phase
                        count = 0;
                    }
                    break;
                }
                case 2: { // move               
                    if (++count < 160) {
                        if (count < 28 || count > 80) {// pause for best effect
                            window2.setLocation(window2.getX() + 1, window2.getY() + 1);
                        }
                    } else {
                        phase = 3; // go to next phase
                        count = 0;
                    }
                    break;
                }
                case 3: {// iris out                
                    if (++count < r) {
                        Shape shape = new Ellipse2D.Double(
                                x + count, y + count, 2 * (r - count), 2 * (r - count));
                        window2.setShape(shape);
                    } else {
                        phase = 99; // go to final (exit) phase
                    }
                    break;
                }
                case 99:
                    System.exit(0);
            }
        }
    }
    

    这篇关于启用nimbus时,JDK7中无法透明且未修饰的JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆