Getting java applications to look native on windows - how?

前端 未结 8 2290
悲哀的现实
悲哀的现实 2021-02-08 03:13

Is it possible to use Java to create apps that look native on Windows? I don\'t care if the solution is portable or not, because I only plan to target windows users. I am using

相关标签:
8条回答
  • 2021-02-08 03:19

    You'd do something like:

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

    (Which of course works only on Windows.) The result looks and feels reasonably native. More info e.g. here.

    0 讨论(0)
  • 2021-02-08 03:24

    It's strange no one has mentioned JGoodies yet.

    The JGoodies Windows look&feel focuses on a precise emulation on Windows 95/98/NT/ME/2000 in the following areas: menus, icons, colors, borders, fonts, font sizes, insets, and widget dimensions. It honors the screen resolution (96dpi vs. 120 dpi) to adjust sizes, insets, and widget dimensions. (Source)

    0 讨论(0)
  • 2021-02-08 03:31

    Everyone else has posted Swing things, so I'm going to play Devil's advocate and mention SWT.

    SWT is a widget toolkit produced by the Eclipse foundation. It is a thin wrapper over the system's native GUI... for Windows, OSX, and various flavors of *nix (Linux, AIX, BSDs?, etc...).

    This is the opposite route that Sun's JFC/Swing took, which draws its own components.

    0 讨论(0)
  • 2021-02-08 03:32
    try {
        // Set the Look and Feel of the application to the operating
        // system's look and feel.
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (ClassNotFoundException e) {
    }
    catch (InstantiationException e) {
    }
    catch (IllegalAccessException e) {
    }
    catch (UnsupportedLookAndFeelException e) {
    }
    

    That should set the Look and Feel to the system look and feel. You would do this before any of your GUI code. For example, in your main method.

    If you want to learn more about Look and Feels, I would check out the Java Tutorial on them, as suggested by carwash.

    0 讨论(0)
  • 2021-02-08 03:35

    You have to use Windows look and feel.

    You can specify it at the command line:

    java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel MyApp
    

    Or in code

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    

    Here are the details: How to set the look and feel

    0 讨论(0)
  • 2021-02-08 03:38

    See here: Java™ Tutorials: How to Set the Look and Feel

    try {
        // Set System L&F
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } 
    catch (UnsupportedLookAndFeelException e) {
       // handle exception
    }
    
    0 讨论(0)
提交回复
热议问题