Java 用Look And Feel打造绚丽的界面外观!

先看效果图:Java 用Look And Feel打造绚丽的界面外观!_第1张图片Java 用Look And Feel打造绚丽的界面外观!_第2张图片

虽然我们知道,一个桌面应用程序的好坏,和它的性能,功能有着很大关系, 然而,对于大多数坐在电脑前的用户而言,他们的标准往往是: 绚丽的外观MVC设计下的的SWING自然没有忽视这一点,通过对UIManager的设置,我们可以很容易的改变应用程序的外观,也就是LOOK AND FEEL

一.如何设置lookand feel:

    改变外观,似乎是一个很麻烦的事情,幸运的是在swing里,我们只需要简单的一行代码就可以改变

UIManager.setLookAndFeel(newLookAndFeel());

比如:

   UIManager.setLookAndFeel(newQuaquaLookAndFeel());

我们还可以通过

   UIManager.setLookAndFeel(String s);

来改变外观,其中,s是表示该外观的路径,比如

   UIManager.setLookAndFeel(

   "org.fife.plaf.OfficeXP.OfficeXPLookAndFeel");

另外,如果该LookAnd Feel类对窗口的边框还有修饰的话,还可以通过

JFrame.setDefaultLookAndFeelDecorated(true);

JDialog.setDefaultLookAndFeelDecorated(true);

这样一来,窗口就会采用该外观特制的边框

二.一些比较帅气的look and feel

swing自带提供了几种look and feel类,不过,这显然是不够的,下面,就让我们看看第三方都提供了些什么酷酷的外观咯:

1.Substance

    这个项目的目的是提供一个流行的外观(look & feel)。这个外观(look & feel)联合了WindowsXP和MacOS 10.4最好的特性并且需要JDK 5.0以上。

    将窗口的边框替换成Substance特定边框后,我们还可以通过点击其左上角的小方块来手工配制其外观,可以配制的有

主题,水印,按钮形状,渐变情况,看,这个蝴蝶形状的button多酷!

2.Smooth

    提供了改进型的windows和metal风格的外观风格

   提供了仿照Xp,Office2003和VS的外观风格

4.其他

   在网站上罗列如今较为流行的外观类,大家有兴趣可以去研究一下咯^_^

http://www.open-open.com/61.htm

推荐http://javootoo.l2fprod.com/plaf/easynth/index.html

 

三.使用方法

将下列jar文件拷贝到你的程序的classpath中,然后将下列代码段加入到你main函数中

(注,其实我在别的文章中给出了一个例子,http://210.42.106.102/bbs/viewth... &extra=page%3D1,

参见用java打造任意形状窗口一文中的的代码)

1.substancelook and feel:

   try {

                       UIManager.setLookAndFeel(new SubstanceLookAndFeel());

                        UIManager.put("swing.boldMetal",false);

                        if(System.getProperty("substancelaf.useDecorations") == null) {

                               JFrame.setDefaultLookAndFeelDecorated(true);

                                JDialog.setDefaultLookAndFeelDecorated(true);

                        }

                       System.setProperty("sun.awt.noerasebackground","true");

                        //设置当前的主题风格,同样我 们还可以设置当前的按钮形状,水印风格等等

                       SubstanceLookAndFeel.setCurrentTheme(new SubstanceLightAquaTheme());

 

                } catch (Exception e) {

                       System.err.println("Oops! Something went wrong!");

                }

 

2.smoothlook and feel

             try {

                       UIManager.setLookAndFeel(new SmoothLookAndFeel());

                       UIManager.put("swing.boldMetal", false);

                } catch (Exception e) {

                       System.err.println("Oops! Something went wrong!");

                }

3.   office/winxp/VisualStudio 2005 look and feel

      try {

                   UIManager.setLookAndFeel("org.fife.plaf.Office2003.Office2003LookAndFeel");

                    //UIManager.setLookAndFeel("org.fife.plaf.OfficeXP.OfficeXPLookAndFeel");

                   //UIManager.setLookAndFeel("org.fife.plaf.VisualStudio2005.VisualStudio2005LookAndFeel");

                   UIManager.put("swing.boldMetal", false);

                } catch (Exception e) {

                       System.err.println("Oops! Something went wrong!");

              }

4、netbeans中使用方法:

 

  try {

            for(javax.swing.UIManager.LookAndFeelInfo info :javax.swing.UIManager.getInstalledLookAndFeels()) {

                if("Windows".equals(info.getName())) {

                   javax.swing.UIManager.setLookAndFeel(info.getClassName());

                   break;

                }

                /**

                 * Metal Nimbus CDE/MotifWindows Windows Classic

                 */

               System.out.println(info.getName());

               

            }

           //javax.swing.UIManager.setLookAndFeel("com.easynth.lookandfeel.EaSynthLookAndFeel");

           //javax.swing.UIManager.setLookAndFeel("org.jvnet.substance.SubstanceLookAndFeel");

           //com.sun.java.swing.plaf.gtk.GTKLookAndFeel com.sun.java.swing.plaf.motif.MotifLookAndFeelcom.sun.java.swing.plaf.windows.WindowsLookAndFeel

           //UIManager.setLookAndFeel("smoothmetal.SmoothLookAndFeel");      

 

            /**UIManager.setLookAndFeel(newSubstanceLookAndFeel());

            UIManager.put("swing.boldMetal",false);

            if(System.getProperty("substancelaf.useDecorations") == null) {

               JFrame.setDefaultLookAndFeelDecorated(true);

               JDialog.setDefaultLookAndFeelDecorated(true);

            }

            System.setProperty("sun.awt.noerasebackground","true");

           SubstanceLookAndFeel.setCurrentTheme(neworg.jvnet.substance.theme.SubstanceBarbyPinkTheme());//设置当前的主题风格,同样我 们还可以设置当前的按钮形状,水印风格等等

            */           

        } catch (Exception ex) {

           java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE,null, ex);

        }


5、EaSynthLookAndFeel

javax.swing.UIManager.setLookAndFeel("com.easynth.lookandfeel.EaSynthLookAndFeel");

你可能感兴趣的:(Java 用Look And Feel打造绚丽的界面外观!)