to xmpp:
我找了一下,没有Tree这个类。

解决方案 »

  1.   

    应该可以,只不过是幅图片吗,呵呵;)记的有个setUI什么的可以用吧,你去看看jTree的源码不就清楚了吗;)HOHO~~~~~~~~
      

  2.   

    JTree jt = new JTree();
    jt.setCellRenderer(new MyCellRenderer());//MyCellRenderer
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import javax.swing.plaf.ColorUIResource;public class MyCellRenderer extends JPanel implements TreeCellRenderer {
        //protected JCheckBox check;
        protected TreeLabel label;
        ImageIcon icon1 = new ImageIcon(
                PcedMainFrame.class.getResource("whitepoint.gif"));
        ImageIcon icon2 = new ImageIcon(
                PcedMainFrame.class.getResource("blackpoint.gif"));    public MyCellRenderer() {
            setLayout(null);
            add(label = new TreeLabel());
            label.setForeground(UIManager.getColor("Tree.textForeground"));
        }    public Component getTreeCellRendererComponent(JTree tree, Object value,
                boolean isSelected, boolean expanded,
                boolean leaf, int row, boolean hasFocus) {
            String  stringValue = tree.convertValueToText(value, isSelected,
                    expanded, leaf, row, hasFocus);
            setEnabled(tree.isEnabled());
            label.setFont(tree.getFont());
            label.setText(stringValue);
            label.setSelected(isSelected);
            label.setFocus(hasFocus);
            if(isSelected){
                label.setIcon(icon2);
            }else{
                label.setIcon(icon1);
            }
            return this;
        }    public Dimension getPreferredSize() {
            Dimension d_label = label.getPreferredSize();
            return new Dimension(d_label.width, d_label.height);
        }    public void doLayout() {
            Dimension d_label = label.getPreferredSize();
            int y_check = 0;
            int y_label = 0;
            y_label = (d_label.height)/2;
            label.setLocation(0,y_label);
            label.setBounds(0,0,d_label.width,d_label.height);
        }    public void setBackground(Color color) {
            if (color instanceof ColorUIResource)
                color = null;
            super.setBackground(color);
        }    public class TreeLabel extends JLabel {
            boolean isSelected;
            boolean hasFocus;        public TreeLabel() {
            }        public void setBackground(Color color) {
                if(color instanceof ColorUIResource)
                    color = null;
                super.setBackground(color);
            }        public void paint(Graphics g) {
                String str;
                if ((str = getText()) != null) {
                    if (0 < str.length()) {
                        if (isSelected) {
                            label.setForeground(Color.white);
                            g.setColor(Color.BLUE);
                        } else {
                            label.setForeground(Color.black);
                            g.setColor(UIManager.getColor("Tree.textBackground"));
                        }
                        Dimension d = getPreferredSize();
                        int imageOffset = 0;
                        Icon currentI = getIcon();
                        if (currentI != null) {
                            imageOffset = currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1);
                        }
                        g.fillRect(imageOffset, 0, d.width -1 - imageOffset, d.height);
                        if (hasFocus) {
                            g.setColor(UIManager.getColor("Tree.selectionBorderColor"));
                            g.drawRect(imageOffset, 0, d.width -1 - imageOffset, d.height -1);
                        }
                    }
                }
                super.paint(g);
            }        public Dimension getPreferredSize() {
                Dimension retDimension = super.getPreferredSize();
                if (retDimension != null) {
                    retDimension = new Dimension(retDimension.width + 3,
                            retDimension.height);
                }
                return retDimension;
            }        public void setSelected(boolean isSelected) {
                this.isSelected = isSelected;
            }        public void setFocus(boolean hasFocus) {
                this.hasFocus = hasFocus;
            }
        }
    }
      

  3.   

    to windloft:
    谢谢windloft的关注,但是还没解决该问题。
    因为该问题不是要更换节点左边的图标,而是要更换节点左边图标再左边的那个图标(呵呵,比较绕口)。
    比如在windows资源管理器中,闭合的文件夹图标左边还有一个“+”的图标;
    而打开的文件夹图标左边还有一个“-”的图标。
    现在问题是要在Metal风格中也显示这样的图标。
      

  4.   

    JTree的结构是这样的,由TreeUI负责你所需要的部分,而由TreeCellRenderer负责绘制每个节点,比如BasicTreeUI有这样一组API
    protected  void paintExpandControl(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) 
    protected  void paintHorizontalLine(Graphics g, JComponent c, int y, int left, int right) 
    protected  void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) 
    protected  void paintRow(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) 
    protected  void paintVerticalLine(Graphics g, JComponent c, int x, int top, int bottom) 
    protected  void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, TreePath path) 覆盖他,应该可以搞定
      

  5.   

    JTree的结构是这样的,由TreeUI负责你所需要的部分,而由TreeCellRenderer负责绘制每个节点,比如BasicTreeUI有这样一组API
    protected  void paintExpandControl(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) 
    protected  void paintHorizontalLine(Graphics g, JComponent c, int y, int left, int right) 
    protected  void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) 
    protected  void paintRow(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) 
    protected  void paintVerticalLine(Graphics g, JComponent c, int x, int top, int bottom) 
    protected  void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, TreePath path) 覆盖他,应该可以搞定
      

  6.   

    JTree的结构是这样的,由TreeUI负责你所需要的部分,而由TreeCellRenderer负责绘制每个节点,比如BasicTreeUI有这样一组API
    protected  void paintExpandControl(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) 
    protected  void paintHorizontalLine(Graphics g, JComponent c, int y, int left, int right) 
    protected  void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) 
    protected  void paintRow(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) 
    protected  void paintVerticalLine(Graphics g, JComponent c, int x, int top, int bottom) 
    protected  void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, TreePath path) 覆盖他,应该可以搞定