• JTree


     http://www.easyicon.net/

    package swing.tree;
    
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.lang.reflect.Field;
    import java.util.Enumeration;
    
    import javax.lang.model.element.Modifier;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.JTree;
    import javax.swing.SwingUtilities;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreeNode;
    import javax.swing.tree.TreePath;
    import javax.swing.tree.TreeSelectionModel;
    
    /*2015-7-11*/
    public class ClassTree {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    ClassTreeFrame frame = new ClassTreeFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
                }
            });
    
        }
    
    }
    
    class ClassTreeFrame extends JFrame {
        private static final long serialVersionUID = 3866055150465649395L;
    
        private DefaultMutableTreeNode root;
        private DefaultTreeModel model;
        private JTree tree;
        private JTextField textField;
        private JTextArea textArea;
        private static final int DEFAULT_WIDTH = 800;
        private static final int DEFAULT_HEIGHT = 600;
    
        public ClassTreeFrame() {
            setTitle(getClass().getSimpleName());
            setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
            root = new DefaultMutableTreeNode(java.lang.Object.class);
            model = new DefaultTreeModel(root);
            tree = new JTree(model);
    
            addClass(getClass());
    
            ClassNameTreeCellRender render = new ClassNameTreeCellRender();
            String filename = getClass().getResource("/swing/tree/red-ball.gif").getPath();
            render.setClosedIcon(new ImageIcon(filename));
            filename = getClass().getResource("/swing/tree/yellow-ball.gif").getPath();
            render.setOpenIcon(new ImageIcon(filename));
            filename = getClass().getResource("/swing/tree/blue-ball.gif").getPath();
            render.setLeafIcon(new ImageIcon(filename));
            tree.setCellRenderer(render);
    
            tree.addTreeSelectionListener(new TreeSelectionListener() {
    
                @Override
                public void valueChanged(TreeSelectionEvent e) {
    
                    TreePath path = tree.getSelectionPath();
                    if (path == null) {
                        return;
                    }
                    DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path.getLastPathComponent();
                    Class<?> c = (Class<?>) selectedNode.getUserObject();
                    String description = getFileDescription(c);
                    textArea.setText(description);
                }
            });
    
            int mode = TreeSelectionModel.SINGLE_TREE_SELECTION;
            tree.getSelectionModel().setSelectionMode(mode);
    
            textArea = new JTextArea();
    
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(1, 2));
            panel.add(new JScrollPane(tree));
            panel.add(new JScrollPane(textArea));
    
            add(panel, BorderLayout.CENTER);
    
            addTextField();
    
        }
    
        private void addTextField() {
            JPanel panel = new JPanel();
            ActionListener addListener = new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        String text = textField.getText();
                        addClass(Class.forName(text));
                        textField.setText("");
                    } catch (ClassNotFoundException e1) {
                        JOptionPane.showMessageDialog(ClassTreeFrame.this, e1.getMessage());
                    }
                }
            };
    
            textField = new JTextField(20);
            textField.addActionListener(addListener);
            panel.add(textField);
    
            JButton addButton = new JButton("Add");
            addButton.addActionListener(addListener);
            panel.add(addButton);
            add(panel, BorderLayout.SOUTH);
    
        }
    
        protected String getFileDescription(Class<?> c) {
            StringBuilder r = new StringBuilder();
            Field[] fields = c.getDeclaredFields();
            for (Field field : fields) {
                if (field.getModifiers() == Modifier.STATIC.ordinal()) {
                    r.append("static");
                }
                r.append(field.getType().getName())
                        .append("")
                        .append(field.getName())
                        .append("
    ");
    
            }
    
            return r.toString();
        }
    
        private DefaultMutableTreeNode addClass(Class<?> c) {
            if (c.isInterface() || c.isPrimitive()) {
                return null;
            }
    
            DefaultMutableTreeNode node = findUserObject(c);
            if (node != null) {
                return node;
            }
    
            Class<?> s = c.getSuperclass();
            DefaultMutableTreeNode parent;
            if (s == null) {
                parent = root;
            } else {
                parent = addClass(s);
            }
    
            DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(c);
            model.insertNodeInto(newChild, parent, parent.getChildCount());
    
            TreePath path = new TreePath(model.getPathToRoot(newChild));
            tree.makeVisible(path);
            return newChild;
        }
    
        private DefaultMutableTreeNode findUserObject(Object obj) {
            @SuppressWarnings("unchecked")
            Enumeration<TreeNode> e = root.breadthFirstEnumeration();
            while (e.hasMoreElements()) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
                if (node.getUserObject().equals(obj)) {
                    return node;
                }
            }
            return null;
        }
    
    }
    
    class ClassNameTreeCellRender extends DefaultTreeCellRenderer {
        private static final long serialVersionUID = -2145290206168596413L;
        private Font plainFont;
        private Font italicFont;
    
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel,
                boolean expanded, boolean leaf, int row, boolean hasFocus) {
    
            Component comp = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
    
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            Class<?> c = (Class<?>) node.getUserObject();
    
            if (plainFont == null) {
                plainFont = getFont();
                if (plainFont == null) {
                    italicFont = plainFont.deriveFont(Font.ITALIC);
                }
            }
    
            if (c.getModifiers() == Modifier.ABSTRACT.ordinal()) {
                setFont(plainFont);
            } else {
                comp.setFont(italicFont);
            }
    
            return comp;
        }
    
    }
  • 相关阅读:
    rust 实战 实现一个线程工作池 ThreadPool
    rust Cell 与 RefCell的区别
    使用pre标签显示原始文本并自动换行
    rust实战 newtype模式
    什么是幻读,怎么解决幻读
    rust match 模式匹配摘录
    Elasticsearch 8的版本来了;可以直接在 Elasticsearch 中使用 PyTorch Machine Learning 模型
    神奇的库 phone
    python中单例的实现
    斐波那契查找
  • 原文地址:https://www.cnblogs.com/softidea/p/4639537.html
Copyright © 2020-2023  润新知