这是我上一个问题的后续问题:
需要 FileDialog 和 Java 中的文件类型过滤器
我有一个 JFileChooser(使用它而不是 FileDialog,这样我就可以有一个文件类型过滤器),并且我已经成功地为我们的深色配色方案选项设计了相当不错的样式,除了左侧的小面板之外。 我终于发现上面的那个是“ToolBar.background”,但我不知道那个叫什么。
帮忙?
我最终通过查看 WindowsPlacesBar 的源代码找到了该属性的名称:
Color bgColor = new Color(UIManager.getColor("ToolBar.shadow").getRGB());
setBackground(bgColor);
我设置了 ToolBar.shadow 但没有任何改变。 进一步的探索最终帮助我意识到 XPStyle.subAppName 属性覆盖了我输入的任何内容。我添加了这段代码:
JFileChooser chooser = new JFileChooser();
setWindowsPlacesBackground( chooser );
private void setWindowsPlacesBackground( Container con ) {
Component[] jc = con.getComponents();
for( int i = 0; i < jc.length; i++ ) {
Component c = jc[i];
if( c instanceof WindowsPlacesBar ) {
((WindowsPlacesBar) c).putClientProperty("XPStyle.subAppName", null);
return;
}
if( c instanceof Container ) {
setWindowsPlacesBackground( (Container)c );
}
}
}
通过取消设置该属性,它可以让我的颜色和方案得以实现。 我仍然觉得应该有一种比遍历容器更干净的方法来取消它,但我找不到它。 WindowsPlacesBar 似乎始终是 FileChooser 中的第一个组件。 我打算再保留一两天,以防其他人可以向我展示一些更“优雅”的东西。
我不知道如何改变它的颜色,但我知道如何摆脱它:
UIManager.put("FileChooser.noPlacesBar", Boolean.TRUE);
或者,如果您确实希望显示面板,那么您可以搜索源代码以查看该面板是如何创建的,以查看是否可以覆盖其默认颜色。
感谢 Morinar 的贡献;这对我有很大帮助。我正在分享对 Morinar 方法的一个小修改,该方法有效。
private void setWindowsPlacesBackground(Container con) {
Component[] components = con.getComponents();
for (int i = 0; i < components.length; i++) {
Component c = components[i];
// If we find a WindowsPlacesBar, we nullify the "XPStyle.subAppName" property
if (c instanceof sun.swing.WindowsPlacesBar) {
System.out.println("Found WindowsPlacesBar, nullifying XPStyle.subAppName property...");
((sun.swing.WindowsPlacesBar) c).putClientProperty("XPStyle.subAppName", null);
// Now you can apply the background color
c.setBackground(new Color(101, 96, 118)); // Change the color here
return; // Exit, as we have modified the first WindowsPlacesBar found
}
// If the component is a container, we continue searching recursively
if (c instanceof Container) {
setWindowsPlacesBackground((Container) c);
}
}
}