当前位置:首页 > Java技术 > 正文内容

Swing动态切换Look And Feel

canca2年前 (2023-07-25)Java技术442

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

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” 的相关文章

Socket与ServerSocket的问题

//服务器端:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.ServerSock...

Java语言的反射机制

    由于项目的需要,在项目中要实现即插即用的方式,也就是说可以动态地加载包,不用设置CLASSPATH路径。当项目发布时,不可能要用户来设置环境变量吧!因此,就要用到JAVA的反射机制了。昨天,我是在研究JAVA的JNI技术。由于没有时间,所以过几天才写JNI。今天...

JSP与Servlet的对应关系

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

JAVA获得一个文件夹大小

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

IM技术(1)

    做项目了,NetCL今天开工了,在这些日子里,我会将自己研究的内容写下来。做个记录,以下是我在网上搜到的。关于管理用户状态的解决方案,当然,我都有一个方案。不过对客户端的任务有点重吧,我方法是客户端从服务器端获到一个用户在线状态后,接着就与服务器无关了。好友离线...

JAVA内部类终极实例

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

发表评论

访客

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