2013-04-25 25 views
8

看看這張圖: Transparent JFrame 能在JDK7不是透明的和未修飾的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); 

這工作不錯,但試圖啓用時加入

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

的LookAndFeel它給了我這個錯誤

螺紋

例外 「AWT-EventQueue的 - 0」 java.awt.IllegalComponentStateException:框架裝飾

這是什麼錯誤?以及如何解決它?

感謝您的回答和建議。

編輯

問問題/ CrossPosted

+2

變化的主要方法的LAF UI之前,形成 – 2013-04-25 15:43:48

+0

'@Sri戒Chilakapati'我很抱歉,但如果你描述更多 – Azad 2013-04-25 15:50:08

+0

我沒有得到你,我會理解的問題是,既然你在啓用透明度後設置外觀。它給出了例外,因爲靈氣不支持裝飾框架。 – 2013-04-25 16:26:25

回答

4
  • 接受的答案被@JamesCherrill on Daniweb

  • 1日。在InitialThread創建頂層容器必須裝飾,isDisplayable(),則有可能以後無論與搖擺定時器

  • problably需要短延時的休息。

    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

    @Azad奧馬爾請查看【JAVA/Swing的 - >創建通知JFrame,並顯示錯誤「The frame is displayable」](http://stackoverflow.com/questions/16698699/java-swing-creating-a-notification-jframe-and-the-error-the-frame -is-disp/16699136#16699136),但仍無法創建裝飾半透明窗,並且改變了L&F – mKorbel 2013-05-22 18:32:02

    5

    變化UI之前main方法的LAF由@Sri戒 Chilakapati創建

    和@Sri戒Chilakapati我很抱歉,但我沒有得到你,我會,如果你描述被 讚賞多由@Azad奧馬爾

    • 更甲骨文教程Modifying the Look and Feel

    • 問題造成的代碼行JFrame.setDefaultLookAndFeelDecorated(true);,需要從代碼中禁用/註釋這行代碼//JFrame.setDefau...

    • 默認情況下有沒有問題,創建具有靈氣大號半透明的JFrame &˚F

    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); 
          } 
         }); 
        } 
    } 
    
    +0

    感謝您的回答,但是'IllegalComponentStateException'告訴我該框架已經過裝飾。我使用jdk1.7.0_15和Netbeans 7.3 – Azad 2013-04-25 20:19:04

    +0

    問題是在這一行'AWTUtilities.setWindowOpacity(w,0.2f);' – Azad 2013-04-25 20:25:18

    +0

    aaach你是對的[也是我的bug](http:// stackoverflow .com/a/7261603/714968),所有適用於公共AWTUtilities.setWindowOpacity()的代碼在Java7中都不起作用,包括未裝飾的容器 – mKorbel 2013-04-26 07:58:04

    3

    經過一番研究,我發現問題在0123之間和com.sun.awt.AWTUtilities,我認爲我們最好不要使用com.sun軟件包,除非是最後的手段,因爲它們可能會導致升級JDK版本時出現問題(它們不屬於JDK API的一部分)。

    瞭解更多關於這個問題Here

    From Oracle

    靈氣的外觀和感覺的搖擺在JDK 6u10中引入作爲 用於替代舊的金屬在低頻。使用JDK 7,Nimbus將從Oracle專有擴展名(com.sun.java.swing)中移除 至 標準API(javax.swing),以使其成爲真正的公民首要類Swing 。

    這似乎是com.sun.awt.AWTUtilities正常工作與JDK6但雨雲LAF在JDK7。 生病少我找出我的第一個問題(這是什麼錯誤)的答案,對於第二個問題(如何解決它)我必須等到新版本com.sun發佈。

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