gpt4 book ai didi

java - JTree 实现

转载 作者:行者123 更新时间:2023-12-01 14:42:26 31 4
gpt4 key购买 nike

出于某种奇怪的原因,当我实现 JTree 时在java中我在Mac OSX上看不到扩展线(有角度的);但是当树展开以显示所有节点时,我在 Windows 上看到它们。我已经尝试过tree.putClientProperty("JTree.lineStyle", "Angled") ,但我什么也没得到。

第二个问题,再次在 OSX 上:我有一张根的图片,也没有完全显示一些东西是从顶部和底部切掉的。

DefaultMutableTreeNode root = new DefaultMutableTreeNode(userId + " Album List");
albTree = new JTree(root);
albTree.setCellRenderer(new IconRenderer());
albTree.putClientProperty("JTree.lineStyle", "Angled");
albTree.setFont(new Font("Segoe UI", Font.PLAIN, 20));
albTreeModel = (DefaultTreeModel) albTree.getModel(); albTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);


class IconRenderer extends DefaultTreeCellRenderer {

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {

// start with default behavior
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

// customize based on local conditions/state
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;

DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();

Icon openIcon = renderer.getDefaultOpenIcon();
Icon rootIcon = new ImageIcon("images/user-1.png");

if (node == nonAdminFrame.root) {

setIcon(rootIcon);

}

else if (nonAdminFrame.root.isNodeChild(node)) {

setIcon(openIcon);
}

return this;
}
}

最佳答案

这是一个完整的示例。请注意,LEG_LINE_STYLE_STRING = "Angled" 是默认值。

附录:显示了一些额外的 getTreeCellRendererComponent() 示例 here ;您可能需要调整 setRowHeight()

image

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.UIManager;

/**
* @see
*/
public class Test {

private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree tree = new JTree();
tree.putClientProperty("JTree.lineStyle", "Angled");
//tree.putClientProperty("JTree.lineStyle", "Horizontal");
//tree.putClientProperty("JTree.lineStyle", "None");
for (int i = 0; i < tree.getRowCount(); i++) {
tree.expandRow(i);
}
f.add(tree);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test().display();
}
});
}
}

关于java - JTree 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15817128/

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