java lookandfeel nimbus_动态改变LookAndFeel
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
總結
以上是生活随笔為你收集整理的java lookandfeel nimbus_动态改变LookAndFeel的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: win7 linux双系统win7启动不
 - 下一篇: $.ajax 加了headers报错_S