欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java仿Windows记事本源代码分享

程序员文章站 2024-02-23 18:05:28
本文实例为大家分享了java仿windows记事本的具体代码,供大家参考,具体内容如下 先上截图: 源代码: import java.awt.*;...

本文实例为大家分享了java仿windows记事本的具体代码,供大家参考,具体内容如下

先上截图:

Java仿Windows记事本源代码分享
Java仿Windows记事本源代码分享
Java仿Windows记事本源代码分享

源代码:

import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.filenameextensionfilter;
import javax.swing.undo.undomanager;
import java.awt.event.*;
import java.io.*;

/**
 * 
 * @author cjbi@outlook.com
 * @created 2015年7月6日 上午11:22:24
 */
public class jnotepad extends jframe implements actionlistener {

 jmenubar menubar = new jmenubar();
 jmenu file = new jmenu("文件(f)");
 jmenu edit = new jmenu("编辑(e)");
 jmenu format = new jmenu("格式(o)");
 jmenu help = new jmenu("帮助(h)");
 jmenuitem create = new jmenuitem("新建");
 jmenuitem open = new jmenuitem("打开...");
 jmenuitem save = new jmenuitem("保存");
 jmenuitem saveas = new jmenuitem("另存为...");
 jmenuitem exit = new jmenuitem("退出");
 jmenuitem undo = new jmenuitem("撤销");
 jmenuitem cut = new jmenuitem("剪切");
 jmenuitem copy = new jmenuitem("复制");
 jmenuitem paste = new jmenuitem("粘贴");
 jmenuitem findrep = new jmenuitem("查找替换");
 jmenuitem selectall = new jmenuitem("全选");
 jmenuitem font = new jmenuitem("字体");
 jmenuitem about = new jmenuitem("关于");
 jmenuitem cut2 = new jmenuitem("剪切(x)");

 jmenuitem copy2 = new jmenuitem("复制(c)");
 jmenuitem paste2 = new jmenuitem("粘贴(v)");
 jmenuitem selectall2 = new jmenuitem("全选(a)");
 public static jtextarea textarea = new jtextarea();
 undomanager um = new undomanager();
 jscrollpane scroll = new jscrollpane(textarea, scrollpaneconstants.vertical_scrollbar_always, scrollpaneconstants.horizontal_scrollbar_as_needed);
 jpopupmenu popup = new jpopupmenu();
 string pathselect;

 // 获取屏幕尺寸
 dimension screensize = toolkit.getdefaulttoolkit().getscreensize();

 public jnotepad() {

  // 此处定义键盘快捷键
  // menubar
  file.setmnemonic(keyevent.vk_f);
  edit.setmnemonic(keyevent.vk_e);
  format.setmnemonic(keyevent.vk_o);
  help.setmnemonic(keyevent.vk_h);
  // menuitem
  create.setaccelerator(keystroke.getkeystroke(keyevent.vk_n, actionevent.ctrl_mask));
  open.setaccelerator(keystroke.getkeystroke(keyevent.vk_o, actionevent.ctrl_mask));
  save.setaccelerator(keystroke.getkeystroke(keyevent.vk_s, actionevent.ctrl_mask));
  undo.setaccelerator(keystroke.getkeystroke(keyevent.vk_z, actionevent.ctrl_mask));
  cut.setaccelerator(keystroke.getkeystroke(keyevent.vk_x, actionevent.ctrl_mask));
  copy.setaccelerator(keystroke.getkeystroke(keyevent.vk_c, actionevent.ctrl_mask));
  paste.setaccelerator(keystroke.getkeystroke(keyevent.vk_v, actionevent.ctrl_mask));
  findrep.setaccelerator(keystroke.getkeystroke(keyevent.vk_f, actionevent.ctrl_mask));
  selectall.setaccelerator(keystroke.getkeystroke(keyevent.vk_a, actionevent.ctrl_mask));

  // 事件监听者
  save.addactionlistener(this);
  create.addactionlistener(this);
  open.addactionlistener(this);
  saveas.addactionlistener(this);
  exit.addactionlistener(this);
  undo.addactionlistener(this);
  cut.addactionlistener(this);
  copy.addactionlistener(this);
  paste.addactionlistener(this);
  selectall.addactionlistener(this);
  font.addactionlistener(this);
  about.addactionlistener(this);
  cut2.addactionlistener(this);
  copy2.addactionlistener(this);
  paste2.addactionlistener(this);
  selectall2.addactionlistener(this);
  findrep.addactionlistener(this);
  // 设置撤销文本的管理器
  textarea.getdocument().addundoableeditlistener(um);
  textarea.setfont(format.font);
  // 文件
  file.add(create);
  file.add(open);
  file.add(save);
  file.add(saveas);
  file.addseparator();
  file.add(exit);

  // 编辑
  edit.add(undo);
  edit.addseparator();
  edit.add(cut);
  edit.add(copy);
  edit.add(paste);
  edit.addseparator();
  edit.add(findrep);
  edit.addseparator();
  edit.add(selectall);

  // 格式
  format.add(font);

  // 帮助
  help.add(about);

  // 菜单栏
  menubar.add(file);
  menubar.add(edit);
  menubar.add(format);
  menubar.add(help);

  // 右键菜单
  popup.add(cut2);
  popup.add(copy2);
  popup.add(paste2);
  popup.addseparator();
  popup.add(selectall2);

  // 添加到文本域容器
  textarea.add(popup);

  // 匿名内部类监听器右键动作
  textarea.addmouselistener(new mouseadapter() {
   public void mousereleased(mouseevent e) {
    if (e.getbutton() == mouseevent.button3) {
     popup.show(textarea, e.getx(), e.gety());
    }
   }
  });

  // 边界布局
  this.add(menubar, borderlayout.north);
  this.add(scroll, borderlayout.center);
  this.settitle("记事本");
  this.setsize(500, 400);
  this.setlocationrelativeto(null);
  this.seticonimage(new imageicon(this.getclass().getresource("/icon/notepad.png")).getimage());//图标放在源目录的icon文件夹
  this.setdefaultcloseoperation(jframe.dispose_on_close);
  this.setvisible(true);

 }

