代码之家  ›  专栏  ›  技术社区  ›  ryeguy

让Java应用程序在Windows上看起来是原生的——如何?

  •  11
  • ryeguy  · 技术社区  · 15 年前

    是否有可能使用Java创建Windows上的本地应用程序?我不在乎该解决方案是否可移植,因为我只计划针对Windows用户。如果这很重要的话,我正在使用scala。

    很抱歉没有细节,但我以前从未使用过Java,所以我甚至不确定这是否可能。

    8 回复  |  直到 15 年前
        1
  •  23
  •   Community    7 年前
    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) {
    }
    

    这应该将外观和感觉设置为系统的外观和感觉。您可以在任何GUI代码之前执行此操作。例如,在 main 方法。

    如果你想了解更多的外观和感觉,我会查看 Java Tutorial on them , as suggested by carwash .

        2
  •  9
  •   Powerlord    15 年前

    其他人都张贴了摇摆的东西,所以我要扮演魔鬼的拥护者并提到 SWT .

    SWT是Eclipse基金会生产的一个小部件工具包。它是系统本机GUI上的一个薄包装器…对于Windows、OSX和各种各样的*nix(Linux、AIX、BSD?)等等)。

    这是与太阳相反的路线 JFC/Swing 取,它绘制自己的组件。

        3
  •  4
  •   user74455    15 年前

    请参见这里: Java™ Tutorials: How to Set the Look and Feel

    try {
        // Set System L&F
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } 
    catch (UnsupportedLookAndFeelException e) {
       // handle exception
    }
    
        4
  •  2
  •   OscarRyz    15 年前

    你必须使用窗户的外观和感觉。

    您可以在命令行中指定它:

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

    或在代码中

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    

    详情如下: How to set the look and feel

        5
  •  2
  •   fglez    15 年前

    奇怪的是没人提到 JGoodies 然而。

    jgoodies Windows look&feel着重于在以下方面对Windows 95/98/NT/ME/2000进行精确模拟:菜单、图标、颜色、边框、字体、字体大小、插入和小部件尺寸。它尊重屏幕分辨率(96dpi对120dpi)来调整大小、插入和小部件尺寸。( Source )

        6
  •  0
  •   Michael Ekstrand    15 年前

    是的,Java在Windows上有一个Windows本地的外观和感觉。看看如何改变你的外观和感觉,这应该会让你朝着正确的方向前进。

        7
  •  0
  •   Jonik    15 年前

    你会做如下的事情:

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

    (当然只能在Windows上使用)结果看起来和感觉都相当自然。更多信息如 here .

        8
  •  0
  •   St.Shadow    15 年前

    你应该使用本地的 Look&Feel