import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
@SuppressWarnings("serial")
public class LookAndFeelDemo extends JFrame implements ActionListener
{
private static class LookAndFeelApplication
{
private LookAndFeelInfo lookAndFeelInfo;
private LookAndFeelApplication(LookAndFeelInfo lookAndFeelInfo)
{
this.lookAndFeelInfo = lookAndFeelInfo;
}
public static LookAndFeelApplication[] getInstalledLookAndFeelApplications()
{
LookAndFeelInfo[] lookAndFeelInfos =
UIManager.getInstalledLookAndFeels();
LookAndFeelApplication[] lookAndFeelApplications =
new LookAndFeelApplication[lookAndFeelInfos.length];
for(int i = 0; i < lookAndFeelInfos.length; i++)
lookAndFeelApplications[i] = new LookAndFeelApplication(lookAndFeelInfos[i]);
return lookAndFeelApplications;
}
public void applyTo(Component component)
{
try {
UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());
SwingUtilities.updateComponentTreeUI(component);
} catch (ClassNotFoundException
| InstantiationException
| IllegalAccessException
| UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
}
@Override
public String toString() {
return lookAndFeelInfo.getClassName();
}
}
private LookAndFeelDemo()
{
super("LookAndFeelDemo");
setLayout(new BorderLayout());
JComboBox<LookAndFeelApplication> comboBox =
new JComboBox<LookAndFeelApplication>(
LookAndFeelApplication.getInstalledLookAndFeelApplications());
comboBox.addActionListener(this);
add(comboBox, BorderLayout.NORTH);
comboBox.setSelectedIndex(0);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel.add(new JButton("Button"));
panel.add(new JLabel("Label"));
panel.add(new JCheckBox("Check Box"));
panel.add(new JRadioButton("Radio Button"));
JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 2);
progressBar.setValue(1);
panel.add(progressBar);
panel.add(new JSlider(JSlider.HORIZONTAL, 0, 2, 1));
panel.add(new JSpinner());
panel.add(new JTextField("Text Field"));
add(panel, BorderLayout.CENTER);
setLocationRelativeTo(null);
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new LookAndFeelDemo();
}
@Override
public void actionPerformed(ActionEvent e) {
@SuppressWarnings("unchecked")
JComboBox<String> comboBox = (JComboBox<String>)e.getSource();
LookAndFeelApplication lookAndFeelApplication = (LookAndFeelApplication) comboBox.getSelectedItem();
lookAndFeelApplication.applyTo(this);
}
}
我的系统是Windows XP,估计在其他系统中会有其他内置皮肤提供。