和同学几个写了一个简单记事本java程序,望大虾给修改一下,有的功能不能实现,感觉越做越乱,都是初学者,一改老是出错,这个功能不知道怎么做“当点击新建、打开、退出等命令时,如果当前有打开的文件并且被改动过,则要提示用户是否保存修改。”这是老师布置的作业,求助各位大虾,帮忙修改一下吧
程序;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TxtEdit2
{
public static void main(String args[])
{
MySimpleTextEditor editor=new MySimpleTextEditor();
editor.setSize(300,200);
editor.setVisible(true);
editor.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
}
class MySimpleTextEditor extends Frame
implements ActionListener
{
private MenuBar myBar=new MenuBar();
private Menu fileMenu, editMenu, cutMenu, aboutMenu;
private MenuItem cutItem, copyItem, pasteItem, selectAllItem;
private MenuItem recentcutItem, aboutItem;
private MenuItem quitItem, openItem, saveItem;
private TextArea mainWorkWindow=new TextArea(10,15);
private String placeHolder=" ";
private Vector recentCuts=new Vector();
public MySimpleTextEditor()
{ super("我的文本编辑器");
setLayout(new BorderLayout());
add("Center",mainWorkWindow);
setMenuBar(myBar);
initFileMenu();
initEditMenu();
initAboutMenu();
}
private void initEditMenu()
{
editMenu=new Menu("编辑");
myBar.add(editMenu);
cutItem=new MenuItem("剪切");
cutItem.addActionListener(this);
editMenu.add(cutItem);
copyItem=new MenuItem("复制");
copyItem.addActionListener(this);
editMenu.add(copyItem);
pasteItem=new MenuItem("粘贴");
pasteItem.addActionListener(this);
editMenu.add(pasteItem);
editMenu.addSeparator();
selectAllItem=new MenuItem("全选");
selectAllItem.addActionListener(this);
editMenu.add(selectAllItem);
editMenu.addSeparator();
cutMenu=new Menu("剪贴簿");
editMenu.add(cutMenu);
}
private void initFileMenu()
{
fileMenu=new Menu("文件");
myBar.add(fileMenu);
openItem=new MenuItem("打开");
openItem.addActionListener(this);
openItem.setEnabled(true);
fileMenu.add(openItem);
saveItem=new MenuItem("保存");
saveItem.addActionListener(this);
saveItem.setEnabled(true);
fileMenu.add(saveItem);
fileMenu.addSeparator();
quitItem=new MenuItem("退出");
quitItem.addActionListener(this);
fileMenu.add(quitItem);
}
private void initAboutMenu()
{
aboutMenu=new Menu("关于");
myBar.add(aboutMenu);
aboutItem=new MenuItem("关于");
aboutMenu.add(aboutItem);
aboutItem.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(!(e.getSource() instanceof MenuItem))
return;
MenuItem m=(MenuItem)e.getSource();
if(m==quitItem)
{
dispose();
System.exit(0);
}
else if(m==cutItem)
{
placeHolder=mainWorkWindow.getSelectedText();
mainWorkWindow.replaceRange("",
mainWorkWindow.getSelectionStart(),
mainWorkWindow.getSelectionEnd());
addRecentCut(placeHolder);
}
else if (m==copyItem)
{
placeHolder=mainWorkWindow.getSelectedText();
}
else if (m==pasteItem)
{
mainWorkWindow.insert(placeHolder,mainWorkWindow.getCaretPosition());
}
else if (m==selectAllItem)
{
mainWorkWindow.selectAll();
}
else if (m==openItem)
{
Frame f=new Frame("FileDialog");
FileDialog d;
d=new FileDialog(f,"FileDialog");
d.setVisible(true);
String filename=d.getFile();
File openf=new File(filename);
String s="";
try
{
RandomAccessFile fr=new RandomAccessFile(d.getDirectory()+filename,"r");
byte[] buffer = new byte[4096];
int len;
while((len=fr.read(buffer))!=-1)
{
s = new String(buffer, 0, len);
mainWorkWindow.append(s+"\n");
}
fr.close();
}catch(Exception er){}
}
else if(m==saveItem)
{
Frame f=new Frame("FileDialog");
FileDialog d;
d=new FileDialog(f,"FileDialog");
d.setVisible(true);
String filename=d.getFile();
// File openf=new File(filename);
try
{
RandomAccessFile fw=new RandomAccessFile(d.getDirectory()+filename,"rw");
String s=mainWorkWindow.getText();
fw.writeChars(s);
fw.close();
}catch(Exception er){}
}
else if(m==aboutItem)
{
MyAboutDialog dlg = new MyAboutDialog(this, false);
dlg.show();
}
}
private class MyAboutDialog extends Dialog implements ActionListener{
Label label1 = new Label("2");
Label label2 = new Label("342");
Button Ok = new Button("确定");
Panel p = new Panel(new BorderLayout());
MyAboutDialog(Frame parent, boolean model){
super(parent, model);
setTitle("关于对话框");
setSize(300, 200);
setResizable(false);
setLayout(new BorderLayout());
p.add(BorderLayout.NORTH , label1);
p.add(BorderLayout.CENTER ,label2);
p.add(BorderLayout.SOUTH ,Ok);
Ok.setBounds(200,160, 50, 30);
add(p);
Ok.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
dispose();
}
}
private void addRecentCut(String cut)
{
recentCuts.insertElementAt(cut,0);
cutMenu.removeAll();
for(int i=0;i<recentCuts.size();i++)
{
MenuItem item=new MenuItem(
(String)recentCuts.elementAt(i));
cutMenu.add(item);
//item.addActionListener(this);
}
}
}