目录
前言:
关于Swing进行一些创作和样品设计,不过笔者还是更加推荐去网上寻找一些视频教程,这里直接看代码的话会比较的蒙,建议初学者以视频讲解类的内容为主。
目标:
下图是使用以下代码实现的成品结构图。
实现:
导入包:
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
组件文本准备:
创建相关的容器主键,这里文本中有了一定的注释,具体还有那里不懂可以私聊笔者进行询问。
JFrame jf = new JFrame("简单树");
//创建 DefaultMutableTreeNode对象代表结点
//定义几个初始结点
DefaultMutableTreeNode root = new DefaultMutableTreeNode("中国");
DefaultMutableTreeNode guangDong = new DefaultMutableTreeNode("广东");
DefaultMutableTreeNode guangXi = new DefaultMutableTreeNode("广西");
DefaultMutableTreeNode foShan = new DefaultMutableTreeNode("佛山");
DefaultMutableTreeNode shanTou = new DefaultMutableTreeNode("汕头");
DefaultMutableTreeNode guiLin = new DefaultMutableTreeNode("桂林");
DefaultMutableTreeNode nanNing = new DefaultMutableTreeNode("南宁");
//定义按钮,完成操作
JButton addSiblingBtn = new JButton("添加兄弟结点");
JButton addChildBtn = new JButton("添加子结点");
JButton deleteBtn = new JButton("删除结点");
JButton editBtn = new JButton("编辑当前结点");
初始化界面:
初始化界面也就是对组件进行一定的封装,将小的组件封装到容器中并实现。
public void init() {
//组装结点之间的关系
root.add(guangDong);
root.add(guangXi);
guangDong.add(foShan);
guangDong.add(shanTou);
guangXi.add(guiLin);
guangXi.add(nanNing);
//创建JTree对象
JTree tree = new JTree(root);
//完成树的结点编辑的代码
tree.setEditable(true);
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
//处理添加
addSiblingBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//添加兄弟结点逻辑
//1.获取当前选中的结点
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (selectedNode == null) {
return;
}
//2.获取当前结点的父节点
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) selectedNode.getParent();
if (parentNode == null) {
return;
}
//3.创建新结点
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("新结点");
//4.把新节点通过父节点进行添加
int index = parentNode.getIndex(selectedNode);
model.insertNodeInto(newNode, parentNode, index);
//5.显示新节点
TreeNode[] pathToRoot = model.getPathToRoot(newNode);
TreePath treePath = new TreePath(pathToRoot);
tree.scrollPathToVisible(treePath);
//6.重绘tree
tree.updateUI();
}
});
addChildBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//添加兄弟结点逻辑
//1.获取当前选中的结点
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (selectedNode == null) {
return;
}
//2.创建新结点
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("新结点");
//3.把新结点添加到当前结点中
selectedNode.add(newNode);
//4.显示新结点
TreeNode[] pathToRoot = model.getPathToRoot(newNode);
TreePath treePath = new TreePath(pathToRoot);
tree.scrollPathToVisible(treePath);
//5.重绘UI
tree.updateUI();
}
});
jf.add(new JScrollPane(tree));
JPanel panel = new JPanel();
panel.add(addSiblingBtn);
panel.add(addChildBtn);
panel.add(deleteBtn);
panel.add(editBtn);
jf.add(panel, BorderLayout.SOUTH);
//处理删除
deleteBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (selectedNode != null && selectedNode.getParent() != null) {
model.removeNodeFromParent(selectedNode);
}
}
});
//处理编辑
editBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取当前选中结点的路径
TreePath selectionPath = tree.getSelectionPath();
//判断路径不为空,则设置该路径的最后一个结点可编辑
if (selectionPath != null) {
tree.startEditingAtPath(selectionPath);
}
}
});
//把JTree对象放入到窗口中进行展示
jf.add(tree);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
总代码:
接下来就是完整的代码实现了
package com.swing11;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* JTree结点编辑1
*/
public class Demo112 {
JFrame jf = new JFrame("简单树");
//创建 DefaultMutableTreeNode对象代表结点
//定义几个初始结点
DefaultMutableTreeNode root = new DefaultMutableTreeNode("中国");
DefaultMutableTreeNode guangDong = new DefaultMutableTreeNode("广东");
DefaultMutableTreeNode guangXi = new DefaultMutableTreeNode("广西");
DefaultMutableTreeNode foShan = new DefaultMutableTreeNode("佛山");
DefaultMutableTreeNode shanTou = new DefaultMutableTreeNode("汕头");
DefaultMutableTreeNode guiLin = new DefaultMutableTreeNode("桂林");
DefaultMutableTreeNode nanNing = new DefaultMutableTreeNode("南宁");
//定义按钮,完成操作
JButton addSiblingBtn = new JButton("添加兄弟结点");
JButton addChildBtn = new JButton("添加子结点");
JButton deleteBtn = new JButton("删除结点");
JButton editBtn = new JButton("编辑当前结点");
public void init() {
//组装结点之间的关系
root.add(guangDong);
root.add(guangXi);
guangDong.add(foShan);
guangDong.add(shanTou);
guangXi.add(guiLin);
guangXi.add(nanNing);
//创建JTree对象
JTree tree = new JTree(root);
//完成树的结点编辑的代码
tree.setEditable(true);
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
//处理添加
addSiblingBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//添加兄弟结点逻辑
//1.获取当前选中的结点
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (selectedNode == null) {
return;
}
//2.获取当前结点的父节点
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) selectedNode.getParent();
if (parentNode == null) {
return;
}
//3.创建新结点
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("新结点");
//4.把新节点通过父节点进行添加
int index = parentNode.getIndex(selectedNode);
model.insertNodeInto(newNode, parentNode, index);
//5.显示新节点
TreeNode[] pathToRoot = model.getPathToRoot(newNode);
TreePath treePath = new TreePath(pathToRoot);
tree.scrollPathToVisible(treePath);
//6.重绘tree
tree.updateUI();
}
});
addChildBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//添加兄弟结点逻辑
//1.获取当前选中的结点
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (selectedNode == null) {
return;
}
//2.创建新结点
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("新结点");
//3.把新结点添加到当前结点中
selectedNode.add(newNode);
//4.显示新结点
TreeNode[] pathToRoot = model.getPathToRoot(newNode);
TreePath treePath = new TreePath(pathToRoot);
tree.scrollPathToVisible(treePath);
//5.重绘UI
tree.updateUI();
}
});
jf.add(new JScrollPane(tree));
JPanel panel = new JPanel();
panel.add(addSiblingBtn);
panel.add(addChildBtn);
panel.add(deleteBtn);
panel.add(editBtn);
jf.add(panel, BorderLayout.SOUTH);
//处理删除
deleteBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (selectedNode != null && selectedNode.getParent() != null) {
model.removeNodeFromParent(selectedNode);
}
}
});
//处理编辑
editBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取当前选中结点的路径
TreePath selectionPath = tree.getSelectionPath();
//判断路径不为空,则设置该路径的最后一个结点可编辑
if (selectionPath != null) {
tree.startEditingAtPath(selectionPath);
}
}
});
//把JTree对象放入到窗口中进行展示
jf.add(tree);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new Demo112().init();
}
}