当前位置: 技术问答>java相关
序列化问题
来源: 互联网 发布时间:2015-07-23
本文导语: 我有一个JTREE的界面,当改变界面内容后(增减节点),想保存改动,下次启动时用改动后的界面, class Tree{ Tree(){ load(); jTree1=new JTree(top); //when first time...
我有一个JTREE的界面,当改变界面内容后(增减节点),想保存改动,下次启动时用改动后的界面,
class Tree{
Tree(){ load();
jTree1=new JTree(top);
//when first time use,init the tree with sample,or init the
//tree with the load date.
createNodes(top);
treemodel=(DefaultTreeModel)jTree1.getModel();
jTree1.setBorder(BorderFactory.createEtchedBorder());
jTree1.setEditable(true);
//set the tree single selected once
jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
jTree1.setRowHeight(26);
//extend the root
jTree1.setShowsRootHandles(true);
//when extend a node, chicked 1 time
jTree1.setToggleClickCount(1);
jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jTree1_mouseClicked(e);
}
});
jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
jTree1_valueChanged(e);
}
});
jPopupMenu1.add(jMenu1);
jPopupMenu1.add(jMenuItem2);
jPopupMenu1.add(jMenuItem3);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem4);
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuItem1_actionPerformed(e);
}
});
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuItem4_actionPerformed(e);
}
});
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuItem2_actionPerformed(e);
}
});
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuItem3_actionPerformed(e);
}
});
}
//method createNodes to add nodes to the tree
public void createNodes(DefaultMutableTreeNode top) {
//create the items in the tree
DefaultMutableTreeNode node = null;
DefaultMutableTreeNode child = null;
node = new DefaultMutableTreeNode("News");
top.add(node);
child = new DefaultMutableTreeNode("www.sina.com.cn");
node.add(child);
node = new DefaultMutableTreeNode("Sports");
top.add(node);
child = new DefaultMutableTreeNode("www.fifa.com");
node.add(child);
}
.
.
.
.
.
//save the jTree1 to a sys file
void save(){
try{
FileOutputStream fout=new FileOutputStream("address.bat");
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(jTree1);
}catch(Exception ed){}
}
//load the jTree date from the sys file
void load(){
try{
FileInputStream fin=new FileInputStream("address.bat");
ObjectInputStream in=new ObjectInputStream(fin);
jTree1=(JTree)in.readObject();
}catch(Exception er){}
}
.
.
.
.
.
但是重启动后界面不变,sava(),load(),也执行过,load()放在其他地方也有问题
请指教
class Tree{
Tree(){ load();
jTree1=new JTree(top);
//when first time use,init the tree with sample,or init the
//tree with the load date.
createNodes(top);
treemodel=(DefaultTreeModel)jTree1.getModel();
jTree1.setBorder(BorderFactory.createEtchedBorder());
jTree1.setEditable(true);
//set the tree single selected once
jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
jTree1.setRowHeight(26);
//extend the root
jTree1.setShowsRootHandles(true);
//when extend a node, chicked 1 time
jTree1.setToggleClickCount(1);
jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jTree1_mouseClicked(e);
}
});
jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
jTree1_valueChanged(e);
}
});
jPopupMenu1.add(jMenu1);
jPopupMenu1.add(jMenuItem2);
jPopupMenu1.add(jMenuItem3);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem4);
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuItem1_actionPerformed(e);
}
});
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuItem4_actionPerformed(e);
}
});
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuItem2_actionPerformed(e);
}
});
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuItem3_actionPerformed(e);
}
});
}
//method createNodes to add nodes to the tree
public void createNodes(DefaultMutableTreeNode top) {
//create the items in the tree
DefaultMutableTreeNode node = null;
DefaultMutableTreeNode child = null;
node = new DefaultMutableTreeNode("News");
top.add(node);
child = new DefaultMutableTreeNode("www.sina.com.cn");
node.add(child);
node = new DefaultMutableTreeNode("Sports");
top.add(node);
child = new DefaultMutableTreeNode("www.fifa.com");
node.add(child);
}
.
.
.
.
.
//save the jTree1 to a sys file
void save(){
try{
FileOutputStream fout=new FileOutputStream("address.bat");
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(jTree1);
}catch(Exception ed){}
}
//load the jTree date from the sys file
void load(){
try{
FileInputStream fin=new FileInputStream("address.bat");
ObjectInputStream in=new ObjectInputStream(fin);
jTree1=(JTree)in.readObject();
}catch(Exception er){}
}
.
.
.
.
.
但是重启动后界面不变,sava(),load(),也执行过,load()放在其他地方也有问题
请指教
|
没有见表示状态的实例变量的定义,怎摸保存状态,