当前位置:首页 > Java技术

Swing动态切换Look And Feel

canca3年前 (2023-07-25)Java技术500

这是最完整、最完美的解决方法

public static void setLookAndFeel(LookAndFeel lookAndFeel, java.util.List<Component>  components){
    if(components == null || components.isEmpty()) return;
    try {
        for(int i = 0 ; i < components.size() ; i++) {
            Component c = components.get(i);
            if(i == 0) {
                //改变全局设置
                JFrame.setDefaultLookAndFeelDecorated(lookAndFeel.getSupportsWindowDecorations());
                JDialog.setDefaultLookAndFeelDecorated(lookAndFeel.getSupportsWindowDecorations());
                UIManager.setLookAndFeel(lookAndFeel);
            }
            if (c instanceof JFrame) {
                JFrame frame = (JFrame) c;
                boolean isVisible = frame.isVisible();
                frame.dispose();
                frame.setUndecorated(lookAndFeel.getSupportsWindowDecorations());
                frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
                if(isVisible)frame.setVisible(true);
            } else if (c instanceof JDialog) {
                JDialog dialog = (JDialog) c;
                boolean isVisible = dialog.isVisible();
                dialog.dispose();
                dialog.setUndecorated(lookAndFeel.getSupportsWindowDecorations());
                dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
                if(isVisible)dialog.setVisible(true);
            }
            if(c != null) SwingUtilities.updateComponentTreeUI(c);
        }
    }catch(Exception ex){
        throw new RuntimeException(ex);
    }
}


扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/856.html

分享给朋友:

“Swing动态切换Look And Feel” 的相关文章

对象序列化与反序列化

    序列化,并不是JAVA独有的。因此,在这里我用比较通俗的话说了。序列化就是把一个对象转换成有规则的二进制流。而反序列化就是把有规则的二进制数据重整成一个对象。其好处不难看见:1.可以把一个对象...…

JSP与Servlet的对应关系

以前在QQzone写下的文章现在贴到这里来了... 最近比较忙啊!现在抽身写一篇文章。是关于JSP与Servlet的对应关系的。希望对大家有所帮助。其实我也是刚刚学的......-------Servlet--------------JSP----------1.ServletContext&nbs…

JAVA获得一个文件夹大小

在JAVA里没有现成的方法获取一个文件夹的大小,那么我们可以用递归的方法,获取文件夹的大小。    import  java.util.*;  import  java.io.*;  class  GetFileSi…

JAVA内部类终极实例

最近心情不好,不想说太多东西了!电脑坏了,我现在又病了. class ClassFactory{ private final static String userName = "Hello,My name is CAnca."; public static Thread in =…

过滤网页HTML标记

JAVA过滤HTML中的所有标记。非常好用!! package canca.regex; import java.util.regex.Matcher;import java.util.regex.Pattern; public class HtmlFilter {  priva…

字符,字节和编码

字符,字节和编码

转自:http://www.regexlab.com/zh/encoding.htm------------------------------------------------------------- 级别:中级 摘要:本文介绍了字符与编码的发展过程,相关概念的正确理解。举例说明了一些实际应…

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。