2013-04-25 11 views
8

Guardate questa immagine: Transparent JFrame può JFrame non trasparente e non decorati in JDK7 quando si abilita nimbo

qui è il codice che trasparente è il telaio:

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

questo funziona bene, ma quando si cerca di attivare LookAndFeel aggiungendo

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

mi dà questo errore

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: La cornice è decorata

Che cos'è questo errore? e come risolverlo?

Grazie per le vostre risposte e suggerimenti.

EDIT

Asked Question/Crosspost

+2

Modificare il LAF nel metodo principale prima ui si crea –

+0

'@Sri Harsha Chilakapati' Mi dispiace, ma non ho avuto te sarò apprezzato se si descrivono più – Azad

+0

Il problema è che dal momento che stanno dando il tocco dopo aver attivato la trasparenza. Dà l'eccezione poiché nimbus non ha il supporto per cornici decorate. –

risposta

4
  • risposta accettata dalla @JamesCherrill on Daniweb,

  • prima. Top-Level Container creato InitialThread deve essere decorata e isDisplayable(), poi dopo è possibile tutto ciò con il riposo di

  • problably richieste breve ritardo da 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

@Azad Omer per favore vedere [Java/Swing - > Creazione di una notifica JFrame e l'errore "La cornice è visualizzabile"] (http://stackoverflow.com/questions/16698699/java-swing-creating-a-notification-jframe-and-the-error-the-frame -is-disp/16699136 # 16699136), ma non è ancora possibile creare finestre traslucide decorate e con L & F modificato – mKorbel

5

Modificare il LAF nel metodo principale prima ui è creato da @Sri Harsha Chilakapati

e @Sri Harsha Chilakapati Mi dispiace, ma non ho avuto te sarò apprezzato se descrivete more di @Azad Omer

  • ancora a Oracle esercitazione Modifying the Look and Feel,

  • problema è causato linea codice JFrame.setDefaultLookAndFeelDecorated(true);, tenuti a disattivare/commentare questa linea di codice //JFrame.setDefau...

  • di default non c'è problema per creare JFrame traslucido con Nimbus L & F

enter image description here

dal codice

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

Grazie per la risposta, ma 'IllegalComponentStateException' mi dice che la cornice è decorata. per informazioni: sto usando jdk1.7.0_15 e Netbeans 7.3 – Azad

+0

Il problema è in questa riga 'AWTUtilities.setWindowOpacity (w, 0.2f);' – Azad

+0

aaach hai ragione [bug anche per me] (http: // stackoverflow .com/a/7261603/714968), tutto il codice che funziona per public AWTUtilities.setWindowOpacity(), non funziona in Java7, incluso il contenitore non decorato – mKorbel

3

Dopo alcune ricerche, scopro che il problema è tra JDK7 e com.sun.awt.AWTUtilities, penso che sia meglio non utilizzare i pacchetti com.sun tranne che come ultima risorsa in quanto potrebbero causare problemi con l'aggiornamento delle versioni di JDK (non fanno parte dell'API JDK).

Ulteriori informazioni su questo emette Here

From Oracle

Il Nimbus look-and-feel per Swing è stato introdotto nel JDK 6u10 in sostituzione per il vecchio metallo LoF. Con JDK 7, Nimbus verrà spostato da un'estensione proprietaria di Oracle (com.sun.java.swing) a un'API standard (javax.swing) in modo che diventi un vero cittadino di prima classe Swing .

Sembra essere com.sun.awt.AWTUtilities funziona bene con JDK6 ma Nimbus LAF è in JDK7. Problemi di meno Scopro la risposta alla mia prima domanda (Che cos'è questo errore), e per la seconda domanda (Come risolverlo) Devo aspettare la nuova versione della versione com.sun.

Sono grato per gli sforzi di mKorbel, grazie.

Problemi correlati