扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
//先选中节点才能增加节点
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、虚拟空间、营销软件、网站建设、达拉特网站维护、网站推广。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class TreeTest implements ActionListener,TreeModelListener{
JLabel label=null;
JTree tree=null;
DefaultTreeModel treeModel=null;
String nodeName=null;//原有节点名称
public TreeTest(){
JFrame f=new JFrame("TreeTest");
Container contentPane=f.getContentPane();
DefaultMutableTreeNode root=new DefaultMutableTreeNode("资源管理器");
tree=new JTree(root);
tree.setEditable(true);
tree.addMouseListener(new MouseHandle());
treeModel=(DefaultTreeModel)tree.getModel();
treeModel.addTreeModelListener(this);
JScrollPane scrollPane=new JScrollPane();
scrollPane.setViewportView(tree);
JPanel panel=new JPanel();
JButton b=new JButton("新增节点");
b.addActionListener(this);
panel.add(b);
b=new JButton("删除节点");
b.addActionListener(this);
panel.add(b);
b=new JButton("清除所有节点");
b.addActionListener(this);
panel.add(b);
label=new JLabel("Action");
contentPane.add(panel,BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(label,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//本方法运行新增、删除、清除所有节点的程序代码.
public void actionPerformed(ActionEvent ae){
if (ae.getActionCommand().equals("新增节点")){
DefaultMutableTreeNode parentNode=null;
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("新节点");
newNode.setAllowsChildren(true);
TreePath parentPath=tree.getSelectionPath();
//取得新节点的父节点
parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent());
//由DefaultTreeModel的insertNodeInto()方法增加新节点
treeModel.insertNodeInto(newNode,parentNode,parentNode.getChildCount());
//tree的scrollPathToVisible()方法在使Tree会自动展开文件夹以便显示所加入的新节点。若没加这行则加入的新节点
//会被 包在文件夹中,你必须自行展开文件夹才看得到。
tree.scrollPathToVisible(new TreePath(newNode.getPath()));
label.setText("新增节点成功");
}
if (ae.getActionCommand().equals("删除节点")){
TreePath treepath=tree.getSelectionPath();
if (treepath!=null){
//下面两行取得选取节点的父节点.
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)treepath.getLastPathComponent();
TreeNode parent=(TreeNode)selectionNode.getParent();
if (parent!=null) {
//由DefaultTreeModel的removeNodeFromParent()方法删除节点,包含它的子节点。
treeModel.removeNodeFromParent(selectionNode);
label.setText("删除节点成功");
}
}
}
if (ae.getActionCommand().equals("清除所有节点")){
//下面一行,由DefaultTreeModel的getRoot()方法取得根节点.
DefaultMutableTreeNode rootNode=(DefaultMutableTreeNode)treeModel.getRoot();
//下面一行删除所有子节点.
rootNode.removeAllChildren();
//删除完后务必运行DefaultTreeModel的reload()操作,整个Tree的节点才会真正被删除.
treeModel.reload();
label.setText("清除所有节点成功");
}
}
public void treeNodesChanged(TreeModelEvent e){
TreePath treePath=e.getTreePath();
DefaultMutableTreeNode node=(DefaultMutableTreeNode)treePath.getLastPathComponent();
try{
int[] index=e.getChildIndices();
node=(DefaultMutableTreeNode)node.getChildAt(index[0]);
}catch(NullPointerException exc){}
label.setText(nodeName+"更改数据为:"+(String)node.getUserObject());
}
public void treeNodesInserted(TreeModelEvent e){
System.out.println("new node insered");
}
public void treeNodesRemoved(TreeModelEvent e){
System.out.println("node deleted");
}
public void treeStructureChanged(TreeModelEvent e){
System.out.println("Structrue changed");
}
public static void main(String[] args){
new TreeTest();
}
class MouseHandle extends MouseAdapter{
public void mousePressed(MouseEvent e){
try{
JTree tree=(JTree)e.getSource();
int rowLocation=tree.getRowForLocation(e.getX(),e.getY());
TreePath treepath=tree.getPathForRow(rowLocation);
TreeNode treenode=(TreeNode)treepath.getLastPathComponent();
nodeName=treenode.toString();
}catch(NullPointerException ne){}
}
}
}
public class ZLinkedList {
private int size;
private Node head;
public ZLinkedList(){
size = 0;
}
public void headInsert(Object obj){
//if(null== obj) {// do something}
Node temp = new Node(obj);
if(size ==0){
head = temp;
}else{
temp.setNext(head);
head = temp;
}
size++;
}
public void preOrder(){
int length = size;
Node temp = head;
for(int i= 0;i length;i ++){
System.out.println(temp.getValue());
temp = temp.getNext();
}
}
private static class Node{
private Object value;
private Node next;
Node(){
}
Node(Object val){
this.value = val;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public static void main(String[] args) {
ZLinkedList test = new ZLinkedList();
test.headInsert("1");
test.headInsert("2");
test.headInsert("3");
test.preOrder();
}
}
public class ZLinkedList {
private int size;
private Node head;
public ZLinkedList(){
size = 0;
}
public void headInsert(Object obj){
//if(null== obj) {// do something}
Node temp = new Node(obj);
if(size ==0){
head = temp;
}else{
temp.setNext(head);
head = temp;
}
size++;
}
public void tailInsert(Object obj){
// if(null== obj) {// do something}
Node temp = new Node(obj);
if (size == 0) {
head = temp;
} else {
Node t = head;
while(t.getNext()!=null)
t= t.getNext();
t.setNext(temp);
}
size++;
}
public void preOrder(){
int length = size;
Node temp = head;
for(int i= 0;i length;i ++){
System.out.println(temp.getValue());
temp = temp.getNext();
}
}
private static class Node{
private Object value;
private Node next;
Node(){
}
Node(Object val){
this.value = val;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public static void main(String[] args) {
ZLinkedList test = new ZLinkedList();
test.tailInsert("1");
test.tailInsert("2");
test.tailInsert("3");
test.preOrder();
}
}
//java qq交流群:251832769
package com.llist;
public class SingleLinkListT implements LListT {
protected NodeT head; // 头指针,指向单链表的头结点
// 默认构造方法,构造空单链表
public SingleLinkList(){
head = new NodeT();
}
// 由指定数组中的多个对象构造单链表,采用尾查构造单链表
public SingleLinkList(T[] element){
this();
NodeT rear = this.head;
for(int i=0;ielement.length;i++){
rear.next = new NodeT(element[i],null);
rear = rear.next;
}
}
// 判断链表是否为空
public boolean isEmpty() {
return this.head.next==null;
}
// 返回链表的长度
public int length() {
int count = 0;
NodeT p = this.head.next;
while(p!=null){
count++;
p = p.next;
}
return count;
}
// 返回第i(i0)个元素,若i的指定序号无效,则返回null
public T get(int i) {
if(i=0||ithis.length()){
return null;
}
else{
NodeT p = this.head;
for(int j=0;ji;j++){
p = p.next;
}
return p.data;
}
}
// 设置第i(i0)个元素的值为x,若i指定序号无效,则抛出序号越界异常
public void set(int i, T x) {
if(x==null)
return;
if(i=0||ithis.length()){
// 抛出序号越界异常
throw new IndexOutOfBoundsException("指定序号i="+i+"越界!");
}
else{
NodeT p = this.head;
for(int j=0;ji;j++){
p = p.next;
}
p.data = x;
}
}
// 链表的插入操作
public void insert(int i, T x) {
if(x==null) // 插入对象不能为空
return;
if(i==0){ // 插在头结点之后
head.next = new NodeT(x,this.head.next);
}
else if(i0i=this.length()){
NodeT p = this.head;
// 寻找插入位置
for(int j=0;ji;j++){
p = p.next;
}
// 插入x作为p结点的后继结点
p.next = new NodeT(x,p.next);
}
else{
// 抛出序号越界异常
throw new IndexOutOfBoundsException("指定序号i="+i+"越界!");
}
}
// 在单链表的最后添加对象
public void append(T x) {
insert(this.length(),x);
}
// 删除序号为i的结点,操作成功返回给对象,否则返回null
public T remove(int i) {
if(i=0||ithis.length()){
return null;
}
else{
NodeT p = this.head;
// 定位到待删除结点(i)的前驱结点(i-1)
for(int j=0;ji-1;j++){
p = p.next;
}
T old = p.next.data;
p.next = p.next.next;
return old;
}
}
// 删除单链表的所有元素
public void removeAll() {
this.head.next = null;
}
// 查找,返回首次出现关键子key的元素的序号
public int Search(T key) {
if(key==null)
return 0;
else{
int i = 0;
NodeT p = this.head;
while(p.next!=null){
i++;
p = p.next;
if(p.data.equals(key))
break;
}
if(p.next==null)
return 0;
else
return i;
}
}
//返回链表的所有元素的描述字符串,覆盖Object类的toString()方法
public String toString(){
String str = " ( ";
NodeT p = this.head.next;
while(p!=null){
str += p.data.toString();
p = p.next;
if(p!=null)
str += ",";
}
str += " ) ";
return str;
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流