gpt4 book ai didi

Java:带有用于展开和折叠的加/减图标的 JTree?

转载 作者:行者123 更新时间:2023-11-29 09:39:58 25 4
gpt4 key购买 nike

如何使我的 Jtree 看起来像下面的东西,带有允许展开和折叠的加号和减号图标?

目前,默认的 JTree 仅在您双击时展开和折叠。我想覆盖此双击以获得另一个功能,并让用户通过单击减号和加号图标来展开/折叠树,如下所示。

enter image description here

最佳答案

您必须更改 L&F 的 Tree.collapsedIconTree.expandedIcon 属性并提供您自己的图标:

UIManager.put("Tree.collapsedIcon", new IconUIResource(new NodeIcon('+')));
UIManager.put("Tree.expandedIcon", new IconUIResource(new NodeIcon('-')));

这是我使用的图标,它是内​​部带有 +/- 的简单正方形:

public class NodeIcon implements Icon {

private static final int SIZE = 9;

private char type;

public NodeIcon(char type) {
this.type = type;
}

public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(UIManager.getColor("Tree.background"));
g.fillRect(x, y, SIZE - 1, SIZE - 1);

g.setColor(UIManager.getColor("Tree.hash").darker());
g.drawRect(x, y, SIZE - 1, SIZE - 1);

g.setColor(UIManager.getColor("Tree.foreground"));
g.drawLine(x + 2, y + SIZE / 2, x + SIZE - 3, y + SIZE / 2);
if (type == '+') {
g.drawLine(x + SIZE / 2, y + 2, x + SIZE / 2, y + SIZE - 3);
}
}

public int getIconWidth() {
return SIZE;
}

public int getIconHeight() {
return SIZE;
}
}

关于Java:带有用于展开和折叠的加/减图标的 JTree?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6590462/

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