 // 重写actionperformed
 @override
 public void actionperformed(actionevent e) {
  // event对象发生源
  if (e.getsource() == open) {

   jfilechooser chooser = new jfilechooser();
   filenameextensionfilter filter = new filenameextensionfilter("文本文档(*.txt)", "txt");
   chooser.setfilefilter(filter);
   chooser.setdialogtitle("文件打开");
   chooser.showopendialog(null);
   chooser.setvisible(true);

   try {
    pathselect = chooser.getselectedfile().getpath();
    filereader wjl = new filereader(pathselect);
    bufferedreader hcl = new bufferedreader(wjl);
    string s = "", zfc = "";
    while ((s = hcl.readline()) != null) {
     zfc += (s + "\n");
    }
    textarea.settext(zfc);

   } catch (exception e1) {
   }
  }

  if (e.getsource() == saveas) {// 另存为

   jfilechooser chooser = new jfilechooser();
   filenameextensionfilter filter = new filenameextensionfilter("文本文档(*.txt)", "txt");
   chooser.setfilefilter(filter);
   chooser.setdialogtitle("另存为");
   chooser.showsavedialog(null);
   chooser.setvisible(true);

   printstream ps;
   try {
    string select = chooser.getselectedfile().getpath();
    ps = new printstream(select);
    system.setout(ps);
    system.out.println(this.textarea.gettext());

   } catch (exception e1) {
   }
  }

  if (e.getsource() == save && (pathselect == null)) {// 保存
   jfilechooser chooser = new jfilechooser();

   chooser.setdialogtitle("保存");
   chooser.showsavedialog(null);
   chooser.setvisible(true);

   printstream ps;
   try {
    pathselect = chooser.getselectedfile().getpath();
    ps = new printstream(pathselect);
    system.setout(ps);
    system.out.println(this.textarea.gettext());

   } catch (exception e1) {
   }
  } else if (e.getsource() == save && !(pathselect == null)) {
   printstream ps;
   try {
    ps = new printstream(pathselect);
    system.setout(ps);
    system.out.println(this.textarea.gettext());
   } catch (filenotfoundexception e1) {
   }
  }

  if (e.getsource() == create) {
   textarea.settext("");
   pathselect = null;
  }

  if (e.getsource() == exit) {
   system.exit(0);
  }

  if (e.getsource() == undo) {
   if (um.canundo()) {
    um.undo();
   }
  }

  if (e.getsource() == cut || e.getsource() == cut2) {
   textarea.cut();
  } else if (e.getsource() == copy || e.getsource() == copy2) {
   textarea.copy();
  } else if (e.getsource() == paste || e.getsource() == paste2) {
   textarea.paste();
  } else if (e.getsource() == findrep) {
   new findandreplace(textarea);
  }

  else if (e.getsource() == selectall || e.getsource() == selectall2) {
   textarea.selectall();
  }
  if (e.getsource() == font) {
   new format(textarea);
  }
  if (e.getsource() == about) {
   new about();
  }

 }

 public static void main(string[] args) {
  new jnotepad();
 }

}

class findandreplace extends jdialog implements actionlistener {// 查找和替换
 jlabel findlabel = new jlabel("查找内容:");
 jlabel replabel = new jlabel(" 替换为:");
 jtextfield findtf = new jtextfield(8);
 jtextfield reptf = new jtextfield(8);
 jbutton findbtn = new jbutton("查找");
 jbutton repbtn = new jbutton("替换");
 jpanel findpn = new jpanel();
 jpanel reppn = new jpanel();
 jtextarea textarea;

 string text;
 boolean flg = false;
 int len;
 int start = 0;
 int k = 0;

