我如何强制/获得在 KDE 上的 Java 中使用 GTKLookAndFeel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/209731/
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-10-29 11:26:02  来源:igfitidea点击:

How do I force/get to use GTKLookAndFeel in Java on KDE?

javaswinggtklook-and-feelkde

提问by Luis Soeiro

First of all, using gnome is not an option (but it is possible to install its libraries).

首先,使用 gnome 不是一种选择(但可以安装它的库)。

I need to know what is necessary to display a Java Swing desktop application using the current installed KDE look and feel of KDE. Ideally, the solution should allow me to apply a look and feel that looks like the underlying windowing system (ie: Windows LNF for Windows, GTK LNF for Gnome(GTK), QT LNF for KDE (QT), the default one for other platforms).

我需要知道使用当前安装的 KDE 外观和风格来显示 Java Swing 桌面应用程序需要什么。理想情况下,该解决方案应该允许我应用看起来像底层窗口系统的外观(即:Windows LNF for Windows、GTK LNF for Gnome(GTK)、QT LNF for KDE (QT),其他平台的默认设置)。

Under KDE, you can configure it to use the current KDE theme for GTK applications, too. So, if the solution works with GTK it is fine.

在 KDE 下,您也可以将其配置为对 GTK 应用程序使用当前的 KDE 主题。因此,如果该解决方案适用于 GTK,那就没问题了。

When I run the following piece of code under Gnome (Ubuntu 8.04), the Java application looks beautiful. It integrates very well with the rest of applications:

当我在 Gnome (Ubuntu 8.04) 下运行以下代码时,Java 应用程序看起来很漂亮。它与其他应用程序集成得很好:

try {
  // Set System L&F
  UIManager.setLookAndFeel(
  UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) { //Handle it }

However, if I run the same thing under Debian (Lenny) with KDE, the UIManager.getSystemLookAndFeelClassName() call returns the Java default one. If I go ahead and force it to use the GTK LNF, the application doesn't work. Some fields are invisible, others become out of place, everything is unusable:

但是,如果我在带有 KDE 的 Debian (Lenny) 下运行相同的东西, UIManager.getSystemLookAndFeelClassName() 调用将返回 Java 默认值。如果我继续并强制它使用 GTK LNF,应用程序将无法工作。有些领域是不可见的,有些领域变得不合适,一切都无法使用:

try {
  //Force the GTK LNF on top of KDE, but **it doesn't work**
  UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} catch (Exception e) { /*Handle it*/ }

I've also tried to put the following code. It let's the user chose any one of the available LNF and then tries to set it. Metal and Motif work fine. GTK doesn't. The slider is really messed up. The list box looks ugly and disappears, but seems to work. Buttons and menu seem ok. The relevant code is shown here:

我也试过把下面的代码。它让用户选择任何一个可用的 LNF,然后尝试设置它。Metal 和 Motif 工作正常。GTK 没有。滑块真的搞砸了。列表框看起来很难看并消失了,但似乎有效。按钮和菜单似乎没问题。相关代码如下所示:

(...)
    /** Creates new form SwingFrame */
    public SwingFrame() {

    initComponents();

    //Save all available lafs in a combobox
    cbLafs.removeAllItems();
    UIManager.LookAndFeelInfo[] lafs=UIManager.getInstalledLookAndFeels();

    for (int i=0,t=lafs.length;i<t;i++)
    {
        cbLafs.addItem(lafs[i]);
        System.out.println(lafs[i].getName());
    }

    }

public void changeLookAndFeel(String laf)
{
    //If not specified, get the default one
    if (laf==null) {
    laf=UIManager.getSystemLookAndFeelClassName();
    }

        try {
        // Set System L&F
        UIManager.setLookAndFeel(laf);
    }
    catch (Exception e) {
       // handle exception
        e.printStackTrace();
    }
    SwingUtilities.updateComponentTreeUI(this);

}

   private void cbLafsActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        UIManager.LookAndFeelInfo laf=(UIManager.LookAndFeelInfo)cbLafs.getSelectedItem();
        if (laf==null)
            changeLookAndFeel(null);
        else
            changeLookAndFeel(laf.getClassName());
    }                                     

This same system has all GTK applications working (for example: Firefox) as expected. So:

同一个系统让所有 GTK 应用程序(例如:Firefox)按预期工作。所以:

1) What is missing from the environment to have a Java GTK LNF application working under KDE?

1) 在 KDE 下运行 Java GTK LNF 应用程序的环境中缺少什么?

