树的可见区域的获取和滚动条监听
来源:互联网 发布:天谕捏脸数据 编辑:程序博客网 时间:2024/06/05 15:27
1.树的前序,中序,后序遍历
2.树所在面板的滚动条监听
3.树的可见区域的获取
package treeTest;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.AdjustmentEvent;import java.awt.event.AdjustmentListener;import java.util.Enumeration;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollBar;import javax.swing.JScrollPane;import javax.swing.JTree;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeModel;import javax.swing.tree.TreePath;public class Treetest extends JFrame { private DefaultMutableTreeNode root; // 定义根节点. JTree tree; // 主方法. public static void main(String[] args) { // 实例化本类. Treetest frame = new Treetest(); frame.setVisible(true); } // 构造方法. public Treetest() { super(); // 继承父类. setTitle("遍历节点方法"); // 设置窗口标题. setBounds(100, 100, 300, 300); // 设置绝对位置. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭方法. root = new DefaultMutableTreeNode("ROOT"); // 设置根节点名. DefaultMutableTreeNode noteA = new DefaultMutableTreeNode("FirstA"); // 定义及设置第一个子节点. root.add(noteA); // 将第一节点添加到树根上. // 给noteA增加两个子节点. noteA.add(new DefaultMutableTreeNode("SecondAA")); noteA.add(new DefaultMutableTreeNode("SecondAB")); DefaultMutableTreeNode noteB = new DefaultMutableTreeNode("FirstB");// 定义及设置第二个子节点. root.add(noteB); DefaultMutableTreeNode noteC = new DefaultMutableTreeNode("FirstC");// 定义及设置第三个子节点. root.add(noteC); noteC.add(new DefaultMutableTreeNode("SecondCA"));// 为noteC增加子节点. DefaultMutableTreeNode noteSecondCB = new DefaultMutableTreeNode( "SecondCB");// 为noteC增加子节点. noteC.add(noteSecondCB);// 将noteSecondCB添加到noteC子节点下. // 给noteSecondCB增加子节点. noteSecondCB.add(new DefaultMutableTreeNode("SecondCBA")); noteSecondCB.add(new DefaultMutableTreeNode("SecondCBB")); // 给noteC增加SecondCC子节点. noteC.add(new DefaultMutableTreeNode("SecondCC")); DefaultMutableTreeNode noteD = new DefaultMutableTreeNode("FirstD");// 定义及设置第三个子节点. root.add(noteD); DefaultMutableTreeNode noteE = new DefaultMutableTreeNode("FirstE");// 定义及设置第三个子节点. root.add(noteE); DefaultMutableTreeNode noteF = new DefaultMutableTreeNode("FirstF");// 定义及设置第三个子节点. root.add(noteF); DefaultMutableTreeNode noteG = new DefaultMutableTreeNode("FirstG");// 定义及设置第三个子节点. root.add(noteG); DefaultMutableTreeNode noteH = new DefaultMutableTreeNode("FirstH");// 定义及设置第三个子节点. root.add(noteH); DefaultMutableTreeNode noteI = new DefaultMutableTreeNode("FirstI");// 定义及设置第三个子节点. root.add(noteI); tree = new JTree(root);// 根据根节点创建树. JScrollPane jscrcoll = new JScrollPane(tree); getContentPane().add(jscrcoll, BorderLayout.CENTER); JScrollBar jscrollBar = jscrcoll.getVerticalScrollBar(); //监听滚动条 jscrollBar.addAdjustmentListener(new AdjustmentListener() { @Override public void adjustmentValueChanged(AdjustmentEvent e) { // 用户一直拖动时,返回true,用户停止拖动时,返回false if(e.getValueIsAdjusting()){ return; } } }); createOperationPanel(); } private void createOperationPanel() { // 开始操作节点. // 定义Panel. final JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); // 设置panel为网格布局. getContentPane().add(panel, BorderLayout.EAST); // 将panel放置到窗口右边. final JButton button_01 = new JButton("按前序遍历节点"); // 定义及设置第一个按钮. button_01.addActionListener(new ButtonActionListener("按前序遍历节点")); panel.add(button_01); // 将按钮放置到panel里. final JButton button_02 = new JButton("按后序遍历节点"); // 定义及设置第二个按钮. button_02.addActionListener(new ButtonActionListener("按后序遍历节点")); panel.add(button_02); // 将按钮放置到panel里. final JButton button_03 = new JButton("以广度遍历节点"); // 定义及设置第三个按钮. button_03.addActionListener(new ButtonActionListener("以广度遍历节点")); panel.add(button_03); // 将按钮放置到panel里. final JButton button_04 = new JButton("以深度遍历节点"); // 定义及设置第四个按钮. button_04.addActionListener(new ButtonActionListener("以深度遍历节点")); panel.add(button_04); // 将按钮放置到panel里. final JButton button_05 = new JButton("遍历直属子节点"); // 定义及设置第五个按钮. button_05.addActionListener(new ButtonActionListener("遍历直属子节点")); panel.add(button_05); // 将按钮放置到panel里. } // 定义按钮点击事件. private class ButtonActionListener implements ActionListener { private String mode;// 定义mode变量. // 构造方法. public ButtonActionListener(String mode) { this.mode = mode; } // 定义激活方法. public void actionPerformed(ActionEvent e) { // 声明节点枚举对象. Enumeration<?> enumeration; if (mode.equals("按前序遍历节点")) { // 按前序遍历根节点. enumeration = root.preorderEnumeration(); } else if (mode.equals("按后序遍历节点")) { enumeration = root.postorderEnumeration(); } else if (mode.equals("以广度遍历节点")) { enumeration = root.breadthFirstEnumeration(); } else if (mode.equals("以深度遍历节点")) { enumeration = root.depthFirstEnumeration(); } else { enumeration = root.children();// 遍历该节点的所有子节点. } printTreeVisibleRect(); // 知道了遍历方式,开始遍历. while (enumeration.hasMoreElements()) { // 遍历枚举对象. // 先定义一个节点变量. DefaultMutableTreeNode node; node = (DefaultMutableTreeNode) enumeration.nextElement();// 将节点名称给node. // 根据级别输出占位符. for (int l = 0; l < node.getLevel(); l++) { System.out.print("---"); } System.out.println(node.getUserObject());// 输入节点标签. treeIsVisible(node); } System.out.println(); System.out.println(); } /** * 打印树的可见区域,当用户改变对话框的大小时,树的可见区域发生变化 */ private void printTreeVisibleRect() { Rectangle rect = tree.getVisibleRect(); System.out.println("x"+rect.x + "y"+rect.y +"width"+rect.width +"height"+rect.height ); } /** * 判断节点是否可见 * @param node */ private void treeIsVisible(DefaultMutableTreeNode node) { DefaultTreeModel treeModel = (DefaultTreeModel)tree.getModel(); TreePath nodePath = new TreePath(treeModel.getPathToRoot(node)); System.out.println(nodePath); if(tree.isVisible(nodePath)){ System.out.println("可见"); } else{ System.out.println("不可见"); } } }}
阅读全文
0 0
- 树的可见区域的获取和滚动条监听
- 获取网页可见区域高度以及滚动条等信息的问题
- ListView 滚动监听 可见区域
- javascript获取可见区域、body对象的宽度和高度
- js获取网页可见区域的高度和宽度
- js如何获取可见区域的宽度和高度
- JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度
- JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度
- JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度
- JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度
- JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度
- JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度
- jQuery 获取浏览器窗口可视区域的高度和宽度及滚动条高度
- JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度
- jq下获取浏览器窗口的可视区域高度和宽度,滚动条高度
- JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度
- QUERY 获取浏览器窗口可视区域的高度和宽度及滚动条高度
- JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度
- 关于国密算法 SM1,SM2,SM3,SM4 的笔记
- 48. Rotate Image
- 记一次ajax跨域访问后,session失效
- 奇异值分解的推理及意义
- 创建用户以及用户授权
- 树的可见区域的获取和滚动条监听
- 2017.8.7 用python实现简单文本编辑器
- zabbix Warning: Using a password on the command line interface can be insecure
- 盛大游戏2017校招前端工程师笔试试卷
- 封装
- 【翻译】在Ext JS 5+ 使用ViewControllers
- Mac OS X下完全卸载MySQL
- log4j配置文件简介&slf4j
- C++ 怎样让函数返回数组