http://jianweili007-163-com.iteye.com/blog/1141358

————————————————————————————————————————————————————————————

public class LAF_Local_1 extends javax.swing.JFrame {
    public LAF_Local_1() {
        super("透明窗体");
        this.setLayout(new java.awt.FlowLayout());
        this.add(new javax.swing.JButton("按钮"));
        this.add(new javax.swing.JCheckBox("复选按钮"));
        this.add(new javax.swing.JRadioButton("单选按钮"));
        this.add(new javax.swing.JProgressBar(20, 100));
        this.setSize(new java.awt.Dimension(400, 300));
        this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        javax.swing.JFrame.setDefaultLookAndFeelDecorated(true);
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                java.awt.Window w = new LAF_Local_1();
                w.setVisible(true);
                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);
            }
        });
    }
}

先说可能遇到的问题:“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方法改一下:

public static void main(String[] args) {
         try {
            javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (javax.swing.UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }          

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                java.awt.Window w = new LAF_Local_1();
                w.setVisible(true);
                com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.6f);
            }
        });
    }

此款皮肤支持上面提到的改变窗体透明度的方法,不支持上面的改变窗体形状的方法。至于能否满足要求,因人而异喽。接下来将介绍一款第三方的皮肤库: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函数做一下改变

public static void main(String[] args) {

        JFrame.setDefaultLookAndFeelDecorated(true); //加上此语句连同窗体外框也改变
        JDialog.setDefaultLookAndFeelDecorated(true); //加上此语句会使弹出的对话框也改变
         try {
                //新建一个图片水印,路径可以自己该,使用自己喜欢的图片来做应用程序的水印图片,貌似图片要在src子目录下
                SubstanceImageWatermark watermark = new SubstanceImageWatermark(LAF_Local_4.class.getResourceAsStream("001.bmp"));
                
                watermark.setKind(ImageWatermarkKind.APP_CENTER);
                watermark.setOpacity((float) 0.7); //更改水印透明度
                UIManager.setLookAndFeel(new SubstanceOfficeBlue2007LookAndFeel());
//                UIManager.setLookAndFeel(new SubstanceCremeLookAndFeel());
                
                //可根据根据外观类型,设置相应的皮肤,实践证实不一定需要一一对应
//                SubstanceSkin skin = new OfficeBlue2007Skin().withWatermark(watermark);
                SubstanceSkin skin = new CremeSkin().withWatermark(watermark);
                
                //此语句设置外观
                SubstanceLookAndFeel.setSkin(skin);

            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(LAF_Local_4.class.getName()).log(Level.SEVERE, null, ex);
            }
            java.awt.EventQueue.invokeLater(new Runnable() {

                public void run() {
                    LAF_Local_4 text = new LAF_Local_4();//实例化窗体对象
                    text.setVisible(true);
                }
            });
    }

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

public static void main(String[] args) {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                SubstanceImageWatermark watermark = new SubstanceImageWatermark(LAF_Local_4.class.getResourceAsStream("001.bmp"));
                
                watermark.setKind(ImageWatermarkKind.APP_CENTER);
                watermark.setOpacity((float) 0.7);
                SubstanceLookAndFeel.setSkin(new OfficeBlue2007Skin().withWatermark(watermark));
                try {
                    new LAF_Local_3().setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

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

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

posted on 2016-01-16 00:21  网络大豆  阅读(1016)  评论(1编辑  收藏  举报