黑马程序员技术交流社区
标题:
java笔记本一个小bug,和许多没有发现的bug,求高手解析
[打印本页]
作者:
王杰
时间:
2012-4-11 16:46
标题:
java笔记本一个小bug,和许多没有发现的bug,求高手解析
本帖最后由 王杰 于 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);
}
});
复制代码
作者:
王杰
时间:
2012-4-11 16:48
errorbox.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
errorbox.setVisible(false);
}
});
//对“取消”按钮监听
back.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
flag_back = true;
box.setVisible(false);
}
});
back.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
flag_back = true;
box.setVisible(false);
}
}
});
//对保存按钮监听;
OK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
flag_save = true;
save(false);
}
});
OK.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
flag_save = true;
save(false);
}
}
});
//对errorbox里的“确定”按钮监听
OK2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
errorbox.setVisible(false);
}
});
OK2.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
errorbox.setVisible(false);
}
});
//对box里的不保存按钮监听;
NO.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
box.setVisible(false);
flag_no = true;
if(flag_create == true || flag_open == true)
return ;
System.exit(0);
}
});
NO.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
box.setVisible(false);
flag_no = true;
if(flag_create == true || flag_open == true)
return ;
System.exit(0);
}
}
});
}
//f的事件监听封装函数;
void noteEvent()
{
//对关闭f监听;
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
if(flag_update == true)
{
message();
if(flag_back ==true)
{
flag_back = false;
box.setVisible(false);
return ;
}
if(flag_no == true)
{
flag_no = false;
System.exit(0);
}
if(flag_save == true)
{
flag_save = false;
if(fileName==null || dirPath==null)
{
return ;
}
}
}
System.exit(0);
}
});
//对菜单里的“退出”监听
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(flag_update == true)
{
message();
if(flag_back ==true)
{
flag_back = false;
return ;
}
if(flag_no == true)
{
flag_no = false;
System.exit(0);
}
if(flag_save == true)
{
flag_save = false;
if(fileName==null || dirPath==null)
{
return ;
}
}
}
System.exit(0);
}
});
//对文本域进行改动监听;
ta.addTextListener(new TextListener()
{
public void textValueChanged(TextEvent e)
{
flag_update = true;
}
});
//下面的都是对菜单里的item进行监听。并且设置了快捷键监听;
ta.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(e.isControlDown() && key == KeyEvent.VK_N)
{
flag_create = true;
if(flag_update == true)
{
message();
if(flag_back == true)
{
flag_back = false;
return ;
}
if(flag_no == true)
{
flag_no = false;
create();
return ;
}
if(flag_save == true)
{
flag_save = false;
if(fileName==null || dirPath==null)
{
return ;
}
}
}
create();
}
}
});
复制代码
作者:
王杰
时间:
2012-4-11 16:48
ta.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(e.isControlDown() && key==KeyEvent.VK_O)
{
flag_open = true;
if(flag_update == true)
{
message();
if(flag_back == true)
{
flag_back = false;
return ;
}
if(flag_no == true)
{
flag_no = false;
open();//调用已经封装好的打开操作;
//以下的message、save、saveAs、create等都是已经自定义封装好的。可以直接调用;
return ;
}
if(flag_save == true)
{
flag_save = false;
if(fileName==null || dirPath==null)
{
return ;
}
}
}
open();
}
}
});
ta.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(e.isControlDown() && key==KeyEvent.VK_S)
{
save(true);
}
}
});
create.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
flag_create = true;
if(flag_update == true)
{
message();
if(flag_back == true)
{
flag_back = false;
return ;
}
if(flag_no == true)
{
flag_no = false;
create();
return ;
}
if(flag_save == true)
{
flag_save = false;
if(fileName==null || dirPath==null)
{
return ;
}
}
}
create();
}
});
open.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
flag_open = true;
if(flag_update==true)
{
message();
if(flag_back == true)
{
flag_back = false;
return ;
}
if(flag_no == true)
{
flag_no = false;
open();
return ;
}
if(flag_save == true)
{
flag_save = false;
if(fileName==null || dirPath==null)
{
return ;
}
}
}
open();
}
});
save.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
save(true);
}
});
saveAs.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
saveAs();
}
});
}
//对保存操作进行封装;
void save(boolean mode)
{
if(file==null)
{
saveDia.setVisible(true);
dirPath = saveDia.getDirectory();
fileName = saveDia.getFile();
if(fileName==null || dirPath==null)
{
f.setTitle(title + defaultName);
if(mode)
return;
else
{
box.setVisible(false);
return;
}
}
if(fileName.lastIndexOf(".")==-1)
fileName = fileName + ".txt";
f.setTitle(title + fileName);
file = new File(dirPath,fileName);
}
BufferedWriter bfw = null;
try
{
bfw = new BufferedWriter(new FileWriter(file));
String text = ta.getText();
bfw.write(text);
}
catch (IOException e1)
{
throw new RuntimeException("文件保存失败");
}
finally
{
if(bfw!=null)
{
try
{
bfw.close();
}
catch (IOException e2)
{
throw new RuntimeException("资源关闭失败");
}
}
}
if(!mode)
box.setVisible(false);
flag_update = false ;
flag_create = false ;
flag_open = false ;
flag_save = false ;
}
void create()
{
file = null;
fileName = null;
f.setTitle(title + defaultName);
ta.setText("");
flag_update = false ;
flag_create = false ;
flag_open = false ;
flag_save = false ;
}
//对另存为操作进行封装;
void saveAs()
{
saveAsDia.setVisible(true);
dirPath = saveAsDia.getDirectory();
fileName = saveAsDia.getFile();
if(fileName==null || dirPath==null)
return ;
if(fileName.lastIndexOf(".")==-1)
fileName = fileName + ".txt";
f.setTitle(title + fileName);
file = new File(dirPath,fileName);
BufferedWriter bfw = null;
try
{
bfw = new BufferedWriter(new FileWriter(file));
String text = ta.getText();
bfw.write(text);
}
catch (IOException e1)
{
throw new RuntimeException("文件保存失败");
}
finally
{
if(bfw!=null)
{
try
{
bfw.close();
}
catch (IOException e2)
{
throw new RuntimeException("资源关闭失败");
}
}
}
flag_update = false ;
flag_create = false ;
flag_open = false ;
flag_save = false ;
}
//对打开操作进行封装;
void open()
{
openDia.setVisible(true);
dirPath = openDia.getDirectory();
fileName = openDia.getFile();
if(fileName==null || dirPath==null)
return ;
file = new File(dirPath,fileName);
if(!file.exists())
{
errorBox();
return ;
}
f.setTitle(title + fileName);
ta.setText("");
BufferedReader bfr = null;
try
{
bfr = new BufferedReader(
new InputStreamReader(new FileInputStream(file),"GB2312"));
String line = null;
while((line = bfr.readLine())!= null)
{
ta.append(line+"\r\n");
}
}
catch (IOException ioe1)
{
throw new RuntimeException("文件打开失败");
}
finally
{
if(bfr!=null)
{
try
{
bfr.close();
}
catch (IOException ioe2)
{
throw new RuntimeException("资源关闭失败");
}
}
}
flag_open = false ;
flag_update = false ;
flag_create = false ;
flag_save = false ;
}
//封装了对话框;
void message()
{
String text = null;
if(fileName == null)
text = "文件 无标题.txt 已经改变,需要保存吗?";
else
text = "文件 " + fileName + " 已经改变,需要保存吗?";
lab.setText(text);
box.setVisible(true);
}
//封装了提示框
void errorBox()
{
lab2.setText("你要找的文件"+fileName+"没有找到!");
errorbox.setVisible(true);
}
//主函数调用;
public static void main(String[] args)
{
new MyNotepad();
}
}
复制代码
作者:
王杰
时间:
2012-4-16 09:18
自己顶一下
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2