import java.awt.Dimension;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
import javax.swing.tree.*;
import javax.swing.*;
import java.awt.event.*;
public class Level
...{

static JTree tree;
static DefaultMutableTreeNode Levels,Law,Rule,Opt,Tec,Mag,Work;
static JPanel panel;
static DefaultTreeModel model;
static DefaultMutableTreeNode root,child,chosen;
Vector v_modify,v_del,v_new;
LevelInfo infor_temp;
public static void main (String[] args)
...{
v_modify = new Vector();
v_new = new Vector();
v_del = new Vector();
infor_temp = new LevelInfo();
Levels = new DefaultMutableTreeNode ("体系层次表");
Law = new DefaultMutableTreeNode ("101");
i_Law = new LevelInfo();
i_Law.setNum("101");
i_Law.setName("企业贯彻的国家及上级法律法规");
Rule = new DefaultMutableTreeNode ("102");
i_Rule= new LevelInfo();
i_Rule.setNum("102");
i_Rule.setName("企业标准化规定");
Opt = new DefaultMutableTreeNode ("103");
i_Opt= new LevelInfo();
i_Opt.setNum("103");
i_Opt.setName("企业适用的法律,法规和规章");
Tec = new DefaultMutableTreeNode ("201");
i_Tec= new LevelInfo();
i_Tec.setNum("201");
i_Tec.setName("技术标准");
Mag = new DefaultMutableTreeNode ("202");
i_Mag= new LevelInfo();
i_Mag.setNum("202");
i_Mag.setName("管理标准");
Work = new DefaultMutableTreeNode ("301");
i_Work= new LevelInfo();
i_Work.setNum("301");
i_Work.setName("工作标准");
Levels.add (Law);
Levels.add (Rule);
Levels.add (Opt);
Levels.add (Tec);
Levels.add (Mag);
Levels.add (Work);
root = Levels;
tree = new JTree (Levels);
tree.setEditable(true);
panel = new JPanel();
panel.setLayout (new BoxLayout (panel, BoxLayout.X_AXIS));
panel.setPreferredSize (new Dimension (400, 400));
panel.add (new JScrollPane (tree));
model=(DefaultTreeModel)tree.getModel(); 


tree.addMouseListener(new java.awt.event.MouseAdapter() ...{ 
public void mouseReleased(MouseEvent e)...{
if (e.getButton() == MouseEvent.BUTTON3)
jTree_mouseRressed(e); }
}); 
JFrame frame = new JFrame ("体系表编辑器");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (panel);
frame.pack();
frame.show();
}


static void jTree_mouseRressed(MouseEvent e) ...{

int x = e.getX();
int y = e.getY();
JPopupMenu popup=new JPopupMenu();
JMenuItem mi1=new JMenuItem("新建");
JMenuItem mi3=new JMenuItem("修改");
JMenuItem mi2=new JMenuItem("删除");
JMenuItem mi4=new JMenuItem("属性");
popup.add(mi1);
popup.add(mi2);
popup.add(mi3);
mi1.addActionListener(new ActionListener() 
...{
public void actionPerformed(ActionEvent event) 
...{
String num=javax.swing.JOptionPane.showInputDialog("请输入层次编号:");
if (num==null || num.equals(""))
...{
return;
}
String name=javax.swing.JOptionPane.showInputDialog("请输入层次名称:");
if (name==null || name.equals(""))
...{
return;
}
DefaultMutableTreeNode selected=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
DefaultMutableTreeNode node=new DefaultMutableTreeNode(num);
model.insertNodeInto(node,selected,0);
TreePath path=new TreePath(node.getPath());
tree.makeVisible(path);
}
}
);
mi2.addActionListener(new ActionListener() 
...{
public void actionPerformed(ActionEvent event) 
...{
DefaultMutableTreeNode selected=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
model.removeNodeFromParent(selected);
}
}
)
;
mi3.addActionListener(new ActionListener() 
...{
public void actionPerformed(ActionEvent event) 
...{
String num=javax.swing.JOptionPane.showInputDialog("请输入修改后的层次编号:");
if (num==null || num.equals(""))
...{
return;
}
String name=javax.swing.JOptionPane.showInputDialog("请输入修改后的层次名称:");
if (name==null || name.equals(""))
...{
return;
}
DefaultMutableTreeNode selected=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
selected.setUserObject(num);

}
}
);
popup.show(e.getComponent(),x,y);
} 


}

class LevelInfo...{
private String num;
private String name;
public void setNum(String inNum)...{this.num=inNum;};
public void setName(String inName)...{this.name=inName;};
public String getNum()...{return num;};
public String getName()...{return name;};
}发表于 @ 2007年08月27日 21:23:00 | 评论( loading... ) | 举报| 收藏