java问题:怎么将下面的类序列化,然后以对象的形式保存进文件里,就是用writeObject()方法保存
classContentPaneextendsJTextPaneimplementsSerializable{privateToolkittool;privateImag...
class ContentPane extends JTextPane implements Serializable
{
private Toolkit tool;
private Image image;
ContentPane()
{
super.setFont(new Font("楷体",Font.PLAIN,25));
tool=getToolkit();
image=tool.getImage("picture/landing.gif");//面板的背景图
setMargin(new Insets(20,20,20,20));//设置边框空白
validate();
}
private static final long serialVersionUID = -8220994963464909916L;
{
setOpaque(false); // 设置透明
}
//给面板设置图片背景
protected void paintComponent(Graphics g)
{
g.drawImage(null,0,0,getWidth(),getHeight(),this);
super.paintComponent(g);
}
} 展开
{
private Toolkit tool;
private Image image;
ContentPane()
{
super.setFont(new Font("楷体",Font.PLAIN,25));
tool=getToolkit();
image=tool.getImage("picture/landing.gif");//面板的背景图
setMargin(new Insets(20,20,20,20));//设置边框空白
validate();
}
private static final long serialVersionUID = -8220994963464909916L;
{
setOpaque(false); // 设置透明
}
//给面板设置图片背景
protected void paintComponent(Graphics g)
{
g.drawImage(null,0,0,getWidth(),getHeight(),this);
super.paintComponent(g);
}
} 展开
1个回答
2013-03-02
展开全部
Exception in thread "main" java.io.NotSerializableException: sun.awt.image.ToolkitImage!
有IO的,一般不适宜。本例中,Tookit不要定义成类变量。
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*; import java.io.*;
public class SerializableTest {
public static void main(String[]args)throws IOException, ClassNotFoundException{
SerializableTest c=new SerializableTest();
c.testSerializableTest();
}
public void testSerializableTest()throws IOException, ClassNotFoundException{
MyContentPane pane=new MyContentPane();
byte [] data;
ByteArrayOutputStream bout=new ByteArrayOutputStream();
ObjectOutputStream out=new ObjectOutputStream( bout );
out.writeObject(pane);
data=bout.toByteArray();
out.close();
bout.close();
ObjectInputStream in_=new ObjectInputStream( new ByteArrayInputStream(data) );
MyContentPane pane1=(MyContentPane)in_.readObject();
in_.close();
System.out.println(pane1);
}
}
class MyContentPane extends JTextPane implements Serializable{
private Image image;//面板的背景图
public MyContentPane(){
super.setFont(new Font("楷体",Font.PLAIN,25));
Image image=Toolkit.getDefaultToolkit().getImage("picture/landing.gif");
setMargin(new Insets(20,20,20,20));//设置边框空白
validate();
setOpaque(false); // 设置透明
}
private static final long serialVersionUID = -8220994963464909916L;
//给面板设置图片背景
protected void paintComponent(Graphics g){
g.drawImage(null,0,0,getWidth(),getHeight(),this);
super.paintComponent(g);
}
}
有IO的,一般不适宜。本例中,Tookit不要定义成类变量。
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*; import java.io.*;
public class SerializableTest {
public static void main(String[]args)throws IOException, ClassNotFoundException{
SerializableTest c=new SerializableTest();
c.testSerializableTest();
}
public void testSerializableTest()throws IOException, ClassNotFoundException{
MyContentPane pane=new MyContentPane();
byte [] data;
ByteArrayOutputStream bout=new ByteArrayOutputStream();
ObjectOutputStream out=new ObjectOutputStream( bout );
out.writeObject(pane);
data=bout.toByteArray();
out.close();
bout.close();
ObjectInputStream in_=new ObjectInputStream( new ByteArrayInputStream(data) );
MyContentPane pane1=(MyContentPane)in_.readObject();
in_.close();
System.out.println(pane1);
}
}
class MyContentPane extends JTextPane implements Serializable{
private Image image;//面板的背景图
public MyContentPane(){
super.setFont(new Font("楷体",Font.PLAIN,25));
Image image=Toolkit.getDefaultToolkit().getImage("picture/landing.gif");
setMargin(new Insets(20,20,20,20));//设置边框空白
validate();
setOpaque(false); // 设置透明
}
private static final long serialVersionUID = -8220994963464909916L;
//给面板设置图片背景
protected void paintComponent(Graphics g){
g.drawImage(null,0,0,getWidth(),getHeight(),this);
super.paintComponent(g);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询