本帖最后由 刘旭 于 2011-12-29 16:55 编辑
//近一下午写的记事本(模仿windows记事本新建、保存、另存为、退出、打开功能),请大家帮忙找找bug,并改改下面两个错误
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Notebook {
public static void main(String[] args) {
new MyNotebook().init();
}
}
class MyNotebook {
public void init() {
/****** 界面初始化开始 ******/
Frame frame = new Frame("My Notebook");
TextArea ta = new TextArea();
MenuBar menubar = new MenuBar();
Menu mwj = new Menu("文件");
MenuItem wj_xj = new MenuItem("新建");
MenuItem wj_dk = new MenuItem("打开");
MenuItem wj_bc = new MenuItem("保存");
MenuItem wj_lc = new MenuItem("另存为");
MenuItem wj_tc = new MenuItem("退出");
Menu mbj = new Menu("编辑");
Menu mgs = new Menu("格式");
Menu mck = new Menu("查看");
Menu mbz = new Menu("帮助");
mbj.add(new MenuItem("功能暂时未定……"));
mgs.add(new MenuItem("功能暂时未定……"));
mck.add(new MenuItem("功能暂时未定……"));
mbz.add(new MenuItem("功能暂时未定……"));
frame.setVisible(true);
frame.setLocation(350, 150);
frame.setSize(600, 400);
frame.setMenuBar(menubar);
frame.add(ta);
menubar.add(mwj);
menubar.add(mbj);
menubar.add(mgs);
menubar.add(mck);
menubar.add(mbz);
mwj.add(wj_xj);
mwj.add(wj_dk);
mwj.add(wj_bc);
mwj.add(wj_lc);
mwj.add(wj_tc);
/****** 界面初始化结束 ******/
/****** 事件监听编辑开始 ******/
frame.addWindowListener(new MyEvemt().getMyWindowListener_close());
wj_tc.addActionListener(new MyEvemt().getActionListener_close());
wj_dk.addActionListener(new MyEvemt().getActionListener_openFile(frame, ta));
wj_bc.addActionListener(new MyEvemt().getActionListener_saveFile(frame, ta));
wj_lc.addActionListener(new MyEvemt().getActionListener_saveAsNewFile(frame, ta));
wj_xj.addActionListener(new MyEvemt().getActionListener_createNewFile(ta));
/****** 事件监听编辑结束 ******/
}
}
class MyEvemt {
WindowListener getMyWindowListener_close() {
return new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
}
ActionListener getActionListener_close() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
}
ActionListener getActionListener_openFile(final Frame frame, final TextArea ta) {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
FileTools.openFile(frame, ta);
}
};
}
ActionListener getActionListener_saveAsNewFile(final Frame frame, final TextArea ta) {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
FileTools.saveAsNewFile(frame, ta);
}
};
}
ActionListener getActionListener_saveFile(final Frame frame, final TextArea ta) {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
FileTools.saveFile(frame, ta);
}
};
}
ActionListener getActionListener_createNewFile(final TextArea ta){
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
FileTools.createNewFile(ta);
}
};
}
}
class FileTools {
static boolean isNewFile=true;
static File openFile=null;
static void createNewFile(TextArea ta){
isNewFile=true;
openFile=null;
ta.setText("");
}
static void openFile(Frame frame, TextArea ta) {
FileDialog fdOpen = new FileDialog(frame, "打开", FileDialog.LOAD);
fdOpen.setVisible(true);
FileReader fr = null;
String fdir = fdOpen.getDirectory();
String fname = fdOpen.getFile();
if ((fdir == null) || (fname == null)) {
System.out.println("错误路径:" + fdir + " 文件:" + fname);
return;
}
openFile = new File(fdir, fname);
isNewFile=false;
try {
fr = new FileReader(openFile);
char ch[] = new char[1024];
int len = 0;
ta.setText("");
while ((len = fr.read(ch)) != -1) {
ta.append(new String(ch, 0, len));
}
} catch (Exception e) {
} finally {
try {
if (fr != null)
fr.close();
} catch (IOException e) {
}
}
}
static void saveFile(Frame frame, TextArea ta){
if(isNewFile==true){
saveAsNewFile(frame, ta);
System.out.println("isNewFile");
}
else{
//为什么此处false改为true时, Dialog对话框显示不正确????
final Dialog d=new Dialog(frame,"提示",false);
d.setBounds(600, 300, 150, 95);
d.setLayout(new FlowLayout());
Label l=new Label("是否覆盖原文件内容?");
Button yes=new Button("Yes");
Button no=new Button("No");
d.setVisible(true);
d.add(l);
d.add(yes);
d.add(no);
d.setResizable(false);
// d.setModal(true);//当初始化d没有设置模式时,为什么此处设置的模式没效果???
final TextArea t=ta;
yes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFcontent(openFile.toString(), t.getText());
d.setVisible(false);
}
});
no.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setVisible(false);
}
});
}
}
static void saveAsNewFile(Frame frame, TextArea ta){
// System.out.println("保存");
FileDialog fdOpen = new FileDialog(frame, "保存", FileDialog.SAVE);
fdOpen.setVisible(true);
String fdir = fdOpen.getDirectory();
String fname = fdOpen.getFile();
if ((fdir == null) || (fname == null)) {
System.out.println("错误路径:" + fdir + " 文件:" + fname);
return;
}
saveFcontent(fdir+"\\"+fname, ta.getText());
}
private static void saveFcontent(String fdirAndfname,String fcontent) {
FileWriter fw=null;
try {
fw=new FileWriter(fdirAndfname);
fw.write(fcontent);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fw!=null)
fw.close();
} catch (IOException e) {
}
}
}
}
|