7

所以我正在制作一个程序选择工具,目前我喜欢一切看起来只有 java 外观和感觉的方式。我唯一要更改的是 Windows 的 JFileChooser 外观。当我调用文件选择器并告诉它改变外观和感觉时,它什么也没做。当我在程序启动时调用它时,它使按钮看起来很糟糕。所以谷歌没有任何东西,我不知道如何让它工作。请帮忙!让我知道哪些代码是相关且有用的。提前致谢!

编辑:所以这里有一些与 JFileChooser 相关的代码以及它是如何启动的:

public class Start(){
    public static JButton assignButton = new JButton(new AbstractAction(
        "Assign") {
    public void actionPerformed(ActionEvent e) {
        AssignWindow.run();
    }
});
}

public class AssignmentWindow(){
   public static void run() {
    Dialogs.assignmentInfo();

    bgImage = aw.getImage("files/background.png");

            //aw is the object of this class
    aw.makeFrame();     //makes the jframe for all the buttons to sit.
    aw.setGraphics();   //assigns a different graphics variable

    aw.fileChooser();
}

public void fileChooser() {
    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

            // here is where i want to set the look and feel....

    if (jfc.showDialog(null, "Select") == JFileChooser.APPROVE_OPTION) {
        File file = jfc.getSelectedFile();
        fileDir = file.getPath();
    } else {
        Dialogs.msg("You cancled selecting a file. Returning to file frame...");
        AssignWindow.destroy();
    }
}
}
4

4 回答 4

16

所需要的只是在创建 JFileChooser 对象时更改 UIManager,然后将其设置回原来的状态,或者您可以只捕获异常,但这是不好的做法。

public void stuff(){
    JFileChooser chooser = null;
    LookAndFeel previousLF = UIManager.getLookAndFeel();
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        chooser = new JFileChooser();
        UIManager.setLookAndFeel(previousLF);
    } catch (IllegalAccessException | UnsupportedLookAndFeelException | InstantiationException | ClassNotFoundException e) {}
    //Add whatever other settings you want to the method
    chooser.showOpenDialog(frame);
}
于 2012-08-08T20:58:52.150 回答
6

是的,这是可能的。您可以手动设置 UI:

JFileChooser jfc = new JFileChooser();
WindowsFileChooserUI wui = new WindowsFileChooserUI(jfc);
wui.installUI(jfc);
jfc.showOpenDialog(parentComponent);

这将为文件选择器设置 Windows UI,但保留所有其他组件的外观。

于 2012-08-08T20:34:21.993 回答
4

push Cancel JButton,从MetaltoSystemNimbus Look and Feel

必须通过代码行调用对已经可见容器的所有更新

SwingUtilities.updateComponentTreeUI(Top-Level Container);

代码

import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;

class ChooserFilterTest {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                String[] properties = {"os.name", "java.version", "java.vm.version", "java.vendor"};
                for (String property : properties) {
                    System.out.println(property + ": " + System.getProperty(property));
                }
                JFileChooser jfc = new JFileChooser();
                jfc.showOpenDialog(null);
                jfc.addChoosableFileFilter(new FileFilter() {

                    @Override
                    public boolean accept(File f) {
                        return f.isDirectory() || f.getName().toLowerCase().endsWith(".obj");
                    }

                    @Override
                    public String getDescription() {
                        return "Wavefront OBJ (*.obj)";
                    }

                    @Override
                    public String toString() {
                        return getDescription();
                    }
                });
                int result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (Metal): " + (result == JOptionPane.YES_OPTION));
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(jfc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                jfc.showOpenDialog(null);
                result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (System): " + (result == JOptionPane.YES_OPTION));
                result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (Metal): " + (result == JOptionPane.YES_OPTION));
                try {
                    for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            SwingUtilities.updateComponentTreeUI(jfc);
                            break;
                        }
                    }
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                }
                jfc.showOpenDialog(null);
                result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (System): " + (result == JOptionPane.YES_OPTION));
            }
        };
        SwingUtilities.invokeLater(r);
    }

    private ChooserFilterTest() {
    }
}
于 2012-08-08T20:38:16.910 回答
2

你应该阅读Look and Feel
此外,我认为每个组件不能有不同的 L&F。至少我从未见过 L&F 不统一的应用程序

于 2012-08-08T20:28:18.890 回答