UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels(); final JComboBox lfComboBox = new JComboBox(info); JLabel lfLabel = new JLabel("Look and Feel:"); lfLabel.setDisplayedMnemonic('f'); lfLabel.setLabelFor(lfComboBox);
// System.out.println(UIManager.getSystemLookAndFeelClassName()); System.out.println(UIManager.getLookAndFeel().getClass()); for (int i = 0; i < info.length; i++) { if (info[i].getName() == UIManager.getLookAndFeel().getName()) { lfComboBox.setSelectedItem(info[i]); System.out.println("当前的外观是:"+info[i].getName()); break; } } lfComboBox.setRenderer(new DefaultListCellRenderer() { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (value != null && value instanceof UIManager.LookAndFeelInfo) { UIManager.LookAndFeelInfo info = (UIManager.LookAndFeelInfo) value; setText(info.getName()); } return c; } }); //侦听外观的改变,即时地绘制主程序的外观 //同时也更改偏好面板的外观,不过在Nimbus的更改的时候似乎有些问题 //需要用布局管理器管理一下 lfComboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Runnable changelf = new Runnable() { public void run() { Frame frame = JOptionPane.getFrameForComponent(lfComboBox); // JOptionPane.getRootFrame() try { UIManager.LookAndFeelInfo info = (UIManager.LookAndFeelInfo) lfComboBox.getSelectedItem(); if (info != null) { UIManager.setLookAndFeel(info.getClassName()); SwingUtilities.updateComponentTreeUI(frame); SwingUtilities.updateComponentTreeUI(PerferenceDialog.this); frame.pack(); PerferenceDialog.this.pack(); } } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showOptionDialog(frame, "The following error occurred while changing the look and feel:\n" + ex.getMessage(), "Look And Feel Error", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Dismiss" }, null); } } }; SwingUtilities.invokeLater(changelf); } });
lookAndFeelPanel=new JPanel(); lookAndFeelPanel.setBorder(new TitledBorder("LookAndFeel")); lookAndFeelPanel.setLayout(layout); lookAndFeelPanel.add(lfLabel); lookAndFeelPanel.add(lfComboBox); lookAndFeelPanel.add(defaultBtn); defaultBtn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent event) { // TODO Auto-generated method stub Runnable changeToDefault=new Runnable(){ @Override public void run() { // TODO Auto-generated method stub Frame frame = JOptionPane.getFrameForComponent(lfComboBox); // JOptionPane.getRootFrame() try { // String defaultUI = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; String defaultUI ="javax.swing.plaf.metal.MetalLookAndFeel"; UIManager.setLookAndFeel(defaultUI); SwingUtilities.updateComponentTreeUI(frame); SwingUtilities.updateComponentTreeUI(PerferenceDialog.this); frame.pack(); // JDialog dialog=(JDialog)defaultBtn.getParent(); // dialog.pack(); // dialog.repaint(); // dialog.validate(); PerferenceDialog.this.pack(); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showOptionDialog(frame, "The following error occurred while changing the look and feel:\n" + ex.getMessage(), "Look And Feel Error", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Dismiss" }, null); } } }; SwingUtilities.invokeLater(changeToDefault); }
2.写个java方法
3.通过java方法进行绘图调用色彩
4.测试
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.DefaultListCellRenderer;
import javax.swing.JDialog;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;/**
* 这个是用户偏好的面板
* @author GaoYong
*
*/
public class PerferenceDialog extends JDialog{
private JPanel lookAndFeelPanel;//外观的属性设置
private JPanel closeWinPanel;//关闭窗体的属性设置
private JButton defaultBtn;
private JLabel label1;
private JComboBox comboBox;
private FlowLayout layout;
public PerferenceDialog(){}
public PerferenceDialog(JFrame parent,String title){
super(parent,title,true);//默认是模态的对话框,在构造方法的最后一个参数声明
defaultBtn=new JButton("Default");
layout=new FlowLayout();
label1=new JLabel("OnWindowClosing");
comboBox=new JComboBox();
comboBox.addItem("MinimizeToTray");
comboBox.addItem("Exit");
label1.setLabelFor(comboBox);
Container contentPane=this.getContentPane();
// JComboBox lookAndFeelCombo=new JComboBox();
// lookAndFeelCombo.addItem("Nimbus");
// lookAndFeelCombo.addItem("Metal");
// lookAndFeelCombo.addItem("Windows");
// lookAndFeelCombo.addItem("Windows Classic");
// lookAndFeelCombo.addItem("Plastic");
// lookAndFeelCombo.addItem("CDE/Motif");
// JLabel lookAndFeelLabel=new JLabel("lookAndFeel");
// lookAndFeelLabel.setLabelFor(lookAndFeelCombo);
UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels(); final JComboBox lfComboBox = new JComboBox(info);
JLabel lfLabel = new JLabel("Look and Feel:");
lfLabel.setDisplayedMnemonic('f');
lfLabel.setLabelFor(lfComboBox);
// System.out.println(UIManager.getSystemLookAndFeelClassName());
System.out.println(UIManager.getLookAndFeel().getClass());
for (int i = 0; i < info.length; i++) {
if (info[i].getName() == UIManager.getLookAndFeel().getName()) {
lfComboBox.setSelectedItem(info[i]);
System.out.println("当前的外观是:"+info[i].getName());
break;
}
} lfComboBox.setRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (value != null && value instanceof UIManager.LookAndFeelInfo) {
UIManager.LookAndFeelInfo info = (UIManager.LookAndFeelInfo) value;
setText(info.getName());
} return c;
}
});
//侦听外观的改变,即时地绘制主程序的外观
//同时也更改偏好面板的外观,不过在Nimbus的更改的时候似乎有些问题
//需要用布局管理器管理一下
lfComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Runnable changelf = new Runnable() {
public void run() {
Frame frame = JOptionPane.getFrameForComponent(lfComboBox);
// JOptionPane.getRootFrame()
try {
UIManager.LookAndFeelInfo info = (UIManager.LookAndFeelInfo) lfComboBox.getSelectedItem();
if (info != null) {
UIManager.setLookAndFeel(info.getClassName());
SwingUtilities.updateComponentTreeUI(frame);
SwingUtilities.updateComponentTreeUI(PerferenceDialog.this);
frame.pack();
PerferenceDialog.this.pack();
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showOptionDialog(frame, "The following error occurred while changing the look and feel:\n" + ex.getMessage(), "Look And Feel Error", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Dismiss" }, null);
}
}
};
SwingUtilities.invokeLater(changelf);
}
});
lookAndFeelPanel=new JPanel();
lookAndFeelPanel.setBorder(new TitledBorder("LookAndFeel"));
lookAndFeelPanel.setLayout(layout);
lookAndFeelPanel.add(lfLabel);
lookAndFeelPanel.add(lfComboBox);
lookAndFeelPanel.add(defaultBtn);
defaultBtn.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
Runnable changeToDefault=new Runnable(){ @Override
public void run() {
// TODO Auto-generated method stub
Frame frame = JOptionPane.getFrameForComponent(lfComboBox);
// JOptionPane.getRootFrame()
try {
// String defaultUI = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
String defaultUI ="javax.swing.plaf.metal.MetalLookAndFeel";
UIManager.setLookAndFeel(defaultUI);
SwingUtilities.updateComponentTreeUI(frame);
SwingUtilities.updateComponentTreeUI(PerferenceDialog.this);
frame.pack();
// JDialog dialog=(JDialog)defaultBtn.getParent();
// dialog.pack();
// dialog.repaint();
// dialog.validate();
PerferenceDialog.this.pack();
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showOptionDialog(frame, "The following error occurred while changing the look and feel:\n" + ex.getMessage(), "Look And Feel Error", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Dismiss" }, null);
}
}
};
SwingUtilities.invokeLater(changeToDefault);
}
});
closeWinPanel=new JPanel();
closeWinPanel.setLayout(layout);
closeWinPanel.add(label1);
closeWinPanel.add(comboBox);
closeWinPanel.setBorder(new TitledBorder("CloseWindow"));
contentPane.setLayout(layout);
contentPane.add(closeWinPanel);
contentPane.add(lookAndFeelPanel);
this.setLocationRelativeTo(null);
this.pack();
this.setVisible(true);
}
public static void main(String[] args){
final JFrame main=new JFrame("demo");
JButton btn=new JButton("showDialog");
btn.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new PerferenceDialog(main,"dialog");
}
});
main.getContentPane().add(btn);
main.pack();
main.setVisible(true);
main.setLocationRelativeTo(null);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}