⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sampletree.java

📁 java中關於tree的寫法,不同于往常
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2003 Sun Microsystems, Inc. All  Rights Reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *  * -Redistributions of source code must retain the above copyright *  notice, this list of conditions and the following disclaimer. *  * -Redistribution in binary form must reproduct the above copyright *  notice, this list of conditions and the following disclaimer in *  the documentation and/or other materials provided with the distribution. *  * Neither the name of Sun Microsystems, Inc. or the names of contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. *  * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. *  * You acknowledge that Software is not designed, licensed or intended for * use in the design, construction, operation or maintenance of any nuclear * facility. *//* * @(#)SampleTree.java	1.22 03/01/23 */import javax.swing.*;import javax.swing.event.*;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.util.*;import javax.swing.tree.*;/**  * A demo for illustrating how to do different things with JTree.  * The data that this displays is rather boring, that is each node will  * have 7 children that have random names based on the fonts.  Each node  * is then drawn with that font and in a different color.  * While the data isn't interesting the example illustrates a number  * of things:  *  * For an example of dynamicaly loading children refer to DynamicTreeNode.  * For an example of adding/removing/inserting/reloading refer to the inner  *     classes of this class, AddAction, RemovAction, InsertAction and  *     ReloadAction.  * For an example of creating your own cell renderer refer to  *     SampleTreeCellRenderer.  * For an example of subclassing JTreeModel for editing refer to  *     SampleTreeModel.  *  * @version 1.22 01/23/03  * @author Scott Violet  */public class SampleTree{    /** Window for showing Tree. */    protected JFrame            frame;    /** Tree used for the example. */    protected JTree             tree;    /** Tree model. */    protected DefaultTreeModel        treeModel;    /**      * Constructs a new instance of SampleTree.      */    public SampleTree() {	// Force SampleTree to come up in the Cross Platform L&F	try {	    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());	    // If you want the System L&F instead, comment out the above line and	    // uncomment the following:	    // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());	} catch (Exception exc) {	    System.err.println("Error loading L&F: " + exc);	}	JMenuBar         menuBar = constructMenuBar();	JPanel           panel = new JPanel(true);	frame = new JFrame("SampleTree");	frame.getContentPane().add("Center", panel);	frame.setJMenuBar(menuBar);	frame.setBackground(Color.lightGray);	/* Create the JTreeModel. */	DefaultMutableTreeNode root = createNewNode("Root");	treeModel = new SampleTreeModel(root);	/* Create the tree. */	tree = new JTree(treeModel);	/* Enable tool tips for the tree, without this tool tips will not	   be picked up. */	ToolTipManager.sharedInstance().registerComponent(tree);	/* Make the tree use an instance of SampleTreeCellRenderer for	   drawing. */	tree.setCellRenderer(new SampleTreeCellRenderer());	/* Make tree ask for the height of each row. */	tree.setRowHeight(-1);	/* Put the Tree in a scroller. */	JScrollPane        sp = new JScrollPane();	sp.setPreferredSize(new Dimension(300, 300));	sp.getViewport().add(tree);	/* And show it. */	panel.setLayout(new BorderLayout());	panel.add("Center", sp);	panel.add("South", constructOptionsPanel());	frame.addWindowListener( new WindowAdapter() {	    public void windowClosing(WindowEvent e) {System.exit(0);}});	frame.pack();	frame.show();    }    /** Constructs a JPanel containing check boxes for the different      * options that tree supports. */    private JPanel constructOptionsPanel() {	JCheckBox               aCheckbox;	JPanel           retPanel = new JPanel(false);	JPanel           borderPane = new JPanel(false);	borderPane.setLayout(new BorderLayout());	retPanel.setLayout(new FlowLayout());	aCheckbox = new JCheckBox("show handles");	aCheckbox.setSelected(tree.getShowsRootHandles());	aCheckbox.addChangeListener(new ShowHandlesChangeListener());	retPanel.add(aCheckbox);	aCheckbox = new JCheckBox("show root");	aCheckbox.setSelected(tree.isRootVisible());	aCheckbox.addChangeListener(new ShowRootChangeListener());	retPanel.add(aCheckbox);	aCheckbox = new JCheckBox("editable");	aCheckbox.setSelected(tree.isEditable());	aCheckbox.addChangeListener(new TreeEditableChangeListener());	aCheckbox.setToolTipText("Triple click to edit");	retPanel.add(aCheckbox);	borderPane.add(retPanel, BorderLayout.CENTER);	/* Create a set of radio buttons that dictate what selection should	   be allowed in the tree. */	ButtonGroup           group = new ButtonGroup();	JPanel         buttonPane = new JPanel(false);	JRadioButton          button;	buttonPane.setLayout(new FlowLayout());	button = new JRadioButton("Single");	button.addActionListener(new AbstractAction() {	    public boolean isEnabled() { return true; }	    public void actionPerformed(ActionEvent e) {		tree.getSelectionModel().setSelectionMode		    (TreeSelectionModel.SINGLE_TREE_SELECTION);	    }	});	group.add(button);	buttonPane.add(button);	button = new JRadioButton("Contiguous");	button.addActionListener(new AbstractAction() {	    public boolean isEnabled() { return true; }	    public void actionPerformed(ActionEvent e) {		tree.getSelectionModel().setSelectionMode		    (TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);	    }	});	group.add(button);	buttonPane.add(button);	button = new JRadioButton("Discontiguous");	button.addActionListener(new AbstractAction() {	    public boolean isEnabled() { return true; }	    public void actionPerformed(ActionEvent e) {		tree.getSelectionModel().setSelectionMode		    (TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);	    }	});	button.setSelected(true);	group.add(button);	buttonPane.add(button);	borderPane.add(buttonPane, BorderLayout.SOUTH);	// NOTE: This will be enabled in a future release.	// Create a label and combobox to determine how many clicks are	// needed to expand./*	JPanel               clickPanel = new JPanel();	Object[]             values = { "Never", new Integer(1),					new Integer(2), new Integer(3) };	final JComboBox      clickCBox = new JComboBox(values);	clickPanel.setLayout(new FlowLayout());	clickPanel.add(new JLabel("Click count to expand:"));	clickCBox.setSelectedIndex(2);	clickCBox.addActionListener(new ActionListener() {	    public void actionPerformed(ActionEvent ae) {		Object       selItem = clickCBox.getSelectedItem();		if(selItem instanceof Integer)		    tree.setToggleClickCount(((Integer)selItem).intValue());		else // Don't toggle		    tree.setToggleClickCount(0);	    }	});	clickPanel.add(clickCBox);	borderPane.add(clickPanel, BorderLayout.NORTH);*/	return borderPane;    }    /** Construct a menu. */    private JMenuBar constructMenuBar() {	JMenu            menu;	JMenuBar         menuBar = new JMenuBar();	JMenuItem        menuItem;	/* Good ol exit. */	menu = new JMenu("File");	menuBar.add(menu);	menuItem = menu.add(new JMenuItem("Exit"));	menuItem.addActionListener(new ActionListener() {	    public void actionPerformed(ActionEvent e) {		System.exit(0);	    }});	/* Tree related stuff. */	menu = new JMenu("Tree");	menuBar.add(menu);	menuItem = menu.add(new JMenuItem("Add"));	menuItem.addActionListener(new AddAction());	menuItem = menu.add(new JMenuItem("Insert"));	menuItem.addActionListener(new InsertAction());	menuItem = menu.add(new JMenuItem("Reload"));	menuItem.addActionListener(new ReloadAction());	menuItem = menu.add(new JMenuItem("Remove"));	menuItem.addActionListener(new RemoveAction());	return menuBar;    }    /**      * Returns the TreeNode instance that is selected in the tree.      * If nothing is selected, null is returned.      */    protected DefaultMutableTreeNode getSelectedNode() {	TreePath   selPath = tree.getSelectionPath();	if(selPath != null)	    return (DefaultMutableTreeNode)selPath.getLastPathComponent();	return null;    }    /**     * Returns the selected TreePaths in the tree, may return null if     * nothing is selected.     */    protected TreePath[] getSelectedPaths() {        return tree.getSelectionPaths();    }    protected DefaultMutableTreeNode createNewNode(String name) {	return new DynamicTreeNode(new SampleData(null, Color.black, name));    }    /**      * AddAction is used to add a new item after the selected item.      */    class AddAction extends Object implements ActionListener    {	/** Number of nodes that have been added. */	public int               addCount;	/**	  * Messaged when the user clicks on the Add menu item.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -