日期:2014-05-20 浏览次数:20952 次
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if ("退出".equals(cmd)) {
if (wasSaved == false) {
int op = JOptionPane.showConfirmDialog(jf, "文件没保存是否退出?",
"确认退出", JOptionPane.YES_NO_CANCEL_OPTION);
if (op != JOptionPane.YES_OPTION) {
return;
}
}
System.exit(0);
}
if ("打开".equals(cmd)) {
FileDialog fd = new FileDialog(jf, "打开文件");
fd.setVisible(true);
filePath = fd.getDirectory() + fd.getFile();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath), "utf-8"));
String str;
while((str=br.readLine())!=null){
jta.append(str+"\n");
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if(br!=null){
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
wasSaved=true;
}
if ("保存".equals(cmd)) {
if(filePath==null){
FileDialog fd=new FileDialog(jf);
fd.setVisible(true);
filePath=fd.getDirectory()+fd.getFile();
}
save();
wasSaved=true;
}
if ("另存为".equals(cmd)) {
FileDialog fd=new FileDialog(jf);
fd.setVisible(true);
filePath=fd.getDirectory()+fd.getFile();
save();
wasSaved=true;
}
}