gpt4 book ai didi

java - JTree 忽略 LaF 覆盖

转载 作者:行者123 更新时间:2023-11-30 03:32:29 25 4
gpt4 key购买 nike

我最近一直在尝试将系统转换为统一的外观和感觉(在本例中为 Nimbus)。然而,某些组件需要更改默认值,我不想仅仅因为单个组件而更改默认值。

我正在尝试覆盖 JTree 组件的行突出显示颜色。我已经设法完全关闭它,但这不是我想要的。

通过阅读这里的各种问题,我尝试使用:

UIDefaults overrides = new UIDefaults();
overrides.put("Tree.selectionBackground", new Color(200,200,200));
overrides.put("nimbusSelectionBackground", new Color(200,200,200));

modelTree = new ModelTree(treeModel);

modelTree.putClientProperty("Nimbus.Overrides", overrides);
modelTree.putClientProperty("Nimbus.Overrides.InheritDefaults",true);

其中 modelTree 扩展了标准 JTree。如果没有这些覆盖,外观完全是默认的,并且我知道派生类中没有任何内容覆盖这些属性。

问题是属性被忽略,当 InheritDefaults 为 true 时颜色保持与默认值相同,并且当 InheritDefaults 为 false 时行突出显示被关闭。

我想要的是行突出显示的简单重新着色。

提前致谢

编辑:

public static class treeTest extends JPanel {

public treeTest() {
super();

try {
UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName());
} catch (Exception e) {
e.printStackTrace();
}

//UIManager.put("Tree.selectionForeground", Color.BLACK);

UIDefaults overrides = new UIDefaults();

overrides.put("Tree.selectionForeground", Color.BLACK);

JTree defaultsTree = new JTree();
JTree overiddenTree = new JTree();

overiddenTree.putClientProperty("Nimbus.Overrides", overrides);
overiddenTree.putClientProperty("Nimbus.Overrides.InheritDefaults", false);

add(defaultsTree);
add(overiddenTree);
}
}

我希望这是一个可以接受的SSCCE(这是我第一次制作)。应该覆盖 overiddenTree 组件所选行上的默认文本颜色,但没有。

取消注释覆盖默认值以执行相同操作的行确实有效,但将为所有实例设置。

最佳答案

看看Nimbus Defaults (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.plaf.nimbus.*;

public class TreeCellBackgroundPainterTest {
public JComponent makeUI() {
UIDefaults d = new UIDefaults();
AbstractRegionPainter rp = new AbstractRegionPainter() {
@Override protected void doPaint(
Graphics2D g, JComponent c, int width, int height, Object[] extendedCacheKeys) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, width, height);
}
@Override protected final PaintContext getPaintContext() {
return null;
}
};
d.put("Tree:TreeCell[Enabled+Selected].backgroundPainter", rp);
d.put("Tree:TreeCell[Focused+Selected].backgroundPainter", rp);

JTree tree = new JTree();
tree.putClientProperty("Nimbus.Overrides", d);
tree.putClientProperty("Nimbus.Overrides.InheritDefaults", true);

JPanel p = new JPanel(new GridLayout(1, 2, 2, 2));
p.add(new JScrollPane(new JTree()));
p.add(new JScrollPane(tree));
return p;
}
public static void createAndShowGUI() {
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
}
}
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame f = new JFrame();
f.getContentPane().add(new TreeCellBackgroundPainterTest().makeUI());
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String... args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
}

关于java - JTree 忽略 LaF 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28691272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com