如何在 KDE 上强制/开始使用 Java 中的 GTKLookAndFeel?
首先,使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以从命令行设置外观:
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
也许这有效:
maybe this works:
引用文档:
但我 99% 确定您的问题是这个(再次引用文档):
如果您不想在所有
JComponents
上调用updateUI
,请务必在每个之前调用UIManager.setLookAndFeel
其他摆动代码。Quoting the documentation:
But I'm 99% sure that your problem is this one (quoting the docs again):
If you don't want to invoke
updateUI
on allJComponents
, be sure to invokeUIManager.setLookAndFeel
before every other swing code.恕我直言,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.