我写了一个程序,是在文本框中输入了数字,点确定可以保存一个文件,这个文件是保存对像序列化的文件,可 是运行出错 ,程序目的是在文本框中输入内容,点确定下次打打开时文本框的内容是上一次保存的内容.
请高手帮我改一下.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;public class serializableDemo implements Serializable,ActionListener
{
JFrame frame=new JFrame("保存设置");
JTextField text1,text2;
transient JButton button=new JButton("确定");
ObjectOutputStream out;
ObjectInputStream in;
public serializableDemo()
{
Container cp=frame.getContentPane();
cp.setLayout(new GridLayout(1,3));
text1=new JTextField(10);
text2=new JTextField(10);
cp.add(text1);
cp.add(text2);
cp.add(button);
button.addActionListener(this);
frame.setBounds(300,300,200,100);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
button.setEnabled(false);
try{
out=new ObjectOutputStream(new FileOutputStream("p.ser"));
out.writeObject(text1);
out.writeObject(text2);
out.close();
}catch(IOException e0){}
button.setEnabled(true);
}
public void fangfa()
{
String s1,s2;
try{
in=new ObjectInputStream(new FileInputStream("p.ser"));
text1.setText(s1=(String)in.readObject());
text2.setText(s2=(String)in.readObject());
in.close();
}catch(ClassNotFoundException e1){}
catch(IOException e2){}
}
public static void main(String[] args)
{
serializableDemo diao=new serializableDemo();
diao.fangfa();
}
}
请高手帮我改一下.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;public class serializableDemo implements Serializable,ActionListener
{
JFrame frame=new JFrame("保存设置");
JTextField text1,text2;
transient JButton button=new JButton("确定");
ObjectOutputStream out;
ObjectInputStream in;
public serializableDemo()
{
Container cp=frame.getContentPane();
cp.setLayout(new GridLayout(1,3));
text1=new JTextField(10);
text2=new JTextField(10);
cp.add(text1);
cp.add(text2);
cp.add(button);
button.addActionListener(this);
frame.setBounds(300,300,200,100);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
button.setEnabled(false);
try{
out=new ObjectOutputStream(new FileOutputStream("p.ser"));
out.writeObject(text1);
out.writeObject(text2);
out.close();
}catch(IOException e0){}
button.setEnabled(true);
}
public void fangfa()
{
String s1,s2;
try{
in=new ObjectInputStream(new FileInputStream("p.ser"));
text1.setText(s1=(String)in.readObject());
text2.setText(s2=(String)in.readObject());
in.close();
}catch(ClassNotFoundException e1){}
catch(IOException e2){}
}
public static void main(String[] args)
{
serializableDemo diao=new serializableDemo();
diao.fangfa();
}
}
解决方案 »
- 提取网页内容失败
- 急!!!如何让浏览器访问某个ip地址?
- gua
- 外部的程序可以调用ECLIPSE中的插件吗?怎样通过JDT去调用ECLPSE中的插件?
- 如何绘制自定义的component。急......................................................................
- 如何去覆写equals()方法呢?
- 克..克隆..这个克隆究竟有什么用怎么用啊???
- 关于jasperreport问题...大家帮忙啊,急..
- 如何实现当数据库中增添了新数据时,立刻将新产生的数据显示在客户端
- java读取ini文件
- 抽象方法
- 关于for循环里数组赋值的问题
证据贴:http://topic.csdn.net/u/20081004/18/dfd231f4-61f3-4706-bd02-3292cd464cd0.html
http://topic.csdn.net/u/20081001/15/ae497b00-da31-46bb-9899-4bff8b982198.html
so,不回答,并且板砖,^_^