IOException的具体是什么呢?
记得以前俺遇到过说image无法序列化的问题
记得以前俺遇到过说image无法序列化的问题
解决方案 »
- SWT写的聊天程序为什么卡死了
- 问java的反射。如何判断类里面是否定义了我指定的字段。
- MessageFormat格式化数字问题
- 新手的一个疑问
- log4j
- 为什么啊,搞不明白,怎么出来负数了呢?汗```````
- servlet这个包在那个目录下
- 有关native调用的问题
- private void arrang(int a[n])这样可以吗,如果不,怎么解决?
- 老天!新学JBuilder,简直一头雾水,请问各位:用它生成的应用程序在Windows中怎样执行?
- 如何把网络文件下载到本地
- 求思路:扩展JTable以实现不规则表头的问题(可用任何已有ide或开源包,b/s体系,tomcat4.1,jb9)
output=new ObjectOutputStream(
new FileOutputStream( fileName ));
output.writeObject(work);//调用这一句的问题,具体什么原因下午我看看
output.flush();
output.close();
}
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this, "Error Opening File",
"Error", JOptionPane.ERROR_MESSAGE );
}
workpanel1.java:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;public class workpanel extends JPanel implements MouseMotionListener,MouseListener
{
//建立向量用来存储当前鼠标点击的点坐标
private Vector vec;
//建立向量用来存储通过鼠标点击的点所绘制的矩形
private Vector rec;
final private Dimension ds; public workpanel()
{
vec = new Vector();
rec=new Vector();
ds=new Dimension(4,4);
addMouseListener(this);
addMouseMotionListener(this);
} public void mouseClicked(MouseEvent event)
{
System.out.println("当鼠标点击时绘制窗口"); Point point=event.getPoint();//取得鼠标点击点
vec.add(point);
point.setLocation(point.getX()-2,point.getY()-2); Rectangle rect=new Rectangle(point,ds);//创建矩形对象
rec.add(rect); repaint();//注意这个很重要,从java.awt.Component继承下来的方法,重新绘制panel区域
} public void paintComponent(Graphics g)
{
System.out.println("准备调用paintComponent"); //这个是系统自动调用的一个方法,负责每次该窗口在windows中改变时,重新绘制窗口
super.paintComponent(g);//这行代码都做了什么?拷贝重新绘制区域
Graphics2D g2 = (Graphics2D) g; for (int i = 0; i < rec.size(); i++)
{
Rectangle temrec=(Rectangle)rec.get(i);//取的对象
g2.fill(temrec);//绘制对象
}
}
//下面的这些可以不要
public void mouseReleased(MouseEvent event)
{
}
public void mousePressed(MouseEvent event)
{
}
public void mouseMoved(MouseEvent event)
{
}
public void mouseExited(MouseEvent event)
{
}
public void mouseEntered(MouseEvent event)
{
}
public void mouseDragged(MouseEvent event)
{
}
}
workpanel1.java:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;public class workpanel extends JPanel implements MouseMotionListener,MouseListener
{
//建立向量用来存储当前鼠标点击的点坐标
private Vector vec;
//建立向量用来存储通过鼠标点击的点所绘制的矩形
private Vector rec;
final private Dimension ds; public workpanel()
{
vec = new Vector();
rec=new Vector();
ds=new Dimension(4,4);
addMouseListener(this);
addMouseMotionListener(this);
} public void mouseClicked(MouseEvent event)
{
System.out.println("当鼠标点击时绘制窗口"); Point point=event.getPoint();//取得鼠标点击点
vec.add(point);
point.setLocation(point.getX()-2,point.getY()-2); Rectangle rect=new Rectangle(point,ds);//创建矩形对象
rec.add(rect); repaint();//注意这个很重要,从java.awt.Component继承下来的方法,重新绘制panel区域
} public void paintComponent(Graphics g)
{
System.out.println("准备调用paintComponent"); //这个是系统自动调用的一个方法,负责每次该窗口在windows中改变时,重新绘制窗口
super.paintComponent(g);//这行代码都做了什么?拷贝重新绘制区域
Graphics2D g2 = (Graphics2D) g; for (int i = 0; i < rec.size(); i++)
{
Rectangle temrec=(Rectangle)rec.get(i);//取的对象
g2.fill(temrec);//绘制对象
}
}
public void setVec(Object vec)
{
this.vec=(Vector)vec;
}
public void setRec(Object rec)
{
this.rec=(Vector)rec;
}
public Vector getVec()
{
return vec;
}
public Vector getRec()
{
return rec;
} //下面的这些可以不要
public void mouseReleased(MouseEvent event)
{
}
public void mousePressed(MouseEvent event)
{
}
public void mouseMoved(MouseEvent event)
{
}
public void mouseExited(MouseEvent event)
{
}
public void mouseEntered(MouseEvent event)
{
}
public void mouseDragged(MouseEvent event)
{
}
}
frame1.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;public class frame1 extends JFrame implements Serializable
{
private workpanel work;//建立工作区对象
private ObjectInputStream input;//读取文件的流对象
private ObjectOutputStream output;//存储文件的流对象 public frame1()
{
work=new workpanel();
//建立绘图容器,并制定布局管理器
Container c=getContentPane();
c.setLayout(new BorderLayout());
c.add(work); //创建Munubar对象
JMenuBar bar=new JMenuBar();
setJMenuBar(bar);
//创建Menu对象,命名为file
JMenu zzz=new JMenu("file");
bar.add(zzz);
//创建Menu下的各项
JMenuItem open=new JMenuItem("open");
open.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openfile();
}
}
);
zzz.add(open);//open菜单完成 JMenuItem save=new JMenuItem("save");
save.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
savefile();
}
}
);
zzz.add(save);//save菜单完成 setSize(300,300);
show();//显示窗口完成
} //保存文件的方法
private void savefile()
{
HashMap hp=new HashMap();
hp.put("1",work.getVec());
hp.put("2",work.getRec()); //保存文件目录对话框对象
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );//只允许保存为文件
int result = fileChooser.showSaveDialog( this );//显示
if( result == JFileChooser.CANCEL_OPTION )
{
return ;
} //取得要包村的文件路径及名称
File fileName = fileChooser.getSelectedFile();
if ( fileName == null || fileName.getName().equals( "" ))
{
JOptionPane.showMessageDialog( this, "Invalid File Name",
"Bad File Name", JOptionPane.ERROR_MESSAGE );
return;
}
//准备输出文件内容
try
{
output=new ObjectOutputStream(
new FileOutputStream( fileName ) );
System.out.println("准备调用writeObject方法");
System.out.println(work);
//output.writeObject(work);//这句话有问题
output.writeObject(hp);//这句话有问题
System.out.println("---");
output.flush(); output.close();
}
catch ( IOException ioException )
{
JOptionPane.showMessageDialog( this, "Error Saving File",
"Error", JOptionPane.ERROR_MESSAGE );
ioException.printStackTrace();
}
}
//打开文件的方法
private void openfile()
{
//创建文件选取文件夹
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
int result = fileChooser.showOpenDialog( this );
if ( result == JFileChooser.CANCEL_OPTION )
{
return ;
}
//取的文件路经和名称
File fileName = fileChooser.getSelectedFile();
if ( fileName == null || fileName.getName().equals( "" ) )
{
JOptionPane.showMessageDialog( this, "Invalid File Name",
"Bad File Name", JOptionPane.ERROR_MESSAGE );
return;
}
//准备读取显示数据
try
{
input=new ObjectInputStream(
new FileInputStream( fileName ));
HashMap hp=(HashMap)input.readObject();
work.setVec(hp.get("1"));
work.setRec(hp.get("2"));
input.close();
}
catch ( IOException ioException )
{
JOptionPane.showMessageDialog( this, "Error Opening File",
"Error", JOptionPane.ERROR_MESSAGE );
}
catch( ClassNotFoundException xsds)
{
JOptionPane.showMessageDialog( this, "class not exits",
"Error", JOptionPane.ERROR_MESSAGE );
}
}
//显示的主方法
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e)
{
System.out.println("设置查找感应类出错");
}
frame1 frame1 = new frame1();
}
}