1

我最近一直在尝试将系统转换为统一的外观和感觉(在本例中为 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 为真,当它为假时,行突出显示被关闭。

我所追求的是对行高亮进行简单的重新着色。

提前致谢

编辑:

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 组件的选定行上的默认文本颜色,但不会。

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

4

1 回答 1

2

查看Nimbus 默认值(Java™ 教程 > 使用 JFC/Swing 创建 GUI > 修改外观)

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();
      }
    });
  }
}
于 2015-02-24T12:49:23.623 回答