就是获得已选择的结点,再在这个结点基础上添加子结点(作为点击右键的一个功能)。
有谁知道,请指教一下,谢谢!
有谁知道,请指教一下,谢谢!
解决方案 »
- String str=new String()+"abc"一共创建了几个对象?
- 一个二维数组根据值(m)改变数组(m)个位置的值
- 关于容器、hashmap的一个问题 高分请教!
- 新手,,求高手帮忙看看这个错误是什么原因
- 关于线程编程的问题
- windows98 或me平台下可以安装java142吗
- 请教怎样在applet中进行socket套接字编程?
- 帮忙: 公式解析不支持科学记数,
- 从自己的库重调用protected函数但调不通,请求帮助。
- 高手请进,有人会么?我问一遍了,这个问题竟然没人回答!!!!!!!!!!!!!!!!!!
- java程序 在32位机和64位机上运行,有什么区别?
- 泛型类的嵌套
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;public class AbstractTree extends JFrame implements ActionListener{ private JTree jtree;
private Box box_right;
private JButton sure;
private JButton cancel;
private JLabel fast_location;
private JTextField fast_location_Text;
private JButton add;
private JButton change;
private JButton del;
private JButton editregular;
private JButton add_kind;
private JButton change_kind;
private JButton del_kind;
private JButton showtavble;
private Box boxbotton;
private DefaultTreeModel model;
Toolkit toolkit=Toolkit.getDefaultToolkit();
Dimension size=toolkit.getScreenSize();
JFrame SiblingNode;
JLabel code;
JLabel name;
JTextField codeField;
JTextField nameField;
JButton sureNode;
JButton cannelNode;
public AbstractTree(){
//通过继承父类,设置窗口标题
super("信息摘要");
//设置外观风格:windows风格
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
init();
} private void init() {
//定义初始节点
DefaultMutableTreeNode Info=new DefaultMutableTreeNode("信息摘要");
DefaultMutableTreeNode Info_buy=new DefaultMutableTreeNode("01-采购");
DefaultMutableTreeNode Info_sale=new DefaultMutableTreeNode("02-销售");
DefaultMutableTreeNode Info_storeManager=new DefaultMutableTreeNode("03-库管");
Info.add(Info_buy);
Info.add(Info_sale);
Info.add(Info_storeManager);
//以info为根节点创建树
jtree=new JTree(Info);
//设置只能选择一个TreePath
jtree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
//设置根节点是否折叠的图标可见
jtree.setShowsRootHandles(true);
//设置根节点是否可见
jtree.setRootVisible(true);
//设置jtree的大小
jtree.setPreferredSize(new Dimension(250,250));
//创建一个垂直排列的Box容器
box_right=Box.createVerticalBox();
sure=new JButton("确 定");
//添加监听事件,具体事件在实现ActionLisenter接口所重写的actionPerformed方法里
sure.addActionListener(this);
cancel=new JButton("取 消");
cancel.addActionListener(this);
fast_location=new JLabel("快速定位:");
fast_location_Text=new JTextField();
add=new JButton("增加摘要");
add.addActionListener(this);
change=new JButton("修改摘要");
change.addActionListener(this);
del=new JButton("删除摘要");
del.addActionListener(this);
editregular=new JButton("编码规则");
editregular.addActionListener(this);
//利用创建的垂直排列的box,设置主件之间的间距,单位:px
box_right.add(Box.createVerticalStrut(10));
box_right.add(sure);
box_right.add(Box.createVerticalStrut(20));
box_right.add(cancel);
box_right.add(Box.createVerticalStrut(50));
box_right.add(fast_location);
box_right.add(Box.createVerticalStrut(10));
box_right.add(fast_location_Text);
box_right.add(Box.createVerticalStrut(20));
box_right.add(add);
box_right.add(Box.createVerticalStrut(20));
box_right.add(change);
box_right.add(Box.createVerticalStrut(20));
box_right.add(del);
box_right.add(Box.createVerticalStrut(50));
box_right.add(editregular);
box_right.add(Box.createVerticalStrut(10));
//创建水平排列的box
boxbotton=Box.createHorizontalBox();
add_kind=new JButton("增加类别");
add_kind.addActionListener(this);
change_kind=new JButton("修改类别");
change_kind.addActionListener(this);
del_kind=new JButton("删除类别");
del_kind.addActionListener(this);
this.add(jtree,BorderLayout.WEST);
this.add(box_right,BorderLayout.EAST);
boxbotton.add(Box.createHorizontalStrut(5));
boxbotton.add(add_kind);
boxbotton.add(Box.createHorizontalStrut(5));
boxbotton.add(change_kind);
boxbotton.add(Box.createHorizontalStrut(5));
boxbotton.add(del_kind);
boxbotton.add(Box.createHorizontalStrut(50));
//获取jtree对应的TreeModel对象
model=(DefaultTreeModel) jtree.getModel();
//设置树可编辑
jtree.setEditable(true);
this.add(boxbotton,BorderLayout.SOUTH);
this.setBounds((size.width-364)/2,(size.height-442)/2,354,442);
//this.pack();
this.setVisible(true);
}
//事件监听集,集中管理,可读性强
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(cancel)){
this.dispose();
}if(e.getSource().equals(add_kind)){
SiblingNode();
}if(e.getSource().equals(change_kind)){
editNode();
}if(e.getSource().equals(del_kind)){
delNode();
}if(e.getSource().equals(add)){
addchild();
}if(e.getSource().equals(change)){
editNode();
}if(e.getSource().equals(del)){
delchild();
}
//if(e.getSource().equals(editregular)){
// new Coding();
// }
}
//以弹出新窗口方式添加类节点的方法
protected void SiblingNode() {
//产生新窗口
SiblingNode=new JFrame();
code=new JLabel("类别代号");
name=new JLabel("类别名称");
codeField=new JTextField(10);
nameField=new JTextField(10);
sureNode=new JButton("确定");
sureNode.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String nameNode=codeField.getText()+"-"+nameField.getText();
//获取选中节点
DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode) jtree.getLastSelectedPathComponent();
if(selectedNode==null){
return;
}
//获取选中节点的父节点
DefaultMutableTreeNode parent=(DefaultMutableTreeNode) selectedNode.getParent();
if(parent==null){
return;
}
//设置新节点的名称产生新节点
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode(nameNode);
//获取选中节点的节点索引
int selectedIndex=parent.getIndex(selectedNode);
//在选中位置插入新节点
model.insertNodeInto(newNode, parent, selectedIndex+1);
//获取根节点到新节点的所有节点
TreeNode[] nodes=model.getPathToRoot(newNode);
//使用指定的节点数组来创建TreePath
TreePath path=new TreePath(nodes);
//显示指定的TreePath
jtree.scrollPathToVisible(path);
}
});
cannelNode=new JButton("取消");
SiblingNode.add(code);
SiblingNode.add(codeField);
SiblingNode.add(name);
SiblingNode.add(nameField);
SiblingNode.add(sureNode);
SiblingNode.add(cannelNode);
SiblingNode.setLayout(new FlowLayout(FlowLayout.LEFT,60,10));
SiblingNode.setBounds((size.width-270)/2,(size.height-164)/2,270,164);
SiblingNode.setVisible(true);
}
//修改节点的方法
protected void editNode() {
TreePath selectedpath=jtree.getSelectionPath();
if(selectedpath!=null){
jtree.startEditingAtPath(selectedpath);
}
}
//删除节点的方法
protected void delNode() {
DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode) jtree.getLastSelectedPathComponent();
if(selectedNode!=null&&selectedNode.getParent()!=null){
//判断选中的节点是否含有子节点
if(selectedNode.isLeaf()){
model.removeNodeFromParent(selectedNode);
}else{
JOptionPane.showConfirmDialog(null,"当前选中的级别尚有下级摘要,不能删除!");
}
}
}
//添加子节点的方法
protected void addchild() {
DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode) jtree.getLastSelectedPathComponent();
if(selectedNode==null){
return ;
}
DefaultMutableTreeNode chlidNode=new DefaultMutableTreeNode("childNode");
selectedNode.add(chlidNode);
TreeNode[] nodes=model.getPathToRoot(chlidNode);
TreePath path=new TreePath(nodes);
jtree.scrollPathToVisible(path);
jtree.updateUI();
}
//删除子节点的方法
protected void delchild() {
DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode) jtree.getLastSelectedPathComponent();
if(selectedNode!=null&&selectedNode.getParent()!=null){
model.removeNodeFromParent(selectedNode);
}
}
public static void main(String[] args) {
new AbstractTree();
}
}
public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
imageTreeValueChanged(evt);
}
});获得选中的节点:private void imageTreeValueChanged(javax.swing.event.TreeSelectionEvent evt){
node = (TreeNode) evt.getPath().getLastPathComponent();
....
}