LAF让Java Swing漂亮起来

       上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助。今天在这里和大家一起学习一下Java Swing 中的LAF。其实很多朋友觉得Swing很丑,那是因为他们还没有真正认识到Swing,要想让Swing漂亮起来就必须熟练掌握LAF (全称:LookAndFeel)。可能很多新接触的朋友对LAF很抽象,这里简单说,LAF的功能有点像网页中的CSS,LAF可以定制Swing app的风格。

     这里还是老规矩,先上官网:http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/index.html 

     其实JDK已经自带了几种LAF, 看看下面的例子,如何正确使用JDK自带的LAF。

package org.dui.laf;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Enumeration;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

/**
 * <code>LAFDemo01</code>
 * 
 * @author Jimmy(SZ)
 * 
 * @since <i>v1.0.0 (Apr 11,2013)</i>
 */
public class LAFDemo01 extends JPanel {
	private static final long serialVersionUID = 1L;
	private JComboBox m_oCmb;

	/**
	 * Create an instance
	 */
	public LAFDemo01() {
		initGUI();
		initEventHandler();
		initDataBindings();
	}

	/**
	 * Initial the ui
	 * 
	 * @since <i>v1.0.0 (Apr 11,2013)</i>
	 */
	public void initGUI() {
		this.setLayout(new GridLayout(7, 2));
		m_oCmb = new JComboBox();
		this.add(new JLabel("在这里选择你喜欢的LAF:"));
		this.add(m_oCmb);
		this.add(new JLabel("Button : "));
		this.add(new JButton("Button"));
		this.add(new JLabel("TextField : "));
		this.add(new JTextField(""));
		this.add(new JLabel("CheckBox : "));
		this.add(new JCheckBox("CheckBox"));
		this.add(new JLabel("RadioButton : "));
		this.add(new JRadioButton("RadioButton"));
		this.add(new JLabel("ComboBox : "));
		this.add(new JComboBox(new String[] { "ComboBox" }));
	}

	/**
	 * Binding the event
	 * 
	 * @since <i>v1.0.0 (Apr 11,2013)</i>
	 */
	public void initEventHandler() {
		m_oCmb.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				try {
					UIManager.setLookAndFeel(((DataType) e.getItem()).getVal());
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				SwingUtilities.updateComponentTreeUI(LAFDemo01.this);
			}
		});
	}

	public void initDataBindings() {
		LookAndFeelInfo[] aInfo = UIManager.getInstalledLookAndFeels();
		for (LookAndFeelInfo oInfo : aInfo) {
			DataType oType = new DataType(oInfo.getName(), oInfo.getClassName());
			m_oCmb.addItem(oType);
		}
	}

	class DataType {
		String m_sCode;
		String m_sVal;

		public DataType(String sCode, String sVal) {
			m_sCode = sCode;
			m_sVal = sVal;
		}

		public String getVal() {
			return m_sVal;
		}

		public String toString() {
			return m_sCode;
		}
	}
	/**
	 * Launch the app
	 * 
	 * @param args
	 * @since <i>v1.0.0 (Apr 11 ,2013)</i>
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				JFrame oFrame = new JFrame();
				oFrame.setContentPane(new LAFDemo01());
				oFrame.setTitle("LAFDemo01");
				oFrame.setSize(400, 400);
				oFrame.setLocationRelativeTo(null);
				oFrame.setVisible(true);
			}
		});
	}
}

 

 好,看到这里,大家就觉得LAF真的那么简单吗,仅仅UIManager.setLookAndFeel(XXX)就搞定。不错,LAF就是这么简单。不过提醒一下,如果运行时改变LAF,要加上SwingUtilities.updateComponentTreeUI(LAFDemo01.this);,这个功能相当于“刷新界面”。来到这里也许大家会有一个问题,如何改变某种LAF里面的Widget的风格呢,比如前景色、背景色等等。这里拿JButton做例子,还没有接触Swing LAF以前,我们会用Button.setForeground(Color),Button.setBackground(Color),但是大家想想,如果一个大型的APP,里面有N多个Button,那岂不是要花很多时间。有了LAF以后我们就方便多了,全部美化的工作都是交给LAF去做。下面修改一下上面的例子:

在LAFDemo01中加入

 

static{
		UIManager.put("Button.foreground", new Color(255, 0 , 0));
		UIManager.put("Button.font", new Font("宋体", Font.BOLD , 32) );
		UIManager.put("Button.background", new Color(0, 255 , 255));
	}

Button的字体、前景色和背景色都发生了变化:前后两张图的对比



 
 是不是觉得很简单呢?在LAF中我们可以通过key-value的方式来定制自己喜欢的风格。这里,大家会问,那怎么知道LAF的所有的Key呢, 大家可以Google一下,这里提供Nimbus的Keys :http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html  。这里提供一个查找当前LAF 的所有的Keys 类(PS:网路上的):

import java.util.Enumeration;

import javax.swing.UIDefaults;
import javax.swing.UIManager;

public class ListProperties {
	@SuppressWarnings({ "rawtypes", "unused" })
	public static void main(String args[]) throws Exception {
		UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();
		UIDefaults defaults = UIManager.getDefaults();
		Enumeration newKeys = defaults.keys();
		while (newKeys.hasMoreElements()) {
			Object obj = newKeys.nextElement();
			if (obj.toString().startsWith("FormattedTextField"))
				System.out.printf("%50s : %s\n", obj, UIManager.get(obj));
		}
	}
}

   如果有的朋友想直接用第三方的LAF,这里介绍给大家 subtance、WebLookAndFeel,请看附件中!这两个LAF很好看,也好用。 今天LAF就介绍到这里,不懂的朋友,加油.眨眼

    最后上一张WebLookAndFeel的图片: