日期:2014-05-20  浏览次数:20762 次

谁来看看“另存为”怎么实现嘛
完整代码如下:
/**
*   @(#)notepad.java
*
*
*   @author
*   @version   1.00   2007/4/18
*/

import   java.awt.*;
import   java.awt.Frame;
import   java.awt.event.*;
import   java.io.*;
import   java.awt.event.ActionListener;
import   javax.swing.*;
import   java.awt.Image;
import   java.lang.String;
import   java.awt.Toolkit;
public   class   notepad   {

public   static   void   main   (String[]   args)   {
finalFrame   f=new   Frame( "记事本 ");
f.setBackground(Color.white);
f.setLocation(200,100);
f.setSize(600,600);
f.addWindowListener(new   WindowAdapter()
{
public   void   windowClosing(WindowEvent   e)
{
System.exit(0);
}

}
);
final   TextArea   ta=new   TextArea(200,200);
f.add(ta);
MenuBar   mb=new   MenuBar();
Menu   m1=new   Menu( "文件 ");
Menu   m2=new   Menu( "编辑 ");
MenuItem   mi1=new   MenuItem( "新建 ");
MenuItem   mi2=new   MenuItem( "打开 ");
mi2.addActionListener(new   ActionListener()
{
public   void   actionPerformed(ActionEvent   e)
{
FileDialog   fd=new   FileDialog(f, "打开文件 ",FileDialog.LOAD);
fd.show();
String   strFile=fd.getDirectory()+fd.getFile();
if(strFile!=null)
{
try   {
FileInputStream   fis=new   FileInputStream(strFile);
byte[]   buf=new   byte[10*1024];
int   len=fis.read(buf);
ta.append(new   String(buf,0,len));
fis.close();

}
catch   (Exception   ex)   {
System.out.println(ex.toString());
}
}
}

}
);
MenuItem   mi3=new   MenuItem( "保存 ");
mi3.addActionListener(new   ActionListener()
{
public   void   actionPerformed(ActionEvent   e)
{

try   {
FileDialog   fd2=new   FileDialog(f, "保存 ",FileDialog.SAVE);
}
catch   (Exception   exc)   {
System.out.println(exc.toString());
}

}
}
);
MenuItem   mi4=new   MenuItem( "退出 ");
MenuItem   mi5=new   MenuItem( "另存为 ");
mi5.addActionListener(new   ActionListener()
{
public   void   actionPerformed(ActionEvent   e)
{
try
{


}
catch(Exception   exc2)
{
System.out.println(exc2.toString());
}
}
}
);
mi4.addActionListener(new   ActionListener()
{
public   void   actionPerformed(ActionEvent   e)
{
System.exit(0);
}
}

);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
m1.add(mi5);
mi5.addActionListener(new   ActionListener()
{
public   void   actionPerformed(ActionEvent   e)
{
FileDialog   fd=new   FileDialog(f, "另存为 ",FileDialog.SAVE);
fd.show();
String   strFile=fd.getDirectory()+fd.getFile();
if(strFile!=null)
{
try   {
FileOutputStream   fos=new   FileOutputStream(strFile);
//byte[]   buf=new   byte[10*1024];
//int   len=fos.write(buf);
ta.insert(strFile,0);
fos.close();

}
catch   (Exception   ex)   {
System.out.println(ex.toString());