如何在Java/Swing应用程序中使用Windows XP主题? [英] How to use Windows XP theme in Java/Swing applications?

查看:110
本文介绍了如何在Java/Swing应用程序中使用Windows XP主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在Java/Swing项目中使用Windows XP主题. 像下面这样的类.

I have to use Windows XP theme in my Java/Swing project. There are classes like below.

com.sun.java.swing.plaf.windows.WindowsLookAndFeel

当某人想要使用与用户当前应用的主题相同的主题时,可以使用

WindowsLookAndFeel.

WindowsLookAndFeel can be used when someone wants to use the same theme as currently applied by the user.

com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel

当有人要专门使用Windows经典主题时,可以使用

WindowsClassicLookAndFeel.

WindowsClassicLookAndFeel can be used when someone wants to specifically use the windows classic theme.

当我们将Windows的主题从XP更改为经典时,swing UI也将从XP更改为经典.我们如何使用前面提到的类来强制它?

When we change a theme of Windows from XP to classic, swing UI also changes from XP to classic. How can we force it using aforementioned classes?

推荐答案

我通常使用此行

javax.swing.UIManager.setLookAndFeel( javax.swing.UIManager.getSystemLookAndFeelClassName());

因为如果您在另一个系统中运行该应用程序,则Windows可能不会具有外观.

because if you run the application in another system you could haven't windows look and feel.

或者您可以使用此:

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("<lookAndFeelName>".equals(info.getName())) {
        javax.swing.UIManager.setLookAndFeel(info.getClassName());
        break;
    }
}

因为这种方式,如果要强制执行Windows外观,则应验证外观是否存在

because in this way, if you want to force the windows Look and feel you should verify if this lookAndFeel exists

或者您有2种好的方法来设置lookAndFeel而不检查是否存在:

Or you have 2 good ways to set lookAndFeel without check if exists:

try {
    javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    //javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
} catch (ClassNotFoundException ex) {
    //Handle Exception
} catch (InstantiationException ex) {
    //Handle Exception
} catch (IllegalAccessException ex) {
    //Handle Exception
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
    //Handle Exception
}

或简单地:

try {
    javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    //javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
} catch (Exception ex) {
    //Handle Exception
}

WindowsLookAndFeel和WindowsClassicLookAndFeel之间的区别在于,"WindowsLookAndFeel"将对应于用户Windows主题,而"WindowsClassicLookAndFeel"将强制Java应用程序GUI匹配Windows经典主题,即使用户未将其Windows主题设置为经典.

The difference between WindowsLookAndFeel and WindowsClassicLookAndFeel, is that "WindowsLookAndFeel" will corresponds to user Windows Theme and "WindowsClassicLookAndFeel" will force java application GUI to match Windows Classic Theme even if user has not set their Windows Theme to classic.

因此,您可以强制使用Classic,但不能使用Windows Classic Theme强制Vista/7 LookAndFeel.

So you can force classic but you can't force Vista/7 LookAndFeel using Windows Classic Theme.

或者您可以将应用程序设计为类似Vista/7 LookAndFeel(只是在开玩笑)

Or you can design your application to anything like Vista/7 LookAndFeel (just kidding)

这篇关于如何在Java/Swing应用程序中使用Windows XP主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