Piccolo esempio fatto:
#Main
codice:public class ChangeLook { public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { TestFrame frame = new TestFrame(); try { for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (Exception e) {} javax.swing.SwingUtilities.updateComponentTreeUI(frame); frame.setVisible(true); } }); } }
#TestFrame
Ho notato che facendo scendere il menù a tendina in Nimbus (il primo che viene applicato) cliccando più volte tra Nimbus, CDE Motif e Windows e anche metal (aprendo di volta in volta il popup) randomicamente dà ancora quell'errore. (bisogna avere un po di pazienza a me lo dà sempre)codice:public class TestFrame extends JFrame implements ActionListener{ private JMenuItem stretch, select, normal, none; private JPopupMenu popup; private JPanel panel, choosedPanel; public TestFrame(){ super("Test"); panel=new JPanel(); choosedPanel =new JPanel(); UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels(); for (int i = 0, n = looks.length; i < n; i++) { JButton button = new JButton(looks[i].getName()); button.setActionCommand(looks[i].getClassName()); button.addActionListener(this); panel.add(button); } popup=new JPopupMenu(); select=new JMenuItem("Scegli..."); none=new JMenuItem("Cancella"); stretch= new JMenuItem("Adatta"); normal=new JMenuItem("Normale"); popup.add(select); popup.add(none); popup.add(stretch); popup.add(normal); choosedPanel.setComponentPopupMenu(popup); this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); this.add(choosePanel, BorderLayout.CENTER); this.add(panel, BorderLayout.SOUTH); this.setSize(400, 150); } public void actionPerformed(ActionEvent e) { String lafClassName = null; lafClassName = e.getActionCommand(); String finalLafClassName = lafClassName; try { UIManager.setLookAndFeel(finalLafClassName); SwingUtilities.updateComponentTreeUI(this); } catch (Exception ex) { ex.printStakeTrace(); } } }
La cosa strana è che mi succede solo in Eclipse, in ambienti di sviluppo meno rinomati il problema non sussiste.
Grazie,
Ciao.

Rispondi quotando


