【问题标题】:How to change the look and feel of a single part of a java program如何更改 Java 程序单个部分的外观
【发布时间】:2013-07-03 15:45:26
【问题描述】:

所以我正在制作一个程序选择工具,目前我喜欢一切看起来只有 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();
    }
}
}

【问题讨论】:

  • 好吧,我有一堆代码,所以我不确定要包含哪些有用的部分...
  • 您的问题与 JFileChooser 外观有关。这是需要审查的部分。它还将帮助您自己分析问题,隔离相关代码。

标签: java swing user-interface look-and-feel jfilechooser


【解决方案1】:

只需在创建 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);
}

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 你能解释一下这有什么帮助吗?
    • 确定 WindowFileChooserUI 是导入的,对吗?因为当我尝试这个时它给了我一个错误......
    • 如果这不能解决你的问题,我不明白你在做什么。您的代码不包含您更改外观的部分。
    • 这个解决方案对我不起作用。我尝试将它与 JScrollPane 一起使用,因为我使用修改后的滚动窗格制作了自己的 LaF。滚动窗格也用于组合框中,这些应该保持修改,但在我的情况下,如果 JTables 附加到它,则有必要使用金属 LaF 来滚动窗格。
    【解决方案3】:

    推送Cancel JButton,有MetalSystemNimbus 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() {
        }
    }
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 我已经查看了该链接,并且我知道该怎么做。那不是我想要的。我想为文件选择器设置 l&f,而不是这个窗口。如果我这样设置,一切都会改变。
      • 您从哪里了解到每个组件可以有不同的 L&F?你有参考吗?
      • @wbAnon 您面临的问题是,如果您更改给定 UI 组件的值,则会影响所有未来创建的 UI 组件(例如 JListJButton、@987654324 @等)
      • 我听说它就像改变滚动条的外观之类的,不幸的是我没有参考
      • @wbAnon:您在 OP 中所说的尝试更新 L&F 的代码在哪里?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 2011-05-07
      相关资源
      最近更新 更多