天天看点

JFrame setDefaultLookAndFeelDecorated(true)

import javax.swing.*;     public class F extends JFrame     {     JFrame f=new JFrame();      
f.setDefaultLookAndFeelDecorated(true);      
F() {      f.setSize(300,200);     f.setDefaultCloseOperation(3);     f.setVisible(true);     }     public static void main(String[] args)     {     new F();     }     }      

如何不继承jframe;

import javax.swing.*;     public class F{         JFrame f = new JFrame();         F() {             f.setSize(300, 200);             f.setDefaultCloseOperation(3);             f.setVisible(true);         }         public static void main(String[] args) {             JFrame.setDefaultLookAndFeelDecorated(true); //调用             new F();         }     }      

必须要先用JFrame.setDefaultLookAndFeelDecorated(true);设置了后,再产生JFrame的对这样才能生效

如果先产生了对象在调用JFrame.setDefaultLookAndFeelDecorated(true);方法 会不能生效

 设置Look and Feel

Swing允许在程序中指定Look and Feel,即:Java look and feel,Windows look and feel和CDE/Motif look and feel等。可以使用UIManaer.setLookAndFeel方法指定Look and Feel,在下面的代码段中,使用getCrossPlatformLookAndFeelClassName方法得到跨平台的Look and Feel(即Java look and feel).

public static void main(String[]args)             {                 try{                 UIManager.setLookAndFeel(                 UIManager.getCrossPlatformLookAndFeelClassName());                 }catch(Exception e) { }                 …//Create and show the GUI…     	}      

如果希望使用程序当前运行的平台所用的Look and Feel,可以使用UIManager.getSystem LookAndFeelClassName方法。当然,还可以直接使用实际的类名来指定,例如:

UIManager.setLookAndFeel(

“com.sun.java.swing.plaf.windows.windowsLookAndFeel”);

总的来说,对于UIManager.setLookAndFeel方法,可以使用下面的参数来设置look and feel:

★UIManager.getCrossPlatformLookAndFeelClassName()

返回一个在任何平台都肯定能正常工作的Look and Feel,即Java look and feel.

★UIManager.getSystemLookAndFeelClassName()

指定当前平台所使用的Look and Feel。在Win32平台,则指定为Windows lood and feel;在Mac OS平台,为Mac OS look and feel;在Sun平台,为CDE/Motiflook and feel.

★“javax.swing.plaf.metal.MetalLookAndFeel”

指定为Java look and feel,它与UIManager.getCrossPlatformLookAndFeelClassName()方法相同。

★“com.sun.java.swing.plaf.windows.Windows.WindosLookAndFeel”

指定为CDE/Motif look and feel,可以在任何平台上使用。

★“java.swing.plat.mac.MacLookAndFeel”

指定为Mac OS look and feel,它公适用Mac OS平台。

当然,还可以使用其他参数,执行在UIManager.aetLookAndFeel方法中指定相应的类名。

在设置了Look and feel之后,还可以使用下面的代码来改变Look and Feel:

UIManager.setLookAndFeel(InfName);

SwingUtilities.rpdateComponentTreeUI(frame);

Frame.pack();

如果每次都使用固定的Look and Feel,还可以通过在程序之外设置环境变量来实现。在J2SE的HOME目录(例如c:\jdk1.3)下,找到lib子目录,创建一个文件swing.properties(如果不存在),在文件中加入类似于下面的内容:

#Swing properties

swing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel

使用 javax.swing.UIManager 可以更改界面风格, 应该在创建画面之前执行,否则设置有可能不起作用. 在 GUI 显示后,也可以改变界面风格,但可见组件不会自动更新显示外观,需要对每个顶层容器调用 SwingUtilities.updateComponentTreeUI() 方法.

  主要的 Look & Feel  有:  Java/Metal, Windows, CDE/Motif, 但如果有其它的 L&F 包,还可以扩展.只要指定类名加载即可.

  设置语句:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());             // 当前系统默认       UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());    // 跨平台       UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");  // Windows      

免费的laf :Napkin  http://napkinlaf.sourceforge.net/

上一篇: Redis-初见