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

📄 simpletree.java

📁 java编程开发技巧与实例的编译测试通过的所有例程
💻 JAVA
字号:
import java.util.Enumeration;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class SimpleTree extends JFrame implements ActionListener
{
	private DefaultMutableTreeNode	root = new DefaultMutableTreeNode("Web sites");
	private JTree tree		=	new JTree(root);
	private JButton expand	=	new JButton("Expand");
	private JButton collapse	=	new JButton("Collapse");
	private JButton quit	=	new JButton("Quit");
	private class WindowCloser extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{	System.exit(0);	}
	}
	private SimpleTree()
	{
		super("A Simple Tree");
		createTreeStructure();
		JPanel buttons	=	new JPanel(new FlowLayout());
		buttons.add(collapse);
		collapse.addActionListener(this);
		buttons.add(expand);
		expand.addActionListener(this);
		buttons.add(quit);
		quit.addActionListener(this);
		getContentPane().add("Center", new JScrollPane(tree));
		getContentPane().add("South", buttons);
		addWindowListener(new WindowCloser());
		validate();
		pack();
		setLocationRelativeTo(this);
		setVisible(true);
	}
	private void createTreeStructure()
	{
		DefaultMutableTreeNode edu	=	new DefaultMutableTreeNode("Universities");
		DefaultMutableTreeNode com	=	new DefaultMutableTreeNode("Search Engines");
		root.add(edu);
		root.add(com);
		edu.add(new DefaultMutableTreeNode("www.njust.edu"));
		edu.add(new DefaultMutableTreeNode("www.tsinghua.edu"));
		com.add(new DefaultMutableTreeNode("www.baidu.com"));
		com.add(new DefaultMutableTreeNode("www.sohu.com"));
	}
	private void collapseTree()
	{
		for (Enumeration nodes = root.depthFirstEnumeration(); nodes.hasMoreElements();)
		{
			DefaultMutableTreeNode node	=	(DefaultMutableTreeNode)nodes.nextElement();
			tree.collapsePath(new TreePath(node.getPath()));
		}
	}
	private void expandTree()
	{
		for (Enumeration nodes = root.breadthFirstEnumeration(); nodes.hasMoreElements();)
		{
			DefaultMutableTreeNode node	=	(DefaultMutableTreeNode)nodes.nextElement();
			tree.expandPath(new TreePath(node.getPath()));
		}
			
	}
	public void actionPerformed(ActionEvent ae)
	{
		if (ae.getSource() == expand)
			expandTree();
		else if (ae.getSource() == collapse)
			collapseTree();
		else if (ae.getSource() == quit)
			System.exit(0);
	}
	public static void main(String args[])
	{
		SimpleTree st	=	new SimpleTree();
	}
}

⌨️ 快捷键说明

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