启用nimbus时,JDK7中无法透明且未修饰的JFrame

时间:2013-04-25 15:40:30

标签: java swing java-7 nimbus translucency

看看这张图: Transparent JFrame

这是透明框架的代码:

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

这很好但是在尝试通过添加

启用LookAndFeel时
    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-0”java.awt.IllegalComponentStateException:框架被修饰

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

感谢您的回答和建议。

修改

提问/ CrossPosted

3 个答案:

答案 0 :(得分:5)

  

在@Sri Harsha创建ui之前更改main方法中的laf   Chilakapati

     

和@Sri Harsha Chilakapati我很抱歉,但我没有得到你,我会   如果您通过@Azad Omer描述更多内容,请欣赏

  • 更多内容在Oracle教程Modifying the Look and Feel

  • 问题导致代码行JFrame.setDefaultLookAndFeelDecorated(true);,需要禁用/评论此代码行//JFrame.setDefau...

  • 默认情况下,使用Nimbus L& F创建半透明JFrame没有问题

enter image description here

来自代码

import java.awt.*;
import javax.swing.*;

public class TranslucentWindow extends JFrame {

    private static final long serialVersionUID = 1L;

    public TranslucentWindow() {
        super("Test translucent window");
        setLayout(new FlowLayout());
        add(new JButton("test"));
        add(new JCheckBox("test"));
        add(new JRadioButton("test"));
        add(new JProgressBar(0, 100));
        JPanel panel = new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
            private static final long serialVersionUID = 1L;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.red);
                g.fillRect(0, 0, getWidth(), getHeight());
            }
        };
        panel.add(new JLabel("Very long textxxxxxxxxxxxxxxxxxxxxx "));
        add(panel);
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //JFrame.setDefaultLookAndFeelDecorated(true);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Window w = new TranslucentWindow();
                w.setVisible(true);
                com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.7f);
            }
        });
    }
}

答案 1 :(得分:4)

  • @JamesCherrill on Daniweb已接受回答,

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

  • 可能需要通过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);
        }
    }
}

答案 2 :(得分:3)

经过一番研究后我发现问题出在JDK7com.sun.awt.AWTUtilities之间,我认为我们最好不要使用com.sun套餐,除非作为最后的手段,因为它们可能导致升级JDK版本的问题(它们不是JDK API的一部分)。

详细了解此问题Here

<强> From Oracle

  

Swing的Nimbus外观是在JDK 6u10中引入的   替换旧的Metal LoF。使用JDK 7,Nimbus将被移动   从Oracle专有扩展(com.sun.java.swing)到   标准API(javax.swing)使它成为真正的第一类Swing   公民。   

似乎com.sun.awt.AWTUtilitiesJDK6一起工作正常,但Nimbus LAF在JDK7中。 我发现第一个问题的答案(这是什么错误),第二个问题(如何解决)我必须等到com.sun的新版本发布。

我很感谢mKorbel的努力,谢谢。