2) What does the JVM checks for to return GTK as the default system theme?

2) JVM 检查什么以返回 GTK 作为默认系统主题?

Thanks for you help Luis Fernando

感谢您帮助路易斯·费尔南多

PS->I've tried other solutions,too, such as JGoodies, plain AWT and SWT. However, Swing with GTK LNF would be the best solution to avoid the hassle of SWT native libraries and JGoodies extra jars (also, JGoodies LNF doesn't look as integrated as Swing GTK under Gnome). AWT looks hideous (motif-like) and misses lots of features.

PS->我也尝试过其他解决方案,例如 JGoodies、plain AWT 和 SWT。然而,Swing with GTK LNF 将是避免 SWT 本地库和 JGoodies 额外 jar 麻烦的最佳解决方案(而且,JGoodies LNF 看起来不像 Gnome 下的 Swing GTK 那样集成)。AWT 看起来很丑陋(类似主题)并且缺少很多功能。

回答by Joshua

You can set the look and feel from the command line:

您可以从命令行设置外观:

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

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

Also, SwingSet2.jnlpprovides a sample demo of all the different things that can be changed. The source and other info can be found here: link text

此外,SwingSet2.jnlp提供了所有可以更改的不同内容的示例演示。来源和其他信息可以在这里找到:链接文本

回答by Davide

Quoting the documentation:

引用文档:

  1. If the system property swing.defaultlafis non-null, use its value as the default look and feel class name.

  2. If the Properties file swing.propertiesexists and contains the key swing.defaultlaf, use its value as the default look and feel class name. The location that is checked for swing.propertiesmay vary depending upon the implementation of the Java platform. In Sun's implementation the location is ${java.home}/lib/swing.properties

Refer to the release notes of the implementation being used for further details.

  1. 如果系统属性swing.defaultlaf为非空,则使用其值作为默认外观类名称。

  2. 如果 Properties 文件swing.properties存在并包含 key swing.defaultlaf,则使用其值作为默认的外观类名。检查的位置swing.properties可能因 Java 平台的实现而异。在 Sun 的实现中,位置是${java.home}/lib/swing.properties

有关更多详细信息,请参阅正在使用的实现的发行说明。

But I'm 99% sure that your problem is this one (quoting the docs again):

但我 99% 确定您的问题是这个(再次引用文档):

Once the look and feel has been changed it is imperative to invoke updateUIon all JComponents. The method SwingUtilities.updateComponentTreeUI(java.awt.Component)makes it easy to apply updateUI to a containment hierarchy. Refer to it for details. The exact behavior of not invoking updateUI after changing the look and feel is unspecified. It is very possible to receive unexpected exceptions, painting problems, or worse.

一旦外观和感觉发生变化,就必须调用updateUI所有JComponents. 该方法SwingUtilities.updateComponentTreeUI(java.awt.Component)可以轻松地将 updateUI 应用于包含层次结构。有关详细信息,请参阅它。未指定更改外观后不调用 updateUI 的确切行为。很可能会收到意外的异常、绘画问题,或者更糟的情况。

If you don't want to invoke updateUIon all JComponents, be sure to invoke UIManager.setLookAndFeelbeforeevery other swing code.

如果您不想updateUI在 all上调用JComponents,请确保UIManager.setLookAndFeel所有其他 Swing 代码之前调用。

回答by Marcus Tik

maybe this works:

也许这有效:

try {
// sure look and feel
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
// not-so-sure look and feel
System.setProperty("os.name", "Windows");
System.setProperty("os.version", "5.1");
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} 
catch (Exception ex) {
ex.printStackTrace();
}

回答by KitsuneYMG

The GTK Laf is, IMHO, broken period. It does nothonor some random settings. I believe it is not supposed to honor anysetBackground(), setForeground(), or setFont() on most components.

恕我直言,GTK Laf 是坏的时期。它并没有兑现一些随机设置。我相信它不应该在大多数组件上尊重任何setBackground()、setForeground() 或 setFont()。

If you are using java >1.4.2 I suggest using MetalLookAndFeel [should be UIManager.getCrossPlatformLookAndFeelClassName()]. If you are using >1.6.0_u10 you can try NautilusLookAndFeel. I personally find Metal nicer looking.

如果您使用的是 java > 1.4.2,我建议使用 MetalLookAndFeel [应该是 UIManager.getCrossPlatformLookAndFeelClassName()]。如果您使用的是 >1.6.0_u10,您可以尝试 NautilusLookAndFeel。我个人觉得金属更好看。