How to use Windows XP theme in Java/Swing applications?

只谈情不闲聊 提交于 2019-12-10 19:52:12

问题


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 can be used when someone wants to use the same theme as currently applied by the user.

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

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

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?


回答1:


I usually use this line

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

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

or you can use this:

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

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

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
}

or simply:

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
}

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.

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

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




回答2:


One way is to implement it in constructor:

public class LookS extends JFrame { 

  String win1 = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
  String win2 = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";

public LookS(){
...

        try {
            UIManager.setLookAndFeel(win1);
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.setDefaultLookAndFeelDecorated(true);
...
}

Also, you can consider these classes too:

  String srt1 = "javax.swing.plaf.metal.MetalLookAndFeel";
  String srt2 = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
  String srt3 = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
  String srt4 = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";



回答3:


UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

this will get you the native look and feel



来源:https://stackoverflow.com/questions/16459758/how-to-use-windows-xp-theme-in-java-swing-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!