如何在 KDE 上强制/开始使用 Java 中的 GTKLookAndFeel?

发布于 2024-07-07 14:31:07 字数 2652 浏览 13 评论 0原文

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

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

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

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

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

但是,如果我在 Debian (Lenny) 下使用 KDE 运行相同的东西,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*/ }

我还尝试添加以下代码。 它让用户选择任何一种可用的 LNF,然后尝试设置它。 金属和图案效果很好。 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());
    }                                     

在同一系统中,所有 GTK 应用程序都按预期工作(例如:Firefox)。 那么:

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

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

谢谢你的帮助 Luis Fernando

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

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

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

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.

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:

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:

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:

(...)
    /** 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:

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

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

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.

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

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

发布评论

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

评论(4

苏璃陌 2024-07-14 14:31:07

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

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

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

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

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

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

梦屿孤独相伴 2024-07-14 14:31:07

也许这有效:

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();
}

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();
}
暗恋未遂 2024-07-14 14:31:07

引用文档:

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

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

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

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

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

如果您不想在所有 JComponents 上调用 updateUI,请务必在每个之前调用 UIManager.setLookAndFeel其他摆动代码。

Quoting the documentation:

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

  2. If the Properties file swing.properties exists and contains the key swing.defaultlaf, use its value as the default look and feel class name. The location that is checked for swing.properties may 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.

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

Once the look and feel has been changed it is imperative to invoke updateUI on 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.

If you don't want to invoke updateUI on all JComponents, be sure to invoke UIManager.setLookAndFeel before every other swing code.

天气好吗我好吗 2024-07-14 14:31:07

恕我直言,GTK Laf 是一个破碎的时期。 它遵循某些随机设置。 我相信它不应该在大多数组件上尊重任何 setBackground()、setForeground() 或 setFont()。

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

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

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.

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