java - javax.swing.UIManager.getIcon(Object key) 从 ArrayList 流式传输字符串键时返回 null
问题描述
我想要实现的目标:
我想javax.swing.Icon
从javax.swing.UIManager
. 在互联网上,我找到了一个UIManager
-keys 列表,它不仅会返回,还会返回Icons
,Strings
等等Colors
。因此,在这一步中,我想过滤键列表,以便仅Icon
保留 -keys。
我的方法:
我将一个键列表复制UIManager
到一个文本文件中,并将其作为资源包含在我的 Java 项目中。我成功读取了文件,因此我按行拆分文件内容并将它们添加ArrayList
到Strings
. 现在我想流式传输它的内容并通过 -Method 是否返回ArrayList
来过滤键......UIManager.getIcon(Object key)
null
到目前为止我的问题UIManager
:总是返回null
。我将所有键和结果打印UIManager
到控制台(请参阅我的代码中的“输出/测试-流键”)。如果我从控制台手动复制一个密钥(我知道应该可以工作)并将其粘贴到完全相同的代码段中,它实际上可以工作(请参阅我的代码中的“输出/测试 - 单个密钥”)。
当我将 a 添加String
到要打印到控制台的键上时,会显示有趣的行为(请参阅我的代码中“输出/测试 - 流键”下的变量“后缀”)。如果变量suffix
不以“\n”开头,则流中的以下 print-Method 将只打印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);
}
}
解决方案
I want to visualize some javax.swing.Icons from the javax.swing.UIManager.
I copied a list of UIManager keys into a textfile
There is no need to create the text file. You just get all the properties from the UIManager and check if the Object is an Icon:
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 );
}
}
On the internet I've found a list of UIManager-keys,
Check out: UIManager Defaults for a little graphical tool that displays all the properties.
Your approach of reading the files seems a little complicated. Here is a simple approach to reading the lines of a file into a 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");
}
}
推荐阅读
- prestashop-1.7 - 解释 Prestashop 上的模块联系表
- java - 从 HashMap 返回 ArrayList
- angular - 角度 7 中的 require("http2")
- reactjs - React 如何让子组件拥有自己的状态
- c++ - 用 Qt::black 初始化 QColorDialog 总是返回黑色
- angular - Angular 获取 url 的最后一部分
- xml - Eclipse IDE 不填充示例 XML 文件中的元素和属性
- javascript - 使用 Javascript 计算 HTML 中的行数
- sql - 复杂的 SQL 请求
- azure-devops - 将用户添加到项目而不是组织的后果