Java图形界面设计——substance皮肤

先看一段代码吧。 

Java代码  收藏代码

  1. public class LAF_Local_1 extends javax.swing.JFrame {  

  2.     public LAF_Local_1() {  

  3.         super("透明窗体");  

  4.         this.setLayout(new java.awt.FlowLayout());  

  5.         this.add(new javax.swing.JButton("按钮"));  

  6.         this.add(new javax.swing.JCheckBox("复选按钮"));  

  7.         this.add(new javax.swing.JRadioButton("单选按钮"));  

  8.         this.add(new javax.swing.JProgressBar(20100));  

  9.         this.setSize(new java.awt.Dimension(400300));  

  10.         this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);  

  11.     }  

  12.   

  13.     public static void main(String[] args) {  

  14.         javax.swing.JFrame.setDefaultLookAndFeelDecorated(true);  

  15.         javax.swing.SwingUtilities.invokeLater(new Runnable() {  

  16.             public void run() {  

  17.                 java.awt.Window w = new LAF_Local_1();  

  18.                 w.setVisible(true);  

  19.                 com.sun.awt.AWTUtilities.setWindowShape(w,   

  20.                         new java.awt.geom.Ellipse2D.Double(00,  

  21.  w.getWidth(), w.getHeight()));  

  22.                 com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.6f);  

  23.             }  

  24.         });  

  25.     }  

  26. }  

先说可能遇到的问题:“com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.6f);”此行代码会报错“Access restriction: The type AWTUtilities is not accessible due to restriction on required library D:\Program Files\jdk1.6.0_24\jre\lib\rt.jar”,而解决的方法也很怪异,要在要在project build path中先移除JRE System Library,再添加库JRE System Library,之后就可以了。类似的问题也可以如此解决。还试过强行运行程序,也没问题。而project build path在哪,下面在介绍添加第三方jar包时,你就能看到它。 

在上面的代码中,没用到第三方的jar包,只是掉用几个比较偏的方法也可玩点花样。 
javax.swing.JFrame.setDefaultLookAndFeelDecorated(true);这一语句具体意思我还说不清楚,可查看AIP文档。加上此语句,界面看上去很不顺眼,难看。 
com.sun.awt.AWTUtilities.setWindowShape(w, new java.awt.geom.Ellipse2D.Double(0, 0, w.getWidth(), w.getHeight()));没有解释,但从方法名及效果上看可知此语句能改变窗体的形状,前提是必须加上上面那句语句。为啥,不知道。 
com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.6f);添加此语句可改变窗体的透明度。第二个参数的赋值范围是0.0f~1.0f,数值越小,窗体透明度越高。 


Swing中还自带了一款皮肤,看上去还蛮顺眼的。将上一段代码中的main方法改一下: 
  

Java代码  收藏代码

  1. public static void main(String[] args) {  

  2.          try {  

  3.             javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");  

  4.         } catch (ClassNotFoundException e) {  

  5.             e.printStackTrace();  

  6.         } catch (InstantiationException e) {  

  7.             e.printStackTrace();  

  8.         } catch (IllegalAccessException e) {  

  9.             e.printStackTrace();  

  10.         } catch (javax.swing.UnsupportedLookAndFeelException e) {  

  11.             e.printStackTrace();  

  12.         }            

  13.   

  14.         javax.swing.SwingUtilities.invokeLater(new Runnable() {  

  15.             public void run() {  

  16.                 java.awt.Window w = new LAF_Local_1();  

  17.                 w.setVisible(true);  

  18.                 com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.6f);  

  19.             }  

  20.         });  

  21.     }  

此款皮肤支持上面提到的改变窗体透明度的方法,不支持上面的改变窗体形状的方法。至于能否满足要求,因人而异喽。接下来将介绍一款第三方的皮肤库:substance.jar。 

不同版本的substance皮肤库其皮肤的样式及所在目录可能会不同。我用的是5.2版本的。其所有皮肤都再org.jvnet.substance.skin目录下,皮肤样式类名如下: 

  • AutumnSkin

  • BusinessSkin

  • BusinessBlackSteelSkin

  • BusinessBlueSteelSkin

  • ChallengerDeepSkin

  • CremeCoffeeSkin

  • CremeSkin

  • DustCoffeeSkin

  • DustSkin

  • EmeraldDuskSkin

  • MagmaSkin

  • MistAquaSkin

  • MistSilverSkin

  • ModerateSkin

  • NebulaBrickWallSkin

  • NebulaSkin

  • OfficeBlue2007Skin

  • OfficeSilver2007Skin

  • RavenGraphiteGlassSkin

  • RavenGraphiteSkin

  • RavenSkin

  • SaharaSkin

  • TwilightSkin


  • /---------------------------------------------/

  • SubstanceAutumnLookAndFeel

  • SubstanceBusinessBlackSteelLookAndFeel

  • SubstanceBusinessBlueSteelLookAndFeel

  • SubstanceBusinessLookAndFeel

  • SubstanceChallengerDeepLookAndFeel

  • SubstanceCremeCoffeeLookAndFeel

  • SubstanceCremeLookAndFeel

  • SubstanceDustCoffeeLookAndFeel

  • SubstanceDustLookAndFeel

  • SubstanceEmeraldDuskLookAndFeel

  • SubstanceMagmaLookAndFeel

  • SubstanceMistAquaLookAndFeel

  • SubstanceMistSilverLookAndFeel

  • SubstanceModerateLookAndFeel

  • SubstanceNebulaBrickWallLookAndFeel

  • SubstanceNebulaLookAndFeel

  • SubstanceOfficeBlue2007LookAndFeel

  • SubstanceOfficeSilver2007LookAndFeel

  • SubstanceRavenGraphiteGlassLookAndFeel

  • SubstanceRavenGraphiteLookAndFeel

  • SubstanceRavenLookAndFeel

  • SubstanceSaharaLookAndFeel

  • SubstanceTwilightLookAndFee


  • /----------------------------------------------/

  • SubstanceCrosshatchWatermark

  • SubstanceImageWatermark

  • SubstanceNoiseWatermark

  • SubstanceNullWatermark

  • SubstancePlanktonWatermark

  • SubstanceStripeWatermark

  • SubstanceWatermarkl

