




免费预览已结束,剩余28页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
用Java编写的记事本程序作者:未知 文章来源:访问次数:6050次 加入时间:2005年01月19日 这个学期,我们学习了Java编程,期末之时老师要我们编个记事本作为这门课程的课程设计,我写了一个,大体上的功能都实现了,只有撤销的功能没有实现,请各位大虾指点一下,“撤销”的算法,不甚感激!特将记事本的源码附下,请大家指点,看还有没有更简洁高效的算法,请不要吝啬,我的E-mail:,欢迎大家来信指点!/*题目:记事本程序*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*/ /jnewjnew.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);/jopenjopen.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*/jundoJMenuItem 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);elsethis.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( 当前时间:+sdf.format(new Date();/ Format/public void Format_word_actionPerformed(ActionEvent e)/自动换行if(!this.text.getLineWrap()this.text.setLineWrap(true);elsethis.text.setLineWrap(false);public void Format_font_actionPerformed(ActionEvent e)/字体常规,斜体,粗体,粗斜体Font cur=this.text.getFont();int type=Font.BOLD;FontSet fs=new FontSet(frame,字体设置,true);fs.show();if(fs.flag)switch(fs.font2()case 0:type=Font.PLAIN;break;case 1:type=Font.ITALIC;break;case 2:type=Font.BOLD; break;case 3:type=Font.ROMAN_BASELINE;break; default:type=Font.PLAIN;break; Font f=new Font(fs.font1(),type,16);this.text.setFont(f);elsethis.text.setFont(cur);public void Format_color_actionPerformed(ActionEvent e)/ 颜色Color current=this.text.getForeground();this.jColor.setColor(current);this.text.setForeground(this.jColor.showDialog(text,选择颜色,current); / Help /public void Help_about_actionPerformed(ActionEvent e)/关于作者new AboutDlg();/public void pop_undo_actionPerformed(ActionEvent e)/Pop undothis.undo();public void pop_cut_actionPerformed(ActionEvent e)/pop cutthis.cut();public void pop_copy_acionPerformed(ActionEvent e)/pop copythis.copy();public void pop_paste_actionPerformed(ActionEvent e)/pop pastethis.paste();public void pop_delete_actionPerformed(ActionEvent e)/pop deletethis.delete();/*/ coustm function /*/void close() if(this.dirty)Dlgtext Dt=new Dlgtext(frame,提示,true);Dt.show();if(Dt.getCheck()this.save();else frame.dispose();System.exit(0);elseframe.dispose();System.exit(0); void newtext()/新建if(!this.dirty)|(!this.saveas()this.text.setText();this.text.setFocusable(true);this.frame.setTitle(未命名-Author:Jeason);this.statubar.setText(新建文本);else this.saveas();void opentext()/打开/String strFileOpen=;if(!this.dirty)tryif(this.jFileChooser1.APPROVE_OPTION=this.jFileChooser1.showOpenDialog(frame)strFileOpen=this.jFileChooser1.getSelectedFile().getPath();File file=new File(strFileOpen);int flength=(int)file.length();int num=0;FileReader fReader=new FileReader(file);char data=new charflength;while(fReader.ready()num+=fReader.read(data,num,flength-num);fReader.close();this.text.setText(new String(data,0,num);this.filename=strFileOpen;this.frame.setTitle(this.filename);this.statubar.setText(Open File:+this.filename);this.dirty=false;elsereturn ;catch(Exception e)this.statubar.setText(Error Open:+e.getMessage();elsethis.save();boolean save()/保存if(this.dirty)if(this.filename.length()!=0)tryFile saveFile=new File(this.filename);FileWriter fw=new FileWriter(saveFile);fw.write(this.text.getText();fw.close();this.dirty=false;this.statubar.setText(保存文件:+this.filename);return true; catch(Exception e)this.statubar.setText(保存出错: +e.getMessage();return false;elsereturn this.saveas();elsereturn true;boolean saveas()/另存为if(this.jFileChooser1.APPROVE_OPTION=this.jFileChooser1.showSaveDialog(frame)this.filename=this.jFileChooser1.getSelectedFile().getPath();return this.save();else return false;void undo()/undo void cut()/cuttryString str=this.text.getSelectedText();if(str.length()!=0)this.strtext=str;this.text.replaceRange(,this.text.getSelectionStart(),this.text.getSelectionEnd();this.dirty=true;catch(Exception ex)this.statubar.setText(剪切出错:+ex.getMessage();void copy()/copytryString str=this.text.getSelectedText();if(str.length()!=0)this.strtext=str; catch(Exception ex)this.statubar.setText(复制出错!+ex.getMessage();void paste()/pasteif(this.strtext.length()0)this.text.insert(this.strtext,this.text.getCaretPosition();this.dirty=true;void delete()/deletethis.text.replaceRange(,this.text.getSelectionStart(),this.text.getSelectionEnd();this.dirty=true;void nextFindStr(String str,int cur,int end)if(curend)this.statubar.setText(没有找到!);elseint i=this.text.getText().indexOf(str);if(i=0)this.text.setSelectionStart(i);this.text.setSelectionEnd(i+str.length();this.statubar.setText(已经在:+i+ 位置找到!);elsenextFindStr(str,+cur,end); void upFindStr(String str,int cur)if(cur=0)this.text.setSelectionStart(i);this.text.setSelectionEnd(i+str.length();this.statubar.setText(已经在:+i+ 位置找到!);elseupFindStr(str,-cur);void replaceStr(String findStr,String replaceStr,int cur,int end)if(curend)this.statubar.setText(没有找到!);elseint i=this.text.getText().indexOf(findStr);if(i0)this.text.setSelectionStart(i);this.text.setSelectionEnd(i+findStr.length();this.text.replaceRange(replaceStr,this.text.getSelectionStart(),this.text.getSelectionEnd();elsereplaceStr(findStr,replaceStr,+cur,end);/*-* main* *-*/public static void main(String args)Jsb jsb1=new Jsb();/*/ Event class /*/class Jsb_frame_closingAdapter extends java.awt.event.WindowAdapterJsb adaptee;Jsb_frame_closingAdapter(Jsb adaptee)this.adaptee=adaptee;public void windowClosing(WindowListener e)adaptee.frame_windowclose_windowClosing(e);/class Jsb_text_mouseAdapter extends java.awt.event.MouseAdapterJsb adaptee;Jsb_text_mouseAdapter(Jsb adaptee)this.adaptee=adaptee;public void mouseClicked(MouseEvent e)adaptee.text_mouseClicked(e); class Jsb_text_ancestorAdapter implements javax.swing.event.AncestorListenerJsb adaptee;Jsb_text_ancestorAdapter(Jsb adaptee)this.adaptee=adaptee;public void ancestorAdded(AncestorEvent e)adaptee.text_ancestorAdded(e);public void ancestorRemoved(AncestorEvent e)public void ancestorMoved(AncestorEvent e)class Jsb_text_caretAdapter implements javax.swing.event.CaretListenerJsb adaptee;Jsb_text_caretAdapter(Jsb adaptee)this.adaptee=adaptee;public void caretUpdate(CaretEvent e)adaptee.text_caretUpdate(e);/class File_open_actionAdapter
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高考单招模拟试题及答案
- 邢台疫情考试试题及答案
- 2025年专项选调考试试题及答案
- 2025年安全业务考试试题及答案
- 2025年专科民法考试试题及答案
- 2025年泌尿系统常见疾病诊断与治疗模拟试卷答案及解析
- 2025年食品安全管理员培训考试试题及答案
- 2025年精神科精神障碍患者心理评估模拟测试答案及解析
- 银行面试线上模拟试题及答案
- 城管队员笔试试题及答案
- 2025年合肥公交集团有限公司驾驶员招聘180人笔试参考题库附带答案详解
- 2024年上海市大数据中心招聘真题
- 2025年网络安全监测预警体系建设实施方案评估报告
- 2025年会计继续教育网络答题真题及答案
- 2025年工勤行政事务高级技师技术等级考试试题及答案
- 中国银行招聘笔试真题及答案(可下载)
- 高血压指南培训课件
- 设计文件更改管理办法
- 飞利浦录音笔VTR8000说明书
- 数字赋能教学课件
- (高清版)DZT 0217-2020 石油天然气储量估算规范
评论
0/150
提交评论