【问题标题】:javax.swing.UIManager.getIcon(Object key) returns null when String-keys are streamed from an ArrayListjavax.swing.UIManager.getIcon(Object key) 从 ArrayList 流式传输字符串键时返回 null
【发布时间】:2020-07-02 08:51:23
【问题描述】:

我想要达到的目标: 我想从javax.swing.UIManager 中可视化一些javax.swing.Icons。我在网上找到了UIManager-keys 的列表,它不仅会返回Icons,还会返回StringsColors 等。因此,在这一步中,我想过滤键列表,以便仅保留 Icon-keys。

我的方法: 我将UIManager 键的列表复制到一个文本文件中,并将其作为资源包含在我的Java 项目中。我成功读取了文件,所以我按行拆分文件内容并将它们添加到StringsArrayList。现在我想流式传输这个ArrayList 的内容并通过UIManager.getIcon(Object key)-方法是否返回null 来过滤键...

我的问题 到目前为止:UIManager 总是返回null。我将所有键和UIManager 结果打印到控制台(请参阅我的代码中的“输出/测试 - 流键”)。如果我从控制台手动复制一个密钥(我知道应该可以工作)并将其粘贴到完全相同的代码中,它实际上可以工作(请参阅我的代码中的“输出/测试 - 单个密钥”)。

有趣的行为显示当我将 String 附加到要打印到控制台的键上时(请参阅“输出/测试 - 流键”下的变量“后缀”在我的代码中)。如果变量suffix 不以“\n”开头,则流中的以下打印方法将只打印suffix,不再显示其他内容。例如,如果我键入 String suffix = "test";,则只会从 .forEach(key->System.out.println(... + key + suffix); 打印“测试”但是,此行为不会出现在“输出/测试 - 单键”-示例中。

我不知道发生了什么,或者(在我看来)奇怪的行为是否与问题有关。感谢您的任何帮助!

来自“UIManagerKeys.txt”的片段: 以下是一些用于测试和重现性目的的键...

FileView.computerIcon
FileView.directoryIcon
FileView.fileIcon
FileView.floppyDriveIcon
FileView.hardDriveIcon
FormattedTextField.background
FormattedTextField.border
windowText

我的代码:

package main;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.UIManager;

public class Main {

    public Main() {
    }

    public static void main(String args[]) {
        ArrayList<String> uiKeys = new ArrayList<>();
        String fileName = "recources/UIManagerKeys.txt";
        ClassLoader classLoader = new Main().getClass().getClassLoader();

        File file = new File(classLoader.getResource(fileName).getFile());

        // Check: is File found?
        System.out.println("File Found : " + file.exists());

        try {
            // Read File Content
            String content = new String(Files.readAllBytes(file.toPath()));

            // Split by line and collect
            String[] keys = content.split("\n");
            uiKeys.addAll(Arrays.asList(keys));
        } catch (IOException e) {
            System.out.println("Error: " + e);
        }

        // Output / Test - stream Keys
        System.out.println("Total Number of Keys: " + uiKeys.size());
        String suffix = ""; // Interesting behavior when String is not empty
        uiKeys.stream()
                .map(key -> key.replaceAll(" ", "").replaceAll("\n", "")) // Just to be sure
                .forEach(key -> System.out.println("IconFound: " + (UIManager.getIcon(key) != null) + "\tKey: " + key + suffix));

        // Output / Test - single Key
        System.out.println("\n");
        String key = "FileView.directoryIcon"; // Copied from console
        System.out.println("IconFound: " + (UIManager.getIcon(key) != null) + "\tKey: " + key + suffix);
    }
}

【问题讨论】:

    标签: java swing icons uimanager


    【解决方案1】:

    我想从 javax.swing.UIManager 中可视化一些 javax.swing.Icons。

    我将 UIManager 键的列表复制到一个文本文件中

    无需创建文本文件。您只需从 UIManager 获取所有属性并检查对象是否为图标:

    public static void main(String[] args) throws Exception
    {
        UIDefaults defaults = UIManager.getLookAndFeelDefaults();
    
        for ( Enumeration enumm = defaults.keys(); enumm.hasMoreElements(); )
        {
            Object key = enumm.nextElement();
            Object value = defaults.get( key );
    
            if (value instanceof Icon)
                System.out.println( key );
        }
    }
    

    在互联网上我找到了 UIManager 键的列表,

    查看:UIManager Defaults 获取显示所有属性的小图形工具。

    您读取文件的方法似乎有点复杂。这是将文件的行读入 ListArray 的简单方法:

    import java.io.*;
    import java.util.*;
    
    public class ReadFile
    {
        public static void main(String [] args) throws Exception
        {
            ArrayList<String> lines = new ArrayList<String>();
    
            BufferedReader in = new BufferedReader( new FileReader( "ReadFile.java" ) );
            String line;
    
            while((line = in.readLine()) != null)
            {
                lines.add(line);
            }
    
            in.close();
    
            System.out.println(lines.size() + " lines read");
        }
    }
    

    【讨论】:

    • 感谢您的宝贵时间!您的方法几乎可以满足我的所有需求。 (但是,我仍然很困惑为什么我的方法不起作用)。解决您的文件读取点:我猜我的方法是从资源中读取文件最简单的方法。据我所知,一旦项目被编译并用作库或独立工具,FileReader 将不再工作;但如果我错了,请纠正我。
    • 我仍然很困惑为什么我的方法不起作用) - 我从未使用过流,所以我不知道代码有什么问题。但是您的方法比我建议的要多得多。因此,如果一个步骤是错误的,那么整个方法都是错误的。这就是为什么你应该简化这些步骤。 一旦项目被编译并用作库或独立工具,FileReader 将不再工作 - 一旦您有权访问 File 对象,FileReader 将工作。
    猜你喜欢
    • 2013-10-25
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 2020-10-07
    • 2020-08-30
    • 2016-10-27
    相关资源
    最近更新 更多