以上水印还有待测试 
使用substance的方法有多种;以下介绍两种: 

/---------------------------1---------------------------/: 
将上面例子中的main函数做一下改变 

Java代码  收藏代码

  1. public static void main(String[] args) {  

  2.   

  3.         JFrame.setDefaultLookAndFeelDecorated(true); //加上此语句连同窗体外框也改变  

  4.         JDialog.setDefaultLookAndFeelDecorated(true); //加上此语句会使弹出的对话框也改变  

  5.          try {  

  6.                 //新建一个图片水印,路径可以自己该,使用自己喜欢的图片来做应用程序的水印图片,貌似图片要在src子目录下  

  7.                 SubstanceImageWatermark watermark = new SubstanceImageWatermark(LAF_Local_4.class.getResourceAsStream("001.bmp"));  

  8.                   

  9.                 watermark.setKind(ImageWatermarkKind.APP_CENTER);  

  10.                 watermark.setOpacity((float0.7); //更改水印透明度  

  11.                 UIManager.setLookAndFeel(new SubstanceOfficeBlue2007LookAndFeel());  

  12. //              UIManager.setLookAndFeel(new SubstanceCremeLookAndFeel());  

  13.                   

  14.                 //可根据根据外观类型,设置相应的皮肤,实践证实不一定需要一一对应  

  15. //              SubstanceSkin skin = new OfficeBlue2007Skin().withWatermark(watermark);  

  16.                 SubstanceSkin skin = new CremeSkin().withWatermark(watermark);  

  17.                   

  18.                 //此语句设置外观  

  19.                 SubstanceLookAndFeel.setSkin(skin);  

  20.   

  21.             } catch (UnsupportedLookAndFeelException ex) {  

  22.                 Logger.getLogger(LAF_Local_4.class.getName()).log(Level.SEVERE, null, ex);  

  23.             }  

  24.             java.awt.EventQueue.invokeLater(new Runnable() {  

  25.   

  26.                 public void run() {  

  27.                     LAF_Local_4 text = new LAF_Local_4();//实例化窗体对象  

  28.                     text.setVisible(true);  

  29.                 }  

  30.             });  

  31.     }  


/------------------2--------------------/: 

Java代码  收藏代码

  1. public static void main(String[] args) {  

  2.   

  3.         JFrame.setDefaultLookAndFeelDecorated(true);  

  4.         JDialog.setDefaultLookAndFeelDecorated(true);  

  5.   

  6.         SwingUtilities.invokeLater(new Runnable() {  

  7.             public void run() {  

  8.                 SubstanceImageWatermark watermark = new SubstanceImageWatermark(LAF_Local_4.class.getResourceAsStream("001.bmp"));  

  9.                   

  10.                 watermark.setKind(ImageWatermarkKind.APP_CENTER);  

  11.                 watermark.setOpacity((float0.7);  

  12.                 SubstanceLookAndFeel.setSkin(new OfficeBlue2007Skin().withWatermark(watermark));  

  13.                 try {  

  14.                     new LAF_Local_3().setVisible(true);  

  15.   

  16.                 } catch (Exception e) {  

  17.                     e.printStackTrace();  

  18.                 }  

  19.             }  

  20.         });  

  21.     }  

在用上面的代码之前,应将substance.jar包导入工程中,注意是工程中。可以在src的同根目录下新建一个lib文件夹,将substance.jar包放入其中。右击工程,在弹出菜单的最下面一行有个Properties选项,左击该选项,在弹出的对话框的左面板有个Java Build Path的选项,左击该选项,此时对话框右面板变更,在对话框的最右边的面板有个Add Jar选项,将substance导入,接下来确定即可。 

本人对Substance的了解有限,只能做些表面功夫。而一个好的界面仅有皮肤是不够的,内容及其布局的安排才是真功夫,所以本人觉得界面的美化,还需要加深对布局管理器、面板的了解,这有这样才能做出好看而实用的界面。另外使用第三方的jar包美化界面会出现bug,如:本人在做连连看时用到了substance.jar包,当改变Jtable中的数据时,其框架大小就会初始化,需要将相关值重新设置,这或许就是使用它的代价吧。更多信息可上网查找资料,由于在官网找不到资源,本人使用的substance.jar包也是别人共享的,所以在这也将资源一并上传。 


你可能感兴趣的:(Java图形界面设计——substance皮肤)