文本编辑器-java图形界面

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class TextEditor extends Frame implements ActionListener{


private static final long serialVersionUID = 1L;
FileDialog fileDlg;
String str,fileName;
byte byteBuf[]=new byte[10000];
TextArea ta = new TextArea();
MenuBar mb = new MenuBar();
Menu ml = new Menu("Menu");
MenuItem open = new MenuItem("open");
MenuItem close = new MenuItem("clear");
MenuItem save = new MenuItem("save");
MenuItem exit = new MenuItem("exit");
@SuppressWarnings("deprecation")
TextEditor(){
setTitle("TextEditor");
setSize(600,400);
add("Center",ta);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}});
ml.add(open);
ml.add(close);
ml.add(save);
ml.addSeparator();
ml.add(exit);
open.addActionListener(this);
save.addActionListener(this);
close.addActionListener(this);
exit.addActionListener(this);
mb.add(ml);
setMenuBar(mb);
show();
}




@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==exit)
System.exit(0);//
else if(e.getSource()==close)
ta.setText(null);
else if(e.getSource()==open)
{
fileDlg = new FileDialog(this,"OpenFile");
fileDlg.show();
fileName = fileDlg.getFile();
try {
FileInputStream in = new FileInputStream(fileName);
in.read(byteBuf);
in.close();
str = new String (byteBuf);
ta.setText(str);
setTitle("TextEditor-"+fileName);
}catch(IOException ioe){}
}
else if(e.getSource()==save){
fileDlg= new FileDialog(this,"saveFile",FileDialog.SAVE);
fileDlg.show();
fileName = fileDlg.getFile();
str = ta.getText();
byteBuf = str.getBytes();
try{
FileOutputStream out = new FileOutputStream(fileName);
out.write(byteBuf);
out.close();
}catch (IOException ioe){}
}
}
public static void main (String args[]){
new TextEditor();
}
}

相关文档
最新文档