Swill的问题,为什么我点击按钮后,JTree没有创建相应结点呢?

dragonlw 2004-12-03 10:14:01
/*
* Created on 2004-12-3
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package praInformal;

/**
* @author dragonlw
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

import java.util.*;
import javax.swing.event.*;


public class Informal extends JFrame {
public Informal(){
super("Informal Demo");
setSize(400,400);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


// JSplitPane mysplit=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
// new TreeGenerated().getTreeNode() ,new ReqFunc());

getContentPane().add(new TreeGenerated().getTreeNode(),BorderLayout.WEST);
getContentPane().add(new ReqFunc(),BorderLayout.CENTER);
}
public static void main(String[] args){
new Informal().show();
}
}
class TreeGenerated implements ActionListener,DocumentListener{
private JTree tree;
private DefaultTreeModel model;
private JScrollPane scrollpane;
private DefaultMutableTreeNode root;
public TreeGenerated(){
root=new DefaultMutableTreeNode("Project");
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("Function");
root.add(newNode);
model=new DefaultTreeModel(root);
tree=new JTree(model);
tree.setEditable(true);
scrollpane=new JScrollPane(tree);

}

public void actionPerformed(ActionEvent o){
if(o.getSource()==ReqFunc.getOk()){

}
if(o.getSource()==ReqFunc.getDecompose()){
DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();

if(selectedNode==null)
selectedNode=root;//do you select a treenode?
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("New");
model.insertNodeInto(newNode,selectedNode,selectedNode.getChildCount());
//now display new node
TreeNode[] nodes=model.getPathToRoot(newNode);
TreePath path=new TreePath(nodes);
tree.scrollPathToVisible(path);
}
if(o.getSource()==ReqFunc.getNextFunc()){
DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if(selectedNode==null)return;//do you select a treenode?
DefaultMutableTreeNode parent=(DefaultMutableTreeNode)selectedNode.getParent();
if(parent==null)return;//is root?
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("New");
int selectedIndex=parent.getIndex(selectedNode);//node's parent wants to caculate the index of the selectedNode
model.insertNodeInto(newNode,parent,selectedIndex+1);
//now display new node
TreeNode[] nodes =model.getPathToRoot(newNode);
TreePath path=new TreePath(nodes);
tree.scrollPathToVisible(path);
}
}
public JScrollPane getTreeNode(){
return scrollpane;
}

public void changedUpdate(DocumentEvent o){

}
public void insertUpdate(DocumentEvent o){

}
public void removeUpdate(DocumentEvent o){

}
}

class ReqFunc extends JPanel {

class MyDocument implements DocumentListener{
//why can't we use the anonymous class here
public void changedUpdate(DocumentEvent o){
nameChanged();
}
public void insertUpdate(DocumentEvent o){
nameChanged();
}
public void removeUpdate(DocumentEvent o){
nameChanged();
}
}

TreeGenerated treeNode=new TreeGenerated();
public ReqFunc(){

this.setLayout(new BorderLayout());
jTextField=new JTextField();
jTextField.getDocument().addDocumentListener(treeNode);
jTextField.getDocument().addDocumentListener(new MyDocument());

add(jTextField,BorderLayout.NORTH);
JPanel jpanel=new JPanel();
jpanel.setLayout(new FlowLayout());

nextFunc=new JButton("NextFunction");
nextFunc.setEnabled(false);
nextFunc.addActionListener(treeNode);
/*
* nextFunc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent o){

}
});
*/
decompose=new JButton("Decompose");
decompose.setEnabled(false);
decompose.addActionListener(treeNode);
/*
* decompose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent o){

}
});
*/
ok=new JButton("Save");
ok.setEnabled(false);

ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent o){
ok.setEnabled(false);
decompose.setEnabled(true);
nextFunc.setEnabled(true);
}
});
ok.addActionListener(treeNode);

jpanel.add(nextFunc);
jpanel.add(decompose);
jpanel.add(ok);
add(jpanel,BorderLayout.SOUTH);
}
public static JButton getOk(){
return ok;
}
public static JButton getNextFunc(){
return nextFunc;
}
public static JButton getDecompose(){
return decompose;
}
public void nameChanged(){
ok.setEnabled(true);
decompose.setEnabled(false);
nextFunc.setEnabled(false);

}
private JTextField jTextField;
private static JButton nextFunc,decompose,ok;
}
...全文
106 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
febchen 2004-12-04
  • 打赏
  • 举报
回复
关键在class ReqFunc中
TreeGenerated treeNode=new TreeGenerated();
重新生成了对象treeNode,这个和
getContentPane().add(new TreeGenerated().getTreeNode(),BorderLayout.WEST);
不是一个treeNode对象,所以不起作用.
大致改了一下,都在注释里了.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

