LookAndFeel可以改变图形界面的风格,比如说可以将Java的默认界面改变成仿Windows,UNIX等其它风格的界面,主要有以下几种界面风格:

metal(默认):

 "javax.swing.plaf.metal.MetalLookAndFeel"

windows:

 "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"

nimbus:

 "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"

windows classic:

 "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"

unix:

 "com.sun.java.swing.plaf.motif.MotifLookAndFeel"

mac(需要相关系统):

 "com.sun.java.swing.plaf.mac.MacLookAndFeel"

GTK(需要相关系统):

 "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"

也可以通过:UIManager.getSystemLookAndFeelClassName()获取当前系统风格UIManager.getCrossPlatformLookAndFeelClassName()获取可跨平台的默认风格

 

动态的改变界面风格,主要用到下面两个方法:

UIManager.setLookAndFeel(String str)设置当前外观为str所指定的

SwingUtilities.updateComponentTreeUI(Component c)重新初始化组件的外观

举个例子:

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

 

 

public class LookAndFeel extends JFrame implements ActionListener{

private String str;

private JButton b1,b2,b3;

private JPanel panel;

LookAndFeel(){

panel = new JPanel();

panel.setLayout(new FlowLayout());

b1 = new JButton("windows");

b1.addActionListener(this);

b2 = new JButton("nimbus");

b2.addActionListener(this);

b3 = new JButton("uniux");

b3.addActionListener(this);

panel.add(b1);

panel.add(b2);

panel.add(b3);

this.add(panel);

this.setSize(400, 300);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==b1){

str = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";

}

else if(e.getSource()==b2){

str = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";

}

else if(e.getSource()==b3){

str = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";

}

try {

UIManager.setLookAndFeel(str);

SwingUtilities.updateComponentTreeUI(this);

} catch (Exception e1) {}

}

public static void main(String str){

SwingUtilities.invokeLater(new Runnable() {

public void run() {

LookAndFeel inst = new LookAndFeel();

inst.setLocationRelativeTo(null);

inst.setVisible(true);

}

});

}

}

效果图如下:
nimbus

unix

windows