1.3 swing之美化系统UIManage

666 阅读2分钟

背景:项目由于原来的定位变更了,原先只是“可以用”的定位,现在变成“商用级别”的水准。产品一直bb界面太丑太难看咯;我们都知道原生的swing确实是挺难看的,最后进过一段时间的研究,总结了几种美化的方式。

第一种:UIManager.setLookAndFeel()

swing为我们提供了各种各样的,UIManager能够简单的实现替换实现方法就可以相对美化界面。 替换代码如下:

try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Windows".equals(info.getName())) {
            javax.swing.UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (Exception e) {
    System.out.println(e);

}

还可以使用其他的如下风格:

// Metal风格 (默认)
        String lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";
        UIManager.setLookAndFeel(lookAndFeel);
// Windows风格
        String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
        UIManager.setLookAndFeel(lookAndFeel);

// Windows Classic风格
        String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
        UIManager.setLookAndFeel(lookAndFeel);

// Motif风格
        String lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
        UIManager.setLookAndFeel(lookAndFeel);

// Mac风格 (需要在相关的操作系统上方可实现)
        String lookAndFeel = "com.sun.java.swing.plaf.mac.MacLookAndFeel";
        UIManager.setLookAndFeel(lookAndFeel);

 //GTK风格 (需要在相关的操作系统上方可实现)
        String lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
        UIManager.setLookAndFeel(lookAndFeel);

 //可跨平台的默认风格
        String lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
        UIManager.setLookAndFeel(lookAndFeel);

// 当前系统的风格
        String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
        UIManager.setLookAndFeel(lookAndFeel);
第二种:引入第三方jar包beautyeye(gitee.com/jackjiang/b…)

这种就不赘述,有需要可以查看上面的地址 代码如下:

public  static void beautyUI(){
    try
    {
        //如何解决切换输入法导致白屏的问题
        System.setProperty("sun.java2d.noddraw", "true");
        //设置属性即可:true表示使用ToolBar.background颜色实现纯
        //色填充背景,BeautyEye中此属性默认是false
        UIManager.put("ToolBar.isPaintPlainBackground", Boolean.TRUE);
        //自定义JToolBar ui的border
        Border bd = new org.jb2011.lnf.beautyeye.ch8_toolbar.BEToolBarUI.ToolBarBorder(
                UIManager.getColor("ToolBar.shadow")     //Floatable时触点的颜色
                , UIManager.getColor("ToolBar.highlight")//Floatable时触点的阴影颜色
                , new Insets(0, 0, 0, 0));              //border的默认insets
        UIManager.put("ToolBar.border",new BorderUIResource(bd));
        //JTabbedPane左缩进
        //改变InsetsUIResource参数的值即可实现
        UIManager.put("TabbedPane.tabAreaInsets"
                , new javax.swing.plaf.InsetsUIResource(0,0,0,0));
        //设置本属性将改变窗口边框样式定义
        BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
        UIManager.put("RootPane.setupButtonVisible", false);
        BeautyEyeLNFHelper.launchBeautyEyeLNF();
    }
    catch(Exception e)
    {
        //TODO exception
    }
}
第三种,定制化,UI大佬设计界面一个个画

这种需要有个UI大佬切图

注意:上面前两种方法都会覆盖默认的样式,可能导致配置的控件属性不生效