import java.util.*;
import javax.swing.event.*;


public class Informal extends JFrame {
public TreeGenerated tg; //这个对象单独生成
public Informal(){
super("Informal Demo");
setSize(400,400);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


// JSplitPane mysplit=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
// new TreeGenerated().getTreeNode() ,new ReqFunc());
tg=new TreeGenerated(); //实例化
getContentPane().add(tg.getTreeNode(),BorderLayout.WEST); //获取scrollPane
getContentPane().add(new ReqFunc(this),BorderLayout.CENTER); //将Informal类传入ReqFunc中
}
public static void main(String[] args){
new Informal().show();
}
}
class TreeGenerated implements ActionListener,DocumentListener{
private JTree tree;
private DefaultTreeModel model;
private JScrollPane scrollpane;
private DefaultMutableTreeNode root;
public TreeGenerated(){
root=new DefaultMutableTreeNode("Project");
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("Function");
root.add(newNode);
model=new DefaultTreeModel(root);
tree=new JTree(model);
tree.setEditable(true);
scrollpane=new JScrollPane(tree);

}

public void actionPerformed(ActionEvent o){
if(o.getSource()==ReqFunc.getOk()){

}
if(o.getSource()==ReqFunc.getDecompose()){
DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();

if(selectedNode==null)
selectedNode=root;//do you select a treenode?
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("New");
model.insertNodeInto(newNode,selectedNode,selectedNode.getChildCount());
//now display new node
TreeNode[] nodes=model.getPathToRoot(newNode);
TreePath path=new TreePath(nodes);
tree.scrollPathToVisible(path);
}
if(o.getSource()==ReqFunc.getNextFunc()){
DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if(selectedNode==null)return;//do you select a treenode?
DefaultMutableTreeNode parent=(DefaultMutableTreeNode)selectedNode.getParent();
if(parent==null)return;//is root?
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("New");
int selectedIndex=parent.getIndex(selectedNode);//node's parent wants to caculate the index of the selectedNode
model.insertNodeInto(newNode,parent,selectedIndex+1);
//now display new node
TreeNode[] nodes =model.getPathToRoot(newNode);
TreePath path=new TreePath(nodes);
tree.scrollPathToVisible(path);
}
}
public JScrollPane getTreeNode(){
return scrollpane;
}

public void changedUpdate(DocumentEvent o){

}
public void insertUpdate(DocumentEvent o){

}
public void removeUpdate(DocumentEvent o){

}
}

class ReqFunc extends JPanel {
Informal mf; //准备接收Informal对象
TreeGenerated treeNode;
class MyDocument implements DocumentListener{
//why can't we use the anonymous class here
public void changedUpdate(DocumentEvent o){
nameChanged();
}
public void insertUpdate(DocumentEvent o){
nameChanged();
}
public void removeUpdate(DocumentEvent o){
nameChanged();
}
}


public ReqFunc(Informal mf){ //构造时传入Informal对象
this.mf=mf;
treeNode=mf.tg; //获取Informal中的TreeNode
this.setLayout(new BorderLayout());
jTextField=new JTextField();
jTextField.getDocument().addDocumentListener(treeNode);
jTextField.getDocument().addDocumentListener(new MyDocument());

add(jTextField,BorderLayout.NORTH);
JPanel jpanel=new JPanel();
jpanel.setLayout(new FlowLayout());

nextFunc=new JButton("NextFunction");
nextFunc.setEnabled(false);
nextFunc.addActionListener(treeNode);
/*
* nextFunc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent o){

}
});
*/
decompose=new JButton("Decompose");
decompose.setEnabled(false);
decompose.addActionListener(treeNode);
/*
* decompose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent o){

}
});
*/
ok=new JButton("Save");
ok.setEnabled(false);

ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent o){
ok.setEnabled(false);
decompose.setEnabled(true);
nextFunc.setEnabled(true);
}
});
ok.addActionListener(treeNode);

jpanel.add(nextFunc);
jpanel.add(decompose);
jpanel.add(ok);
add(jpanel,BorderLayout.SOUTH);
}
public static JButton getOk(){
return ok;
}
public static JButton getNextFunc(){
return nextFunc;
}
public static JButton getDecompose(){
return decompose;
}
public void nameChanged(){
ok.setEnabled(true);
decompose.setEnabled(false);
nextFunc.setEnabled(false);

}
private JTextField jTextField;
private static JButton nextFunc,decompose,ok;
}
dragonlw 2004-12-04
  • 打赏
  • 举报
回复
分不够可以在给嘛,来者有分
dragonlw 2004-12-03
  • 打赏
  • 举报
回复
问题解决,马上给分。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