 public findandreplace(jtextarea textarea) {

  this.textarea = textarea;

  findpn.add(findlabel);
  findpn.add(findtf);
  findpn.add(findbtn);
  reppn.add(replabel);
  reppn.add(reptf);
  reppn.add(repbtn);
  this.add(findpn);
  this.add(reppn);

  findbtn.addactionlistener(this);
  repbtn.addactionlistener(this);

  this.settitle("查找和替换");
  this.setlayout(new gridlayout(2, 1));
  // this.setbounds(400, 200, 300, 140);
  this.pack();
  this.setlocationrelativeto(null);
  this.setresizable(false);
  this.setvisible(true);
  this.setdefaultcloseoperation(jdialog.dispose_on_close);
 }

 @suppresswarnings("deprecation")
 public void actionperformed(actionevent e) {
  string findtext = findtf.gettext();
  string reptext = reptf.gettext();
  text = textarea.gettext();
  if (e.getsource() == findbtn) {
   findbtn.setlabel("下一个");
   if (findtext != null) {
    len = findtext.length();
    start = text.indexof(findtext, k);
    k = start + len;
    textarea.select(start, start + len);
    flg = true;
    if (start == -1) {
     joptionpane.showmessagedialog(null, "已到文件尾部!", "提示", joptionpane.information_message);
     start = 0;
     k = 0;
     flg = false;
    }
   }
  } else if (e.getsource() == repbtn) {
   if (flg) {
    textarea.replacerange(reptext, start, start + len);
    flg = false;
   }
  }
 }
}

// 字体格式
class format extends jdialog implements actionlistener {

 public static int style = 0; // 全局变量类型,默认值为0
 public static int size = 16; // 全局变量字体大小,默认值为16
 public static font font = new font("新宋体", style, size); // 全局变量字体,默认值为新宋体

 jpanel pn = new jpanel();
 jpanel okcelpn = new jpanel();
 jpanel fontpn = new jpanel();
 jpanel ptpn = new jpanel();
 jlabel fontlabel = new jlabel("字体: ");
 jlabel fontstylelabel = new jlabel(" 字形: ");
 jlabel ptlabel = new jlabel("  磅值: ");
 jbutton ok = new jbutton("确定");
 jbutton cancel = new jbutton("取消");
 graphicsenvironment e = graphicsenvironment.getlocalgraphicsenvironment();// 获取系统中可用的字体的名字
 string[] fontname = e.getavailablefontfamilynames();// 获取系统中可用的字体的名字
 string[] fonttype = { "常规", "倾斜", "粗体", "粗偏斜体" };
 jlist fontlist = new jlist(fontname);
 jlist fonttypelist = new jlist(fonttype);
 jscrollpane fontscroll = new jscrollpane(fontlist);
 jscrollpane fonttypescroll = new jscrollpane(fonttypelist);

 jtextarea textarea;
 spinnermodel spinnermodel = new spinnernumbermodel(size, // initial value
   0, // min
   100, // max
   2 // step
 );
 jspinner spinner = new jspinner(spinnermodel);

 public format(jtextarea textarea) {
  this.textarea = textarea;
  ok.addactionlistener(this);
  cancel.addactionlistener(this);

  pn.setlayout(new gridlayout(2, 1));
  pn.add(fontpn);
  pn.add(ptpn);

  fontpn.add(fontlabel);
  fontpn.add(fontscroll);
  fontpn.add(fontstylelabel);
  fontpn.add(fonttypescroll);

  ptpn.add(ptlabel);
  ptpn.add(spinner);

  fontlist.setvisiblerowcount(5);
  fontlist.setfixedcellwidth(60);
  fontlist.setselectedindex(50);
  fontlist.setselectedvalue(font.getfontname(), true);

  fonttypelist.setvisiblerowcount(5);
  fonttypelist.setselectedindex(style);
  okcelpn.add(ok);
  okcelpn.add(cancel);

  okcelpn.setlayout(new flowlayout(flowlayout.right));

  this.add(pn, borderlayout.center);
  this.add(okcelpn, borderlayout.south);

  this.settitle("字体");
  this.pack();
  this.setlocationrelativeto(null);
  this.setresizable(false);
  this.setvisible(true);
  this.setdefaultcloseoperation(jframe.dispose_on_close);
 }

 public void actionperformed(actionevent e) {
  if (e.getsource() == ok) {
   system.out.println(fontlist.getselectedvalue());
   style = this.type();
   size = integer.parseint(spinner.getvalue().tostring());
   font = new font((string) fontlist.getselectedvalue(), style, size);
   textarea.setfont(font);
   this.dispose();
   system.out.println(type());
  } else if (e.getsource() == cancel) {
   this.dispose();
  }
 }

 private int type() {
  if (fonttypelist.getselectedvalue().equals("倾斜")) {
   return 1;
  } else if (fonttypelist.getselectedvalue().equals("粗体")) {
   return 2;
  } else if (fonttypelist.getselectedvalue().equals("粗偏斜体")) {
   return 3;
  } else
   return 0;
 }

}

class about extends jdialog {// 关于窗口

 about() {
  joptionpane.showmessagedialog(null, " 作者:cjb 版本:v1.5\n\n 联系:cjbi@outlook.com", "关于",
    joptionpane.plain_message);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。