JAVA怎么设置树状节点,使之在点击各个节点时,在标签上显示相应图片呢?

2024-11-15 14:34:46
推荐回答(3个)
回答(1):

点击节点的时候有事件触发的,可以得到节点本身信息。
接下来是使用Ajax

回答(2):


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;

/**
 *
 * @author beans
 */
public class TreeMain {

    public static void main(String[] args) {
        new TreeMain().showDialog();
    }

    public TreeMain() {
    }

    /**
     * 显示窗口
     */
    private void showDialog() {
        JDialog dialog = new JDialog();

        dialog.setBounds(new Rectangle(50, 50, 380, 280));
        dialog.setTitle("演示树");

        dialog.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                dialog.setVisible(false);
                dialog.dispose();
            }
        });

        dialog.add(this.getPanel(), BorderLayout.CENTER);
        dialog.setVisible(true);
    }

    private JPanel getPanel() {
        JPanel panel = new JPanel();
        JScrollPane treePanel = new JScrollPane();
        treePanel.setViewportView(this.getTree());
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(panel);
        panel.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(treePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(treePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(18, Short.MAX_VALUE))
        );
        panel.add(treePanel, BorderLayout.CENTER);
        return panel;
    }

    /**
     * 取得树。
     * @return 
     */
    private JTree getTree() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode();
        root.add(this.getNode());
        root.add(this.getNode());
        root.add(this.getNode());
        JTree jtree =  new JTree(root);
        jtree.setRootVisible(false);
        jtree.setCellRenderer(new CTreeCellRenderer());
        jtree.expandRow(1);
        return jtree;
    }
    
    /**
     * 取得树节点。
     * @return 
     */
    private DefaultMutableTreeNode getNode() {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(new NodeObject(true, "节"));
        for(int i = 0; i < 5; i++) {
            DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(new NodeObject(false, "叶" + i));
        node.add(leaf);
        }
        return node;
    }

    /**
     * 树节点和树叶,关联对象。
     */
    class NodeObject {

        boolean isNode;
        String name;

        /**
         *
         * @param isNode the value of isNode
         * @param name the value of name
         */
        NodeObject(boolean isNode, String name) {
            this.isNode = isNode;
            this.name = name;
        }

        /**
         * 图标
         * @param isSelect 选中节点时返回不同的图标。
         * @return 
         */
        ImageIcon getIcon(boolean isSelect) {
            int wh = 20;
            BufferedImage image = new BufferedImage(wh, wh, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = image.createGraphics();

            image = g2.getDeviceConfiguration().createCompatibleImage(wh, wh, Transparency.TRANSLUCENT);
            Graphics2D g2d = image.createGraphics();
            Font font = new Font("Dialog", Font.PLAIN, wh - 4);
            g2d.setFont(font);
            g2d.setBackground(Color.WHITE);
            g2d.setColor(Color.BLACK);
            g2d.drawString(isSelect ? " S " : " N ", 0, wh - 1);
            g2d.setColor(this.isNode ? Color.RED : Color.YELLOW);
            g2d.drawLine(0, 5, wh, 5);
            g2d.drawLine(0, 10, wh, 10);
            g2d.drawLine(0, 15, wh, 15);

            g2d.dispose();
            g2.dispose();
            return new ImageIcon(image);
        }

        String getName() {
            return this.name;
        }
    }

    /**
     * 树渲染器
     */
    protected class CTreeCellRenderer extends JLabel implements TreeCellRenderer {

        protected Color m_textSelectionColor;
        protected Color m_textNonSelectionColor;
        protected Color m_bkSelectionColor;
        protected Color m_bkNonSelectionColor;
        protected Color m_borderSelectionColor;
        protected boolean m_selected;

        public CTreeCellRenderer() {
            m_textSelectionColor = UIManager.getColor("Tree.selectionForeground");
            m_textNonSelectionColor = UIManager.getColor("Tree.textForeground");
            m_bkSelectionColor = UIManager.getColor("Tree.selectionBackground");
            m_bkNonSelectionColor = UIManager.getColor("Tree.textBackground");
            m_borderSelectionColor = UIManager.getColor("Tree.selectionBorderColor");
        }

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value,
                boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            NodeObject obj = (NodeObject) node.getUserObject();
            this.setIcon(obj.getIcon(selected));
            this.setText("  " + obj.getName() + " ");
            this.setForeground(selected ? m_textSelectionColor : m_textNonSelectionColor);
            this.setBackground(selected ? m_bkSelectionColor : m_bkNonSelectionColor);
            this.m_selected = selected;
            return this;
        }

        @Override
        public void paint(Graphics g) {
            Color bColor = this.getBackground();
            Icon icon = this.getIcon();

            g.setColor(bColor);
            int offset = 0;
            if (icon != null && getText() != null) {
                offset = (icon.getIconWidth() + this.getIconTextGap());
            }
            g.fillRect(offset, 0, this.getWidth() - 1 - offset, this.getHeight() - 1);

            if (this.m_selected) {
                g.setColor(this.m_borderSelectionColor);
                g.drawRect(offset, 0, this.getWidth() - 1 - offset, this.getHeight() - 1);
            }
            super.paint(g);
        }
    }

}

回答(3):