更改 java 外观而不修改应用程序

发布于 2024-07-09 05:27:50 字数 877 浏览 12 评论 0原文

是否可以在不修改现实环境中的程序的情况下改变java外观?

我知道理论上答案是肯定的。 我还创建了演示应用程序,甚至在运行时也更改了外观,并且它有效。 但是,当我创建一个包含大量控件的真实应用程序时,外观和感觉的更改并没有真正起作用:

  • 不同的外观和感觉有不同的错误(或奇怪的功能)。 例如,其中一些为 JTextField 和 JFormattedTextField 创建相同的样式,其中一些使用不同的样式。 这种变化对于用户来说可能相当烦人。
  • 控件大小不一样(使用不同的字体,不同的边框),所以有时改变后窗口看起来很难看。
  • 键盘快捷键也可以不同。
  • ...

在外观和感觉更改后的应用程序中(实际上,我尝试使用具有不同皮肤的相同外观和感觉,而不是更改外观和感觉)我总是必须修改应用程序。 不是一个很大的修改,但有很多小的修改,这使得外观和感觉的改变变得困难。

我只是对我尝试过的外观感到非常不幸吗(目前使用 skinlf),或者外观变化不是和小型演示应用程序一样简单吗?

更新:我正在使用 Swing,现在无法更改为其他 GUI 库。

更新2:在我看来,这个问题并不清楚。 我确实知道如何改变外观。 我的问题是:是否有可能创建一个真正的应用程序,其中更改外观(或外观的皮肤)是一项简单的任务? 目前,如果我更改外观,仅更改 2 或 3 行是不够的,我必须检查整个应用程序并修复由外观更改引起的相当多的问题。 dhiller 建议用更好的外观和感觉替换 Skinlf。 你能给我展示一下皮肤替换效果更好的可换肤外观和感觉吗?

Is it possible to change java lookandfeel without modifying the program in a real-world environment?

I know that theoretically the answer is yes. I also created demo applications and changed the lookandfeel even at runtime and it worked. But when I created a real application with lots of controls lookandfeel change was not really working:

  • Different lookandfeels have different bugs (or strange features). For instance some of them creates the same style for JTextField and JFormattedTextField, some of them uses different styles. This change can be quite annoying for users.
  • Control sizes are not the same (using different fonts, different borders), so sometimes the window looks ugly after change.
  • Keyboard shortcuts can also be different.
  • ...

In my applications after lookandfeel change (actually instead of changing the lookandfeel I tried using the same lookandfeel with different skins) I always have to modify the application. Not a very big modification, but lots of small modifications which makes lookandfeel change difficult.

Am I just very unlucky with the lookandfeels I tried (currently using skinlf), or lookandfeel change is not that easy as in small demo applications?

UPDATE: I'm using Swing and changing to other GUI library is not possible for me right now.

UPDATE2: It seems to me the question was not clear. I do know how to change the lookandfeel. My questions is: Is it possible to create a real application where changing lookandfeel (or the skin of the lookandfeel) is an easy task? Currently if I change the lookandfeel it is not enough to change 2 or 3 lines, I have to review the whole application and fix quite a few problems caused by the lookandfeel change. dhiller suggested replacing skinlf with a better lookandfeel. Can you show me a skinable lookandfeel where skin replacement works better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

少女净妖师 2024-07-16 05:27:50

是的,有多种方法可以在不修改应用程序代码的情况下以特定的外观启动应用程序。 一种简单的方法是在应用程序启动时设置系统属性。 例如,如果您有启动应用程序的 shell 脚本或批处理文件,则只需将命令从 -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel 更改

java -jar yourapp.jar

java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel -jar yourapp.jar

GTKLookAndFeel 即可设置默认外观和感觉。

还有一种方法是使用 Java 安装的 swing.properties 文件。 有关更多详细信息,请参阅

Yes, there are different ways to start your application with a certain look and fell without modifying the code of your application. An easy way is to set a system property at startup of your application. E.g. if you have a shell script or batch file starting your application you just change the command from

java -jar yourapp.jar

to

java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel -jar yourapp.jar

-Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel will set the default look and feel to use to the GTKLookAndFeel.

There is also a way using a swing.properties file of your Java installation. For more details have a look at this.

つ低調成傷 2024-07-16 05:27:50

一般来说,在我看来,使用系统外观和感觉是一个好主意,以避免在用户已经习惯了平台外观和感觉时通过引入新外观来使用户感到困惑。 平台外观可以通过以下代码实现。

  UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

无论如何,请参阅 更改启动后的外观 (java教程),这可能对您有帮助。

顺便说一句:也许您使用了错误的布局管理器?

In general it is a good idea IMO to use the system look and feel to avoid confusing the user by introducing a new look when he's already accustomed to the platform look and feel. The platform look and feel can be achieved by the following code.

  UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

Anyway, see Changing the Look and Feel After Startup (java tutorial), that might help you.

BTW: Maybe you are using the wrong layout managers?

梦情居士 2024-07-16 05:27:50

您是否尝试过在 UIManager 上设置外观? 只是在开玩笑。 不过,说真的,如果您更改外观和感觉并且应用程序看起来不正确,那是因为您没有以正确的方式使用 LayoutManager。 最容易理解的示例是使用空布局管理器。 这种方法不会跨平台(极端的例子)。 作为一种开发技术,您应该使用各种 LookAndFeels 来完成所有 Swing 概念测试。 否则,您会试图在应用程序中“让事情正常运转”,但这不是重点。 真正理解布局可能很困难。

也就是说,像键盘快捷键这样的东西总是需要特殊处理。 Mac 上的菜单需要特殊处理。 等等...然后是应用程序打包。

所以,是的,Java 在小型桌面演示中非常有用,并且需要努力才能使真正的应用程序变得完美。

Have you tried setting the look and feel on the UIManager? Just kidding. Seriously, though, if you change look and feel and the app does not look right, it's because you're not using the LayoutManagers in the correct way. The easiest-to -grok example of this is using a null layout manager. This approach will not be cross-platform (extreme example). As a development technique, you should be doing all your Swing concept tests using various lookAndFeels. Otherwise you'll be tempted to "make things work" in your app, which is not the point. Really understanding the layouts can be hard work.

That said, some things like keyboard shortcuts need special handling always. Menus on Mac need special handling. Etc... and then there's app packaging.

So yeah, Java is great in small desktop demos, and requires work to get a real app perfect.

夏末 2024-07-16 05:27:50

外观和感觉应该改变字体和行为,这就是外观和感觉系统的目的:它应该像本机操作系统一样运行,并且不同的操作系统有不同的显示方式。 我必须承认,“Java”的外观和感觉(称为金属或其他东西)通常比许多标准操作系统版本更漂亮(自从我上次使用 Java 以来,这可能已经改变了几年)。 我建议您默认使用操作系统的外观和感觉,因为如果打开文件菜单的行为与其他程序不同(不幸的是 Windows 用户习惯于不一致的 gui,但 Mac 用户希望所有程序都如此),就会令人困惑行为得体)。

The look and feel should change fonts and behaveour, that's the purouse of the look and feel system: it should behave as the native OS, and different OS have different ways of displaying things. I must confess that the "Java" look and feel (called metallic or something) is generally more pretty than many standard OS versions (This may have changed its a few years since I last used Java). I reccomend you by default use the OS look and feel, because it's confusing if the, for instance, Open file menu does not behave as it does in every other program (windows users is unfortunately used to inconsistent gui, but Mac users expect all program to behave properly).

许你一世情深 2024-07-16 05:27:50

如果您的最终目标是每个目标操作系统的本机外观和感觉,您可以查看 SWT。 这将提供操作系统本机小部件,这些小部件将随着操作系统的外观和感觉而变化。

这样,用户不需要更改应用程序的选项,他们所需要做的就是修改操作系统主题,所做的更改将自动传播到您的应用程序。

If your final goal is native look and feel for each target OS, you can look at SWT. This will provide OS native widgets which will change along with the OS look and feel.

That way, the user does not need to change the options for your application, all they need to do is modify their OS theme and the changes will propogate automatically to your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文