JFileChooser上的系统外观布局,但具有灵气的外观和感觉主题 [英] System look and feel layout on JFileChooser but with nimbus look and feel theme

查看:466
本文介绍了JFileChooser上的系统外观布局,但具有灵气的外观和感觉主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JFileChooser上的Windows外观和感觉布局比其他外观要好得多,感觉就像灵气。

The windows look and feel layout on JFileChooser is much better than other look and feels like nimbus.

所以我正在寻找一种方法来布局一个系统的外观和感觉,但顶部有nimbus或其他主题。

So I'm looking for a way to have the layout of a system look and feel but have nimbus or others theme on top.

这可能吗?如果是这样,怎么办呢?

Is this possible? If so how can it be done?

推荐答案

虽然我不知道是否推荐,但这是可能的。我设法通过要求视图在除最顶层的JFileChooser组件之外的所有组件上更新自己来实现它(因为这将用你不想要的Nimbus组件替换所有选择器组件)。

It's possible, though I don't know whether it's recommended. I managed to get it to work by asking the view to update itself on all but the topmost JFileChooser component (since that would replace all the chooser components with the Nimbus ones which you don't want).

我认为这可能会或可能不会起作用,具体取决于Windows外观的内部结构。它几乎依赖于由Swing组件构建的整个JFileChooser。如果它被更改为使用更直接的本机渲染(即Java要求Windows绘制选择器的重要部分),它将无法工作。不知道这个技巧对其他组件的效果如何。

I'd regard this as a hack that may or may not work depending on the internals of the Windows look and feel. It relies on pretty much the whole JFileChooser being built up by Swing components. If it ever was changed to use more direct native rendering (i.e. Java asks Windows to paint a significant portion of the chooser), it wont work. Don't know how well that trick will work with other components.

无论如何,这段代码似乎适用于JDK 7:

Anyway, this code seemed to work with JDK 7:

package test;

import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel; //Or use com.sun.... if you are using JDK < 7

public class LAFTester
{
    public static void main(String... args)
    throws Exception
    {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JFileChooser chooser = new JFileChooser();
        chooser.updateUI(); //Create UI objects
        UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName()); //Now set look and feel
        //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); //works with metal as well
        refreshUI(chooser, false);

        chooser.showOpenDialog(null);
    }

    private static void refreshUI(JComponent c, boolean includeParent)
    {
        if (includeParent)
            c.updateUI();

        for (int i = 0; i < c.getComponentCount(); i++)
        {
            Component child = c.getComponent(i);
            if (child instanceof JComponent)
            {
                refreshUI((JComponent)child, true);
            }
        }
    }
}

这篇关于JFileChooser上的系统外观布局,但具有灵气的外观和感觉主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