這個學期,我們學習了Java編程,期末之時老師要我們編個記事本作為這門課程的課程設計,我寫了一個,大體上的功能都實現了,只有"撤銷"的功能沒有實現,請各位大蝦指點一下,“撤銷”的算法,不甚感激!
特將記事本的源碼附下,請大家指點,看還有沒有更簡潔高效的算法,請不要吝啬,我的E-mail:[email protected],歡迎大家來信指點!
/*
*
*題目:記事本程序
*Author: Jeason
*
* 2004-12-20
*
**/
package jeason;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
import java.text.*;
public class Jsb{
///
boolean dirty=true;
String filename="";
String strtext="";
///////////////////////////////////
////////// Menu ////////////////
///////////////////////
JFrame frame=new JFrame("我的記事本");
JPopupMenu pop=new JPopupMenu("彈出");
JTextArea text=new JTextArea();
JLabel statubar=new JLabel("狀態欄");
JMenuBar Mbar=new JMenuBar();
JFileChooser jFileChooser1=new JFileChooser();
JColorChooser jColor=new JColorChooser();
JMenu jFile=new JMenu("文件");
JMenu jEdit=new JMenu("編輯");
JMenu jFormat=new JMenu("格式");
JMenu jHelp=new JMenu("幫助");
JMenuItem jnew=new JMenuItem("新建(N)");
public Jsb(){ ///無參數的構造函數
///
this.dirty=false;
/////
final BorderLayout borderLayout1=new BorderLayout();
frame.setSize(600,500);//
frame.addWindowListener(new Jsb_frame_closingAdapter(this));
JPanel panel=new JPanel();
panel.setLayout(borderLayout1);//布局
panel.add(new JScrollPane(text));//滾動條
panel.add(statubar,borderLayout1.SOUTH);
frame.getContentPane().add(panel);
//
frame.setJMenuBar(Mbar); //將主菜單添加到窗體中
/*
* jnew
**/
//jnew
jnew.addActionListener(new File_new_actionAdapter(this));
jnew.setMnemonic(´N´);//N上下劃線
jnew.setAccelerator(KeyStroke.getKeyStroke(´N´,java.awt.Event.CTRL_MASK,true));
//快捷方式
/*
* jopen
**/
JMenuItem jopen=new JMenuItem("打開(O)");//jopen
jopen.addActionListener(new File_open_actionAdapter(this));
jopen.setMnemonic(´O´);
jopen.setAccelerator(KeyStroke.getKeyStroke(´O´,java.awt.Event.CTRL_MASK,true));
/*
* jsave
**/
JMenuItem jsave=new JMenuItem("保存(S)");
jsave.addActionListener(new File_save_actionAdapter(this));
jsave.setMnemonic(´S´);
jsave.setAccelerator(KeyStroke.getKeyStroke(´S´,java.awt.Event.CTRL_MASK,true));
/*
* jsave as
**/
JMenuItem jsaveas=new JMenuItem("另存為...");
jsaveas.addActionListener(new File_saveas_actionAdapter(this));
/*
* jquite
**/
JMenuItem jquite=new JMenuItem("關閉(Q)");
jquite.addActionListener(new File_quite_actionAdapter(this));
jquite.setMnemonic(´Q´);
jquite.setAccelerator(KeyStroke.getKeyStroke(´Q´,java.awt.Event.CTRL_MASK,true));
//------------------------------------
// jFile code of end
//--------------------------------------
/**
* jEdit code
*/
//jundo
JMenuItem jundo=new JMenuItem("撤銷(U)");
jundo.addActionListener(new Edit_undo_actionAdapter(this));
jundo.setMnemonic(´U´);
jundo.setAccelerator(KeyStroke.getKeyStroke(´U´,java.awt.Event.CTRL_MASK,true));
/*
* jcut
**/
JMenuItem jcut=new JMenuItem("剪切(X)");
jcut.addActionListener(new Edit_cut_actionAdapter(this));
jcut.setMnemonic(´X´);
jcut.setAccelerator(KeyStroke.getKeyStroke(´X´,java.awt.Event.CTRL_MASK,true));
/*
* jcopy
**/
JMenuItem jcopy=new JMenuItem("復制(C)");
jcopy.addActionListener(new Edit_copy_actionAdapter(this));
jcopy.setMnemonic(´C´);
jcopy.setAccelerator(KeyStroke.getKeyStroke(´C´,java.awt.Event.CTRL_MASK,true));
/*
* jpaste
**/
JMenuItem jpaste=new JMenuItem("粘貼(V)");
jpaste.addActionListener(new Edit_paste_actionAdapter(this));
jpaste.setMnemonic(´V´);
jpaste.setAccelerator(KeyStroke.getKeyStroke(´V´,java.awt.Event.CTRL_MASK,true));
/*
* jdelete
**/
JMenuItem jdelete=new JMenuItem("刪除(D)");
jdelete.addActionListener(new Edit_delete_actionAdapter(this));
jdelete.setMnemonic(´D´);
jdelete.setAccelerator(KeyStroke.getKeyStroke(´D´,java.awt.Event.CTRL_MASK,true));
/*
* jfind
**/
JMenuItem jfind=new JMenuItem("查找(F)");
jfind.addActionListener(new Edit_find_actionAdapter(this));
jfind.setMnemonic(´F´);
jfind.setAccelerator(KeyStroke.getKeyStroke(´F´,java.awt.Event.CTRL_MASK,true));
/*
* jreplace
**/
JMenuItem jreplace=new JMenuItem("替換(R)");
jreplace.addActionListener(new Edit_replace_actionAdapter(this));
jreplace.setMnemonic(´R´);
jreplace.setAccelerator(KeyStroke.getKeyStroke(´R´,java.awt.Event.CTRL_MASK,true));
/*
*jselectall
**/
JMenuItem jselectall=new JMenuItem("全選(A)");
jselectall.addActionListener(new Edit_selectall_actionAdapter(this));
jselectall.setMnemonic(´A´);
jselectall.setAccelerator(KeyStroke.getKeyStroke(´A´,java.awt.Event.CTRL_MASK,true));
/*
* jdate
**/
JMenuItem jdate=new JMenuItem("日期/時間(T)");
jdate.addActionListener(new Edit_timedate_actionAdapter(this));
jdate.setMnemonic(´T´);
jdate.setAccelerator(KeyStroke.getKeyStroke(´T´,java.awt.Event.CTRL_MASK,true));
//------------------------------
// jEdit code of end
//
//-----------------------------
/*
* jword
**/
JMenuItem jword=new JMenuItem("自動換行");
jword.addActionListener(new Format_word_actionAdapter(this));
/*
* jfont
**/
JMenuItem jfont=new JMenuItem("字體..");
jfont.addActionListener(new Format_font_actionAdapter(this));
/*
* jcolor
**/
JMenuItem jcolor=new JMenuItem("顏色...");
jcolor.addActionListener(new Format_color_actionAdapter(this));
//--------------------------------------------------
// jFormat code of end
//--------------------------------------------------
/*
* jabout
**/
JMenuItem jabout=new JMenuItem("關於作者(A)");
jabout.addActionListener(new Help_about_actionAdapter(this));
jabout.setMnemonic(´A´);
jabout.setAccelerator(KeyStroke.getKeyStroke(´A´,java.awt.Event.CTRL_MASK,true));
//-------------------------------------------------
// jHelp code of end
//------------------------------------------------------
/*************************************************
*------------------------------------------------
* PopupMenuItem
*------------------------------------------------
*************************************************/
/*
* pundo
**/
JMenuItem pundo=new JMenuItem("撤銷(U)");
pundo.addActionListener(new Pop_undo_actionAdapter(this));
pundo.setMnemonic(´U´);
pundo.setAccelerator(KeyStroke.getKeyStroke(´U´,java.awt.Event.CTRL_MASK,true));
/*
* pcut
**/
JMenuItem pcut=new JMenuItem("剪切(X)");
pcut.addActionListener(new Pop_cut_actionAdapter(this));
pcut.setMnemonic(´X´);
pcut.setAccelerator(KeyStroke.getKeyStroke(´X´,java.awt.Event.CTRL_MASK,true));
/*
* pcopy
**/
JMenuItem pcopy=new JMenuItem("復制(C)");
pcopy.addActionListener(new Pop_copy_actionAdapter(this));
pcopy.setMnemonic(´C´);
pcopy.setAccelerator(KeyStroke.getKeyStroke(´C´,java.awt.Event.CTRL_MASK,true));
/*
* ppaste
**/
JMenuItem ppaste=new JMenuItem("粘貼(V)");
ppaste.addActionListener(new Pop_paste_actionAdapter(this));
ppaste.setMnemonic(´V´);
ppaste.setAccelerator(KeyStroke.getKeyStroke(´V´,java.awt.Event.CTRL_MASK,true));
/*
*pdelete
**/
JMenuItem pdelete=new JMenuItem("刪除(D)");
pdelete.addActionListener(new Pop_delete_actionAdapter(this));
pdelete.setMnemonic(´D´);
pdelete.setAccelerator(KeyStroke.getKeyStroke(´D´,java.awt.Event.CTRL_MASK,true));
////////
///////////// text////////////////////////////////////////
text.setDoubleBuffered(false);
text.setToolTipText("楊勇的記事本");
text.setVerifyInputWhenFocusTarget(true);
text.setText("");
text.addCaretListener(new Jsb_text_caretAdapter(this));
text.addMouseListener(new Jsb_text_mouseAdapter(this));
text.addAncestorListener(new Jsb_text_ancestorAdapter(this));
//////////////////////////////////////////////////////////
Mbar.add(jFile);
Mbar.add(jEdit);
Mbar.add(jFormat);
Mbar.add(jHelp);
jFile.add(jnew);
jFile.add(jopen);
jFile.addSeparator();
jFile.add(jsave);
jFile.add(jsaveas);
jFile.addSeparator();
jFile.add(jquite);
jEdit.add(jundo);
jEdit.addSeparator();
jEdit.add(jcut);
jEdit.add(jcopy);
jEdit.add(jpaste);
jEdit.add(jdelete);
jEdit.addSeparator();
jEdit.add(jfind);
jEdit.add(jreplace);
jEdit.addSeparator();
jEdit.add(jselectall);
jEdit.add(jdate);
jFormat.add(jword);
jFormat.addSeparator();
jFormat.add(jfont);
jFormat.addSeparator();
jFormat.add(jcolor);
jHelp.add(jabout);
pop.add(pundo);
pop.addSeparator();
pop.add(pcut);
pop.add(pcopy);
pop.add(ppaste);
pop.add(pdelete);
frame.setVisible(true);
}
/************************************************************
//////////////////////Event/////////////////////////////////
**************************************************************/
public void frame_windowclose_windowClosing(WindowListener e){
this.close();
}
/////////////////////////////////////////////////////////
public void text_mouseClicked(MouseEvent e){
if(e.getModifiers()==InputEvent.BUTTON3_MASK){
pop.show((Component)e.getSource(),e.getX(),e.getY());
}
}
public void text_ancestorAdded(AncestorEvent e){
this.dirty=false;
this.newtext();
}
public void text_caretUpdate(CaretEvent e)
{
this.dirty=true;
//this.statubar.setText(this.text.getText());
}
///////////// File /////////////////////////////////////
public void File_open_actionPerformed(ActionEvent e){
//打開的事件
this.opentext();
}
public void File_new_actionPerformed(ActionEvent e){
///新建的事件
this.newtext();
}
public void File_save_actionPerformed(ActionEvent e){
//保存的事件
this.save();
}
public void File_saveas_actionPerformed(ActionEvent e){
//另存為
this.saveas();
}
public void File_quite_actionPerformed(ActionEvent e){
this.close();
}
////////////////// Edit /////////////////////////////////////
public void Edit_undo_actionPerformed(ActionEvent e){
//撤銷
this.undo();
}
public void Edit_cut_actionPerformed(ActionEvent e){
//剪切
this.cut();
}
public void Edit_copy_actionPerformed(ActionEvent e){
//復制
this.copy();
}
public void Edit_paste_actionPerformed(ActionEvent e){
//粘貼
this.paste();
}
public void Edit_delete_actionPerformed(ActionEvent e){
//刪除
this.delete();
}
public void Edit_find_actionPerformed(ActionEvent e){
//查找
int cu=this.text.getCaretPosition();
int end=this.text.getText().length();
FindDlg fd=new FindDlg(frame,"查找",true);
fd.show();
String str=fd.getFindStr().trim();
if(fd.getFlag()){
this.nextFindStr(str,cu,end);
}else{
this.upFindStr(str,cu);
}
}
public void Edit_replace_actionPerformed(ActionEvent e){
//替換
int cu=this.text.getCaretPosition();
int end=this.text.getText().length();
ReplaceDlg rd=new ReplaceDlg(frame,"替換",true);
rd.show();
this.replaceStr(rd.findStr().trim(),rd.replaceStr().trim(),cu,end);
}
public void Edit_selectall_actionPerformed(ActionEvent e){
//全選
this.text.setSelectionStart(0);
this.text.setSelectionEnd(this.text.getText().length());
}
public void Edit_timedate_actionPerformed(ActionEvent e){
//時間日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
this.text.append("\r\n當前