gpt4 book ai didi

java - 为我的 Tree CellRenderer 设置背景颜色时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:55:12 25 4
gpt4 key购买 nike

嗨,我已经创建了自己的渲染器。我希望背景应该是蓝色的。我也将背景颜色设置为蓝色。但我不知道我的渲染器的背景颜色似乎总是白色是什么问题。

我已经发布了代码。请帮助我错误的地方,使背景颜色变成白色。

class CheckTreeCellRenderer extends JPanel implements TreeCellRenderer {

private CheckTreeSelectionModel selectionModel;
private MyRenderer delegate;
private TristateCheckBox checkBox = new TristateCheckBox("", null, true);
public static final State NOT_SELECTED = new State();
public static final State SELECTED = new State();
public static final State DONT_CARE = new State();

public CheckTreeCellRenderer(MyRenderer delegate, CheckTreeSelectionModel selectionModel) {
this.delegate = delegate;

this.selectionModel = selectionModel;
setLayout(new BorderLayout());
setOpaque(true);
setBackground(new Color(207, 219, 234));
checkBox.setState(Boolean.TRUE);

checkBox.setOpaque(true);
checkBox.setBackground(new Color(207, 219, 234));
}

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);


TreePath path = tree.getPathForRow(row);

if (path != null) {
if (selectionModel.isPathSelected(path, true)) {
checkBox.setState(Boolean.TRUE);
} else {
checkBox.setState(selectionModel.isPartiallySelected(path) ? null : Boolean.FALSE);
}
}

renderer.setBackground(new Color(207, 219, 234));
tree.setOpaque(true);
tree.setBackground(new Color(207, 219, 234));
this.setOpaque(true);
this.setBackground(new Color(207, 219, 234));

add(checkBox, BorderLayout.WEST);
add(renderer, BorderLayout.CENTER);

return this;
}

}

最佳答案

如果不看到其余代码,很难判断。

我猜代理的渲染器很可能包含具有白色背景的不透明组件。该代码仅将渲染器设置为蓝色,渲染器包含的组件(如果有)不会通过上述代码进行调整。

关于java - 为我的 Tree CellRenderer 设置背景颜色时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2447773/

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