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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1590863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:18:30  来源:igfitidea点击:

Getting java applications to look native on windows - how?

javawindowsuser-interfacescala

提问by ryeguy

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 Scala if that matters.

是否可以使用 Java 来创建在 Windows 上看起来本机的应用程序?我不在乎解决方案是否可移植,因为我只打算针对 Windows 用户。如果这很重要,我正在使用 Scala。

Sorry for the lack of details, but I have never used Java before so I'm not even sure if this is possible.

抱歉缺少细节,但我以前从未使用过 Java,所以我什至不确定这是否可能。

回答by Thomas Owens

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 mainmethod.

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

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

如果您想了解更多有关的外观和感觉,我想看看在Java教程上他们用洗车的建议

回答by Powerlord

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

其他人都发布了 Swing 的东西,所以我将扮演魔鬼的拥护者并提到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...).

SWT 是由 Eclipse 基金会制作的小部件工具包。它是系统原生 GUI 的薄包装器……适用于 Windows、OSX 和各种 *nix(Linux、AIX、BSD?等)。

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

这与 Sun 的JFC/Swing走的路线相反,它绘制自己的组件。

回答by Powerlord

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

看这里:Java?教程:如何设置外观

try {
    // Set System L&F
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} 
catch (UnsupportedLookAndFeelException e) {
   // handle exception
}

回答by OscarRyz

You have to use Windows look and feel.

您必须使用 Windows 外观。

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

以下是详细信息:如何设置外观

回答by fglez

It's strange no one has mentioned JGoodiesyet.

奇怪的是还没有人提到JGoodies

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)

JGoodies Windows 外观和感觉专注于在以下领域对 Windows 95/98/NT/ME/2000 进行精确模拟:菜单、图标、颜色、边框、字体、字体大小、插图和小部件尺寸。它遵循屏幕分辨率(96dpi 与 120 dpi)来调整大小、插图和小部件尺寸。(来源

回答by St.Shadow

Your should use native Look&Feel

你应该使用原生的Look&Feel

回答by Michael Ekstrand

Yes, Java does have a Windows-native look and feel available on Windows. Look up how to change your look-and-feel and that should take you in the right direction.

是的,Java 在 Windows 上确实具有 Windows 原生的外观和感觉。看看如何改变你的外观和感觉,这应该会带你走向正确的方向。

回答by Jonik

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.

(这当然只适用于 Windows。)结果看起来和感觉都相当原生。更多信息,例如这里