简体   繁体   English

JFileChooser上的系统外观布局,但具有灵气的外观和感觉主题

[英]System look and feel layout on JFileChooser but with nimbus look and feel theme

The windows look and feel layout on JFileChooser is much better than other look and feels like nimbus. JFileChooser上的窗口外观和感觉布局比其他外观好很多,感觉就像灵气。

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. 所以我正在寻找一种方法来获得系统外观和感觉的布局,但在顶部有nimbus或其他主题。

Is this possible? 这可能吗? If so how can it be done? 如果是这样,怎么办呢?

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). 我设法通过要求视图在除了最顶层的JFileChooser组件之外的所有组件上更新自己来实现它(因为这将用你不想要的Nimbus组件替换所有选择器组件)。

I'd regard this as a hack that may or may not work depending on the internals of the Windows look and feel. 我认为这可能会或可能不会起作用,具体取决于Windows外观的内部结构。 It relies on pretty much the whole JFileChooser being built up by Swing components. 它几乎依赖于由Swing组件构建的整个JFileChooser。 If it ever was changed to use more direct native rendering (ie Java asks Windows to paint a significant portion of the chooser), it wont work. 如果它被更改为使用更直接的本机渲染(即Java要求Windows绘制选择器的重要部分),它将无法工作。 Don't know how well that trick will work with other components. 不知道这个技巧与其他组件的效果如何。

Anyway, this code seemed to work with JDK 7: 无论如何,这段代码似乎适用于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);
            }
        }
    }
}

I assume you are talking about the panel on the left side of the Windows file chooser dialog which has Desktop , My Computer My Documents icons? 我假设您正在讨论Windows文件选择器对话框左侧的面板,其中包含DesktopMy Computer My Documents图标?

Well, I doubt this can be done because this is LAF specific. 好吧,我怀疑这可以做到,因为这是特定于LAF的。 This was added to the Windows LAF because that is what the Windows platform file choose looks like. 这被添加到Windows LAF中,因为这是Windows平台文件选择的样子。 It is not support in other LAF's. 它不支持其他LAF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM