如何用Java Swing做模版?(十万火急!!!!!!)

rox 2002-04-26 11:07:00
请问各位大虾,如何用Java做模版?
就是,我要在一个textarea或label中加入图片和文字。
并且,文字有它的格式(颜色、大小、字体及有无黑体、斜体、下划线等)。
如何将它们保存下来,无论是文件还是数据库。但要求读出时,所有的图片和格式均保留!
JDK中有一个类似的例子:Stylepad.jar。
好像只有用ObjectOutput将Document作为对象直接进行读写,一般都还好。
但是只要有中文就会出错,抛出异常:
写入时的异常:
IOException: java.lang.Object
读出时的异常:
IOException: writing aborted; java.io.NotSerializableException: java.lang.Object
那位清楚呀?
谢谢!
十万火急!!!!!!
公司需要写个这样的东西!
希望给我高手指点迷津!
谢谢!
...全文
132 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rox 2002-04-26
  • 打赏
  • 举报
回复
对了,你的那段程序在我中文系统的环境下怎么出错了!
java.io.NotSerializableException: java.lang.Object
我都搞不懂了,难道是SUN对中文的支持问题?
怎么会日文环境可以,而中文不行?!
麻烦看看,谢谢!
rox 2002-04-26
  • 打赏
  • 举报
回复
//读出文件中对象的类:
class OpenAction extends AbstractAction {

OpenAction() {
super(openAction);
}

public void actionPerformed(ActionEvent e) {
Frame frame = getFrame();
if (fileDialog == null) {
fileDialog = new FileDialog(frame);
}
fileDialog.setMode(FileDialog.LOAD);
fileDialog.show();

String file = fileDialog.getFile();
if (file == null) {
return;
}
String directory = fileDialog.getDirectory();
File f = new File(directory, file);
if (f.exists()) {
try {
FileInputStream fin = new FileInputStream(f);
ObjectInputStream istrm = new ObjectInputStream(fin);
Document doc = (Document) istrm.readObject();
if(getEditor().getDocument() != null)
getEditor().getDocument().removeUndoableEditListener
(undoHandler);
getEditor().setDocument(doc);
doc.addUndoableEditListener(undoHandler);
resetUndoManager();
frame.setTitle(file);
validate();
} catch (IOException io) {
// should put in status panel
System.err.println("IOException: " + io.getMessage());
} catch (ClassNotFoundException cnf) {
// should put in status panel
System.out.println("Read Errors!");
System.err.println("Class not found: " + cnf.getMessage());
}
} else {
// should put in status panel
System.err.println("No such file: " + f);
}
}
}

//将对象写入文件的类:
class SaveAction extends AbstractAction {

SaveAction() {
super(saveAction);
}

public void actionPerformed(ActionEvent e) {
Frame frame = getFrame();
if (fileDialog == null) {
fileDialog = new FileDialog(frame);
}
fileDialog.setMode(FileDialog.SAVE);
fileDialog.show();
String file = fileDialog.getFile();
if (file == null) {
return;
}
String directory = fileDialog.getDirectory();
File f = new File(directory, file);
try {
FileOutputStream fstrm = new FileOutputStream(f);
ObjectOutput ostrm = new ObjectOutputStream(fstrm);
//ByteArrayOutputStream bastrm = new ByteArrayOutputStream()
ostrm.writeObject(getEditor().getDocument());
ostrm.flush();
} catch (IOException io) {
// should put in status panel
System.out.println("Write Errors!");
System.err.println("IOException: " + io.getMessage());
}
}
}

另:我现在就是在JDK的Sample中的Stylepad.jar中打包的几个Java文件基础上进行修改,这个类也是现成的,不是我写的。所以……
惭愧!希望能够指点迷津。
rox 2002-04-26
  • 打赏
  • 举报
回复
你的方法好像还要自己根据属性参数来重新画图,可以修改恢复对象程序来改变对象的大小和颜色属性。

我所希望的把这些东西存为对象后,读出对象,将对象直接输出,不用在乎它的属性会不会被修改!
lianyunzxp 2002-04-26
  • 打赏
  • 举报
回复
下面这个例子就序列化包含日文的对象,没问题的(不好意思,我的系统是日文的)
我感觉应该是你的Document的中包含有没有继承serialable接口的对象
如果不是的话,你可以把代码贴出来,我帮你看一下
import java.io.*;
import javax.swing.*;
public class test{
JTextField tf = new JTextField("核");
public static void main(String [] arg){
test test1 = new test();
test1.invoke();
}
public void invoke(){
try{
ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("1.dat"));
os.writeObject(tf);
os.flush();
os.close();
System.out.println("Before Serial" + tf.getText());
System.out.println("****************************");
ObjectInputStream in= new ObjectInputStream(new FileInputStream("1.dat"));
JTextField tfnew = (JTextField)in.readObject();
in.close();
System.out.println("After Serial" + tfnew.getText());
}catch(Exception e){}
}
}
lianyunzxp 2002-04-26
  • 打赏
  • 举报
回复
Document中是不是有不能序列化的对象呀
参照http://www.csdn.net/expert/topic/637/637704.xml?temp=.5445368
好象是没问题

62,634

社区成员

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

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