1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| package lf;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager.LookAndFeelInfo;
public class LFAction extends AbstractAction {
private LookAndFeelInfo info;
private JFrame mainFrame;
public LFAction(LookAndFeelInfo info, JFrame mainFrame) {
super(info.getName());
this.info = info;
this.mainFrame = mainFrame;
}
@Override
public void actionPerformed(ActionEvent e) {
try {
UIManager.setLookAndFeel(info.getClassName());
SwingUtilities.updateComponentTreeUI(mainFrame);
mainFrame.pack();
} catch (ClassNotFoundException e1) {
// TODO Bloc catch auto-généré
e1.printStackTrace();
} catch (InstantiationException e1) {
// TODO Bloc catch auto-généré
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Bloc catch auto-généré
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
// TODO Bloc catch auto-généré
e1.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
JFrame f = new JFrame();
JMenu menuThemes= new JMenu("Themes");
UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
for(LookAndFeelInfo info : infos ) {
menuThemes.add(new LFAction(info,f));
}
JMenuBar b = new JMenuBar();
b.add(menuThemes);
f.setJMenuBar(b);
f.add(new JButton("O Hai"));
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
}
} |