使用 Numbus L&F 设置 JTree 选择背景
Set JTree selection background using Numbus L&F
如何在使用 Nimbus L&F 时为 JTree
中的选定项目设置背景颜色?
类似问题的旧答案中的建议对 Nimbus 不起作用,所以我尝试了:
final UIDefaults def = new UIDefaults();
def.put( "Tree.background", Color.LIGHT_GRAY );
def.put( "Tree.selectionBackground", Color.RED );
def.put( "Tree.drawHorizontalLines", true );
def.put( "Tree.drawVerticalLines", true );
def.put( "Tree.leftChildIndent", 12 );
def.put( "Tree.rightChildIndent", 12 );
myTree.putClientProperty( "Nimbus.Overrides", def );
myTree.putClientProperty( "Nimbus.Overrides.InheritDefaults", false );
这适用于背景,drawHorizontalLines
、drawVerticalLines
、leftChildIndent
和 rightChildIndent
,但对选择背景没有任何作用。
我也尝试为 "Tree.textForeground"、"Tree.foreground" 和 "Tree.textBackground" 设置各种颜色,但这些都没有效果。
我也试过:
final Painter<JComponent> painter = new Painter<JComponent>() {
@Override
public void paint( final Graphics2D g, final JComponent c, final int w, final int h ) {
g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g.setColor( Color.RED );
g.fillRect( 0, 0, w, h );
}
};
def.put( "Tree:TreeCell[Enabled+Selected].backgroundPainter", painter );
这也没有效果。
Tree:TreeCell[Enabled+Selected].backgroundPainter
在 Windows 上使用 JDK1.8.0_66
对我来说效果很好 10.
import java.awt.*;
import javax.swing.*;
import javax.swing.Painter;
public class TreeCellPainterTest {
public JComponent makeUI() {
//UIDefaults d = new UIDefaults();
UIDefaults d = UIManager.getLookAndFeelDefaults();
d.put("Tree:TreeCell[Enabled+Selected].backgroundPainter", new Painter<JComponent>() {
@Override public void paint(Graphics2D g, JComponent c, int w, int h) {
g.setColor(Color.GREEN);
g.fillRoundRect(0, 0, w - 1, h - 1, 15, 15);
}
});
d.put("Tree:TreeCell[Focused+Selected].backgroundPainter", new Painter<JComponent>() {
@Override public void paint(Graphics2D g, JComponent c, int w, int h) {
g.setColor(Color.RED);
g.fillRect(0, 0, w, h);
}
});
JTree tree = new JTree();
tree.putClientProperty("Nimbus.Overrides", d);
tree.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
JPanel p = new JPanel(new GridLayout(1, 2));
p.add(new JScrollPane(new JTree()));
p.add(new JScrollPane(tree));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new TreeCellPainterTest().makeUI());
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
如何在使用 Nimbus L&F 时为 JTree
中的选定项目设置背景颜色?
类似问题的旧答案中的建议对 Nimbus 不起作用,所以我尝试了:
final UIDefaults def = new UIDefaults();
def.put( "Tree.background", Color.LIGHT_GRAY );
def.put( "Tree.selectionBackground", Color.RED );
def.put( "Tree.drawHorizontalLines", true );
def.put( "Tree.drawVerticalLines", true );
def.put( "Tree.leftChildIndent", 12 );
def.put( "Tree.rightChildIndent", 12 );
myTree.putClientProperty( "Nimbus.Overrides", def );
myTree.putClientProperty( "Nimbus.Overrides.InheritDefaults", false );
这适用于背景,drawHorizontalLines
、drawVerticalLines
、leftChildIndent
和 rightChildIndent
,但对选择背景没有任何作用。
我也尝试为 "Tree.textForeground"、"Tree.foreground" 和 "Tree.textBackground" 设置各种颜色,但这些都没有效果。
我也试过:
final Painter<JComponent> painter = new Painter<JComponent>() {
@Override
public void paint( final Graphics2D g, final JComponent c, final int w, final int h ) {
g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g.setColor( Color.RED );
g.fillRect( 0, 0, w, h );
}
};
def.put( "Tree:TreeCell[Enabled+Selected].backgroundPainter", painter );
这也没有效果。
Tree:TreeCell[Enabled+Selected].backgroundPainter
在 Windows 上使用 JDK1.8.0_66
对我来说效果很好 10.
import java.awt.*;
import javax.swing.*;
import javax.swing.Painter;
public class TreeCellPainterTest {
public JComponent makeUI() {
//UIDefaults d = new UIDefaults();
UIDefaults d = UIManager.getLookAndFeelDefaults();
d.put("Tree:TreeCell[Enabled+Selected].backgroundPainter", new Painter<JComponent>() {
@Override public void paint(Graphics2D g, JComponent c, int w, int h) {
g.setColor(Color.GREEN);
g.fillRoundRect(0, 0, w - 1, h - 1, 15, 15);
}
});
d.put("Tree:TreeCell[Focused+Selected].backgroundPainter", new Painter<JComponent>() {
@Override public void paint(Graphics2D g, JComponent c, int w, int h) {
g.setColor(Color.RED);
g.fillRect(0, 0, w, h);
}
});
JTree tree = new JTree();
tree.putClientProperty("Nimbus.Overrides", d);
tree.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
JPanel p = new JPanel(new GridLayout(1, 2));
p.add(new JScrollPane(new JTree()));
p.add(new JScrollPane(tree));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new TreeCellPainterTest().makeUI());
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}