本帖最后由 王杰 于 2012-4-11 16:50 编辑
- /*用java编写的一个类似Windows记事本的程序。现在已知有个bug,就是新打开的文件你没有修改,如果关闭文档的话,会有对话框提示你记事本已经改变,问你是否要保存,可是我并没有改变文档呀,就只是打开操作而已呀。*/
- package notepad;
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- public class MyNotepad
- {
- private Frame f;
- private MenuBar mbar;
- private Menu m1;
- private MenuItem exit;
- private MenuItem create;
- private MenuItem open;
- private MenuItem save;
- private MenuItem saveAs;
- private TextArea ta;
- private FileDialog saveDia,openDia,saveAsDia;
- private File file = null;
- private String dirPath = null;
- private String fileName = null;
- private String title = "MyNotepad - ";
- private String defaultName = "无标题.txt";
-
- private Dialog box;
- private Panel subbox;
- private Button OK;
- private Button NO;
- private Button back;
- private Label lab;
- private Dialog errorbox;
- private Panel suberrorbox;
- private Label lab2;
- private Button OK2;
- /*
- 自定义信号量:
- 如果textarea(文本域)内有更新,flag_update就设为ture;
- 每次打开、新建、另保存操作后,就设为false;
- 如果新建一个文档,flag_create 就设为ture;新建动作完成后设为false;
- 如果打开一个文档,flag_open 就设为ture;打开动作完成后设为false;
- 如果点击box内的取消(back),flag_back就设为ture;在判断完成后又将flag_设为false;
- ... ...
- */
- private boolean flag_update = false;
- private boolean flag_create = false;
- private boolean flag_open = false;
- private boolean flag_save = false;
- private boolean flag_no = false;
- private boolean flag_back = false;
- MyNotepad()
- {
- init();
- }
-
- void init()
- {
- saveDia = new FileDialog(f,"保存",FileDialog.SAVE);
- saveAsDia = new FileDialog(f,"另存为",FileDialog.SAVE);
- openDia = new FileDialog(f,"打开",FileDialog.LOAD);
- f = new Frame(title + defaultName);
- f.setBounds(400,200,540,420);
- mbar = new MenuBar();
- m1 = new Menu("文件");
- exit = new MenuItem("退出");
- saveAs = new MenuItem("另存为");
- open = new MenuItem("打开 Ctrl+O");
- save = new MenuItem("保存 Ctrl+S");
- create = new MenuItem("新建 Ctrl+N");
- ta = new TextArea();
- f.setMenuBar(mbar);
- f.add(ta);
- mbar.add(m1);
- m1.add(create);
- m1.add(open);
- m1.add(save);
- m1.add(saveAs);
- m1.addSeparator();
- m1.add(exit);
- box = new Dialog(f,"MyNotepad",true);
- box.setBounds(480,300,250,150);
- box.setLayout(new GridLayout(2,1));
- subbox = new Panel(new FlowLayout());
- lab = new Label();
- OK = new Button("保存");
- NO = new Button("不保存");
- back = new Button("取消");
-
- box.add(lab);
- box.add(subbox);
- subbox.add(OK);
- subbox.add(NO);
- subbox.add(back);
- errorbox = new Dialog(f,"MyNotepad - 提示",true);
- errorbox.setLayout(new GridLayout(2,1));
- errorbox.setBounds(480,300,250,150);
- suberrorbox = new Panel(new FlowLayout());
-
- lab2 = new Label();
- OK2 = new Button("确定");
-
- errorbox.add(lab2);
- errorbox.add(suberrorbox);
- suberrorbox.add(OK2);
- noteEvent();//f里的监听器都封装在这里面;
- boxEvent();//对话框里的监听器都封装在这里面。
- f.setVisible(true);
- }
- //对话框监听器的封装函数
- void boxEvent()
- {
- //对关闭对话框监听;
- box.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- flag_back = true;
- box.setVisible(false);
- }
- });
-
复制代码 |
|