文本编辑器-java图形界面

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();
}
}

相关文档
最新文档