请教一道关于java jtree的编程题
是用java制作一个mind-map的工具要求在model部分每个想法由标题和描写组成,然后在application里可以实现展开树的节点,添加,删除,undo和redo...
是用java制作一个mind-map的工具 要求在model部分每个想法由标题和描写组成, 然后在application里可以实现展开树的节点,添加,删除, undo和redo的功能
有没有人能给我个相关的例子参考一下,谢谢了!!
ps不是在编写一个.java程序,因为我之前在找到的都是比较简单的jtree示例,题目是比较复杂一点....ms越说越乱了...
谢谢你的回答,关于jtree的节点(叶子) 如何定义你能不能给点提示?
还有jtree的undo ,redo 该如何实现?
我之前是想定义一个class idea, 然后再把它定义成list形式,不知道这样想对不对。。。 展开
有没有人能给我个相关的例子参考一下,谢谢了!!
ps不是在编写一个.java程序,因为我之前在找到的都是比较简单的jtree示例,题目是比较复杂一点....ms越说越乱了...
谢谢你的回答,关于jtree的节点(叶子) 如何定义你能不能给点提示?
还有jtree的undo ,redo 该如何实现?
我之前是想定义一个class idea, 然后再把它定义成list形式,不知道这样想对不对。。。 展开
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.beans.*;
public class JTree_main extends JFrame {
static JTree tree = new JTree();
public JTree_main() {
Container contentPane = getContentPane();
JScrollPane scrollPane = new JScrollPane(tree);
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
tree.addPropertyChangeListener(new PropertyListener());
}
class ControlPanel extends JPanel {
JCheckBox showRoot = new JCheckBox("show root node");
public ControlPanel() {
showRoot.setSelected(tree.isRootVisible());
setLayout(new FlowLayout());
add(showRoot);
showRoot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tree.setRootVisible(showRoot.isSelected());
}
});
}
}
class PropertyListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String name = e.getPropertyName();
if(name.equals(JTree.ROOT_VISIBLE_PROPERTY)) {
String msg = "Root Visible Property: " +e.getNewValue().toString();
JOptionPane.showMessageDialog(JTree_main.tree,msg,"Property Change",JOptionPane.INFORMATION_MESSAGE);
}
}
}
public static void main(String args[]) {
JTree_main a=new JTree_main();
a.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
a.setBounds(300,300,450,300);
a.setVisible(true);
}
}
给你一个参考一下
下面的是增加和删除树节点
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JTree tree = new JTree();
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
TreeSelectionModel selectionModel = tree.getSelectionModel();
JButton removeButton = new JButton("Remove selected node");
JButton addButton = new JButton("Add node");
public Test() {
Container contentPane = getContentPane();
selectionModel.setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(tree, BorderLayout.CENTER);
tree.addTreeSelectionListener(
new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath path = e.getNewLeadSelectionPath();
boolean nodesAreSelected = (path != null);
addButton.setEnabled(nodesAreSelected);
removeButton.setEnabled(nodesAreSelected);
}
});
model.addTreeModelListener(new TreeModelListener() {
public void treeNodesInserted(TreeModelEvent e) {
showInsertionOrRemoval(e, " added to ");
}
public void treeNodesRemoved(TreeModelEvent e) {
showInsertionOrRemoval(e, " removed from ");
}
private void showInsertionOrRemoval(TreeModelEvent e,
String s) {
Object[] parentPath = e.getPath();
int[] indexes = e.getChildIndices();
Object[] children = e.getChildren();
Object parent = parentPath[parentPath.length-1];
JOptionPane.showMessageDialog(Test.this,
"Node \"" + children[0].toString() +
"\"" + s + parent.toString() +
" at index " + indexes[0],
"Node Added or Removed",
JOptionPane.INFORMATION_MESSAGE);
}
public void treeNodesChanged(TreeModelEvent e) {}
public void treeStructureChanged(TreeModelEvent e) {}
});
}
class ControlPanel extends JPanel {
public ControlPanel() {
addButton.setEnabled(false);
removeButton.setEnabled(false);
add(addButton);
add(removeButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path =
selectionModel.getSelectionPath();
MutableTreeNode parent, node =
(MutableTreeNode)path.getLastPathComponent();
if(path.getPathCount() > 1)
parent = (MutableTreeNode)node.getParent();
else
parent = (MutableTreeNode)node;
int index = parent.getIndex(node) + 1;
String s = JOptionPane.showInputDialog(
Test.this,
"Enter a name for the new node:",
"New Tree Node",
JOptionPane.QUESTION_MESSAGE);
MutableTreeNode newNode =
new DefaultMutableTreeNode(s);
model.insertNodeInto(newNode, parent, index);
}
});
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path =
selectionModel.getSelectionPath();
if(path.getPathCount() == 1) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove root node!");
return;
}
MutableTreeNode node =
(MutableTreeNode)path.getLastPathComponent();
model.removeNodeFromParent(node);
}
});
}
}
public static void main(String args[]) {
GraphicJavaApplication.launch(new Test(),
"Tree Model Example",300,300,450,300);
}
}
class GraphicJavaApplication extends WindowAdapter {
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.beans.*;
public class JTree_main extends JFrame {
static JTree tree = new JTree();
public JTree_main() {
Container contentPane = getContentPane();
JScrollPane scrollPane = new JScrollPane(tree);
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
tree.addPropertyChangeListener(new PropertyListener());
}
class ControlPanel extends JPanel {
JCheckBox showRoot = new JCheckBox("show root node");
public ControlPanel() {
showRoot.setSelected(tree.isRootVisible());
setLayout(new FlowLayout());
add(showRoot);
showRoot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tree.setRootVisible(showRoot.isSelected());
}
});
}
}
class PropertyListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String name = e.getPropertyName();
if(name.equals(JTree.ROOT_VISIBLE_PROPERTY)) {
String msg = "Root Visible Property: " +e.getNewValue().toString();
JOptionPane.showMessageDialog(JTree_main.tree,msg,"Property Change",JOptionPane.INFORMATION_MESSAGE);
}
}
}
public static void main(String args[]) {
JTree_main a=new JTree_main();
a.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
a.setBounds(300,300,450,300);
a.setVisible(true);
}
}
给你一个参考一下
下面的是增加和删除树节点
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JTree tree = new JTree();
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
TreeSelectionModel selectionModel = tree.getSelectionModel();
JButton removeButton = new JButton("Remove selected node");
JButton addButton = new JButton("Add node");
public Test() {
Container contentPane = getContentPane();
selectionModel.setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(tree, BorderLayout.CENTER);
tree.addTreeSelectionListener(
new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath path = e.getNewLeadSelectionPath();
boolean nodesAreSelected = (path != null);
addButton.setEnabled(nodesAreSelected);
removeButton.setEnabled(nodesAreSelected);
}
});
model.addTreeModelListener(new TreeModelListener() {
public void treeNodesInserted(TreeModelEvent e) {
showInsertionOrRemoval(e, " added to ");
}
public void treeNodesRemoved(TreeModelEvent e) {
showInsertionOrRemoval(e, " removed from ");
}
private void showInsertionOrRemoval(TreeModelEvent e,
String s) {
Object[] parentPath = e.getPath();
int[] indexes = e.getChildIndices();
Object[] children = e.getChildren();
Object parent = parentPath[parentPath.length-1];
JOptionPane.showMessageDialog(Test.this,
"Node \"" + children[0].toString() +
"\"" + s + parent.toString() +
" at index " + indexes[0],
"Node Added or Removed",
JOptionPane.INFORMATION_MESSAGE);
}
public void treeNodesChanged(TreeModelEvent e) {}
public void treeStructureChanged(TreeModelEvent e) {}
});
}
class ControlPanel extends JPanel {
public ControlPanel() {
addButton.setEnabled(false);
removeButton.setEnabled(false);
add(addButton);
add(removeButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path =
selectionModel.getSelectionPath();
MutableTreeNode parent, node =
(MutableTreeNode)path.getLastPathComponent();
if(path.getPathCount() > 1)
parent = (MutableTreeNode)node.getParent();
else
parent = (MutableTreeNode)node;
int index = parent.getIndex(node) + 1;
String s = JOptionPane.showInputDialog(
Test.this,
"Enter a name for the new node:",
"New Tree Node",
JOptionPane.QUESTION_MESSAGE);
MutableTreeNode newNode =
new DefaultMutableTreeNode(s);
model.insertNodeInto(newNode, parent, index);
}
});
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path =
selectionModel.getSelectionPath();
if(path.getPathCount() == 1) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove root node!");
return;
}
MutableTreeNode node =
(MutableTreeNode)path.getLastPathComponent();
model.removeNodeFromParent(node);
}
});
}
}
public static void main(String args[]) {
GraphicJavaApplication.launch(new Test(),
"Tree Model Example",300,300,450,300);
}
}
class GraphicJavaApplication extends WindowAdapter {
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询