0

出于某种奇怪的原因,当我JTree在 java 中实现 a 时,我在 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;
    }
}
4

1 回答 1

1

这是一个完整的例子。请注意,这LEG_LINE_STYLE_STRING = "Angled"是默认设置。

附录:此处getTreeCellRendererComponent()显示了一些其他示例;你可能需要调整。setRowHeight()

图片

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();
            }
        });
    }
}
于 2013-04-06T10:42:31.843 回答