上接<java记事本程序(一)> 记得回复哦.....嘿嘿
public void caretUpdate(CaretEvent e)
{
//统计当前文本内有字数
lab_statusBar2.setText(BytesTotal());
}
//以下为窗体操作引发的事件
public void windowOpened(WindowEvent e)
{
}
public void windowClosing(WindowEvent e) //当窗口关闭时检查当前文本是否为空,若为空则提示用户保存当前文档,否则退出当前程序
{
if(text.getText().length()>0) //当文本非空时,做如下处理
{
Object[] options = { "OK", "CANCEL" };
int option=JOptionPane.showOptionDialog(null, "是否保存当前文档", "提示",
JOptionPane.YES_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
if(option==0) //若用户单击了保存,则弹出保存对话框让用户保存当前文档,然后退出当前程序
{
saveFile();
System.exit(0);
}
else if(option==1) //若用户单击了"否"刚直接退出当前程序
{
System.exit(0);
}
}
else //若当前文本为空则直接退出
{
System.exit(0);
}
}
public void windowClosed(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==menuitem11)
{
new NotePad3();
}
else if(e.getSource()==menuitem12) //打开文件
{
openFile();
}
else if(e.getSource()==menuitem13) //保存文件
{
saveFile();
}
else if(e.getSource()==menuitem14) //另存为文件
{
chooser=new JFileChooser();
chooser.showDialog(this,"另存为");
path=chooser.getSelectedFile().getPath();
try
{
BufferedWriter buff=new BufferedWriter(new FileWriter(path));
String str = text.getText();
buff.write(str, 0, text.getText().length());
buff.close();
this.setTitle(path);
}
catch(FileNotFoundException e1)
{
JOptionPane.showMessageDialog(null,"保存失败!");
}
catch(IOException e2)
{
JOptionPane.showMes