我用AWT做的一个很很简单的记事本.另存为功能做好了,我想问下直接保存那个功能的代码要怎么写啊? 就是点保存时,没有对话框出来,直接保存. 代码如下:
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*; class MainWindow extends Frame implements ActionListener
{
MenuBar menubar;
Menu document,edit;
MenuItem open,close,save,tosave,exit,copy,cut,paste;
Clipboard clipboard=null;
TextArea textarea;
FileDialog fileSave,fileLoad;
BufferedReader bufRead;
FileReader fileRead;
BufferedWriter bufWriter;
FileWriter fileWriter;
MainWindow()
{
super( "文本编辑器 ");
clipboard=getToolkit().getSystemClipboard();//获得系统剪贴板
menubar=new MenuBar();
document=new Menu( "文件 ");
edit=new Menu( "编辑 ");
open=new MenuItem( "打开 ");
close=new MenuItem( "关闭 ");
save=new MenuItem( "保存 ");
tosave=new MenuItem( "另存为 ");
exit=new MenuItem( "退出 ");
copy=new MenuItem( "复制 ");
cut=new MenuItem( "剪切 ");
paste=new MenuItem( "粘贴 ");
textarea=new TextArea();
fileSave=new FileDialog(this, "保存文件对话框 ",FileDialog.SAVE);
fileLoad=new FileDialog(this, "打开文件对话框 ",FileDialog.LOAD);
fileSave.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
fileSave.setVisible(false);
}
}
);
fileLoad.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
fileLoad.setVisible(false);
}
}
);
this.open.addActionListener(this);
this.tosave.addActionListener(this);
this.copy.addActionListener(this);
this.cut.addActionListener(this);
this.paste.addActionListener(this);
this.exit.addActionListener(this);
this.save.addActionListener(this);
setLayout(new FlowLayout());
document.add(open);document.add(save);document.add(tosave);
document.add(close);document.add(exit);
edit.add(copy);edit.add(cut);edit.add(paste);
menubar.add(document);menubar.add(edit);
setMenuBar(menubar);
add(textarea);
setBounds(200,150,500,450);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==copy) //实现复制功能
{
String selected=textarea.getSelectedText();
StringSelection text=new StringSelection(selected);
clipboard.setContents(text,null);
}
else if(e.getSource()==cut) //实现剪切功能
{
String selected=textarea.getSelectedText();
StringSelection text=new StringSelection(selected);
clipboard.setContents(text,null);
int start=textarea.getSelectionStart();
int end=textarea.getSelectionEnd();
textarea.replaceRange( " ",start,end);
}
else if(e.getSource()==paste) //实现粘贴功能
{
Transferable contents=clipboard.getContents(this);
DataFlavor flavor= DataFlavor.stringFlavor;
if( contents.isDataFlavorSupported(flavor))
try{
String str;
str=(String)contents.getTransferData(flavor);
textarea.append(str);//将剪贴板内容作为字符串添加到文本区中
}
catch(Exception ee)
{
}
}
else if(e.getSource()==open) //实现打开文件
{
fileLoad.setVisible(true);
this.textarea.setText(null);
String s;
if(fileLoad.getFile()!=null)
{
try
{
File file=new File(fileLoad.getDirectory(),fileLoad.getFile());
fileRead=new FileReader(file);
bufRead=new BufferedReader(fileRead);
while((s=bufRead.readLine())!=null)
{
this.textarea.append(s+ '\n ');
}
bufRead.close();
fileRead.close();
}
catch(IOException e1)
{
}
}
}
else if(e.getSource()==tosave) //实现另存为功能
{
fileSave.setVisible(true);
if(fileSave.getFile()!=null)
{
try
{
File file=new File(fileSave.getDirectory(),fileSave.getFile());
fileWriter=new FileWriter(file);
bufWriter=new BufferedWriter(fileWriter);
bufWriter.write(this.textarea.getText(),0,(textarea.getText()).length());
bufWriter.flush();
bufWriter.close();
fileWriter.close();
}
catch(IOException e2)
{}
}
}
else if(e.getSource()==this.exit) //退出
{
System.exit(0);
}
else if(e.getSource()==save) //保存功能。这个该怎么写呀?
{
} }
}
public class WindowMain
{
public static void main(String args[])
{
MainWindow mw=new MainWindow();
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*; class MainWindow extends Frame implements ActionListener
{
MenuBar menubar;
Menu document,edit;
MenuItem open,close,save,tosave,exit,copy,cut,paste;
Clipboard clipboard=null;
TextArea textarea;
FileDialog fileSave,fileLoad;
BufferedReader bufRead;
FileReader fileRead;
BufferedWriter bufWriter;
FileWriter fileWriter;
MainWindow()
{
super( "文本编辑器 ");
clipboard=getToolkit().getSystemClipboard();//获得系统剪贴板
menubar=new MenuBar();
document=new Menu( "文件 ");
edit=new Menu( "编辑 ");
open=new MenuItem( "打开 ");
close=new MenuItem( "关闭 ");
save=new MenuItem( "保存 ");
tosave=new MenuItem( "另存为 ");
exit=new MenuItem( "退出 ");
copy=new MenuItem( "复制 ");
cut=new MenuItem( "剪切 ");
paste=new MenuItem( "粘贴 ");
textarea=new TextArea();
fileSave=new FileDialog(this, "保存文件对话框 ",FileDialog.SAVE);
fileLoad=new FileDialog(this, "打开文件对话框 ",FileDialog.LOAD);
fileSave.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
fileSave.setVisible(false);
}
}
);
fileLoad.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
fileLoad.setVisible(false);
}
}
);
this.open.addActionListener(this);
this.tosave.addActionListener(this);
this.copy.addActionListener(this);
this.cut.addActionListener(this);
this.paste.addActionListener(this);
this.exit.addActionListener(this);
this.save.addActionListener(this);
setLayout(new FlowLayout());
document.add(open);document.add(save);document.add(tosave);
document.add(close);document.add(exit);
edit.add(copy);edit.add(cut);edit.add(paste);
menubar.add(document);menubar.add(edit);
setMenuBar(menubar);
add(textarea);
setBounds(200,150,500,450);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==copy) //实现复制功能
{
String selected=textarea.getSelectedText();
StringSelection text=new StringSelection(selected);
clipboard.setContents(text,null);
}
else if(e.getSource()==cut) //实现剪切功能
{
String selected=textarea.getSelectedText();
StringSelection text=new StringSelection(selected);
clipboard.setContents(text,null);
int start=textarea.getSelectionStart();
int end=textarea.getSelectionEnd();
textarea.replaceRange( " ",start,end);
}
else if(e.getSource()==paste) //实现粘贴功能
{
Transferable contents=clipboard.getContents(this);
DataFlavor flavor= DataFlavor.stringFlavor;
if( contents.isDataFlavorSupported(flavor))
try{
String str;
str=(String)contents.getTransferData(flavor);
textarea.append(str);//将剪贴板内容作为字符串添加到文本区中
}
catch(Exception ee)
{
}
}
else if(e.getSource()==open) //实现打开文件
{
fileLoad.setVisible(true);
this.textarea.setText(null);
String s;
if(fileLoad.getFile()!=null)
{
try
{
File file=new File(fileLoad.getDirectory(),fileLoad.getFile());
fileRead=new FileReader(file);
bufRead=new BufferedReader(fileRead);
while((s=bufRead.readLine())!=null)
{
this.textarea.append(s+ '\n ');
}
bufRead.close();
fileRead.close();
}
catch(IOException e1)
{
}
}
}
else if(e.getSource()==tosave) //实现另存为功能
{
fileSave.setVisible(true);
if(fileSave.getFile()!=null)
{
try
{
File file=new File(fileSave.getDirectory(),fileSave.getFile());
fileWriter=new FileWriter(file);
bufWriter=new BufferedWriter(fileWriter);
bufWriter.write(this.textarea.getText(),0,(textarea.getText()).length());
bufWriter.flush();
bufWriter.close();
fileWriter.close();
}
catch(IOException e2)
{}
}
}
else if(e.getSource()==this.exit) //退出
{
System.exit(0);
}
else if(e.getSource()==save) //保存功能。这个该怎么写呀?
{
} }
}
public class WindowMain
{
public static void main(String args[])
{
MainWindow mw=new MainWindow();
}
}
我给你个思路,你可以设置一个标志位,随便设,来标明当前文件为新文件.比如boolean isNewFile=true; 增加一个tempFile临时存放当前文件:File tempFile;(当然,这是针对你的代码而写的,如果是我写记事本,我可能不会用到,因为每个人的思路不同嘛)
然后当触发保存 if(e.getSource()==save){
if(isNewFile){ //此文件是新创建文件,执行另存为操作.
fileSave.setVisible(true);
if(fileSave.getFile()!=null){
try
{
File file=new File(fileSave.getDirectory(),fileSave.getFile());
tempFile=file; //通过tempFile存放当前文件. fileWriter=new FileWriter(file);
bufWriter=new BufferedWriter(fileWriter);
bufWriter.write(this.textarea.getText(),0,(textarea.getText()).length());
bufWriter.flush();
bufWriter.close();
fileWriter.close(); isNewFile=false; //将标记置为false,则再次选择"保存"选项时不会弹出"另存为"界面.
}
catch(IOException e2)
{}
}
}
else{ //此文件是已存在文件,执行保存操作.
try{
File saveFileName=tempFile; //将fileWriter指定为tempFile所保存的文件,即当前文件.我这多用了些File类实例,目的是为了看得清楚些,也可以不要saveFileName,直接FileWriter(tempFile);
fileWriter=new FileWriter(saveFileName);
bufWriter=new BufferedWriter(fileWriter);
bufWriter.write(this.textarea.getText(),0,(textarea.getText()).length());
bufWriter.flush();
bufWriter.close();
fileWriter.close();
}catch(Exception ee){
ee.printStackTrace();
}
}
}还要在"打开"功能相关代码里加上 tempFile=file;isNewFile=false;
我自己运行了下,可以了,点击"保存"选项不会再弹出另存框了.打开相应的文件,内容已经更改.
如果新建的,那么这个路径为null
如果是打开的,那么这个路径为打开文件时文件的路径选保存的时候,如果路径为null,则显示对话框,相当于另存为
如果路径不为null,则按照此路径来保存文件内容