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

java仿windows记事本小程序

程序员文章站 2023-12-18 16:16:16
本文实例为大家分享了java仿windows记事本小程序的具体代码,供大家参考,具体内容如下 import java.awt.checkboxmenuitem;...

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

import java.awt.checkboxmenuitem;
import java.awt.font;
import java.awt.frame;
import java.awt.menu;
import java.awt.menubar;
import java.awt.menuitem;
import java.awt.menushortcut;
import java.awt.textarea;
import java.awt.toolkit;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.event.itemevent;
import java.awt.event.itemlistener;
import java.awt.event.keyevent;
import java.awt.event.windowadapter;
import java.awt.event.windowevent;
import java.awt.peer.buttonpeer;
import java.io.bufferedreader;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.outputstream;
import java.io.stringreader;
import java.io.stringwriter;
import java.sql.date;
import java.text.simpledateformat;
 
import javax.print.docflavor.string;
import javax.script.scriptexception;
import javax.swing.borderfactory;
import javax.swing.boxlayout;
import javax.swing.buttongroup;
import javax.swing.jfilechooser;
import javax.swing.jframe;
import javax.swing.joptionpane;
import javax.swing.jpanel;
import javax.swing.jradiobutton;
import javax.swing.jscrollbar;
import javax.swing.jscrollpane;
import javax.swing.jtextarea;
import javax.swing.border.etchedborder;
import javax.swing.border.titledborder;
import javax.swing.event.ancestorlistener;
import javax.swing.filechooser.filenameextensionfilter;
 
public class crazy_15_4 {
 private jframe f = new jframe("记事本");
 
 //成员变量
 static string shearplate = null;
 static boolean isautowrap = true;//是否自动换行
 jtextarea ta = new jtextarea(20, 60);
 jscrollpane jsp = new jscrollpane(ta);
 private menubar mb = new menubar();
 public string fontstr = "宋体";
 public string shapestr = "0";
 public string sizestr = "20";
 font font = new font(fontstr, integer.parseint(shapestr),integer.parseint(sizestr) );
 
 
 //menu
 menu file = new menu("文件");
 menu edit = new menu("编辑");
 menu format = new menu("格式");
 
 //menuitem
 menuitem newitem = new menuitem("新建");
 menuitem openitem = new menuitem("打开");
 menuitem saveitem = new menuitem("保存");
 menuitem saveasitem = new menuitem("另存为");
 menuitem cutitem = new menuitem("剪切");
 menuitem copyitem = new menuitem("复制");
 menuitem pasteitem = new menuitem("粘贴");
 checkboxmenuitem autowrap = new checkboxmenuitem("自动换行",isautowrap);
 menuitem fontitem = new menuitem("字体");
 //使用“ctrl+x”快捷键
 menuitem exitiitem = new menuitem("退出", new menushortcut(keyevent.vk_x));
 
 
 
 
 
 public void init() {
 //带滚动条的文本域
 ta.setlinewrap(isautowrap);
 jsp.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_never);
 jsp.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_as_needed);
 
 
 file.add(newitem);
 file.add(openitem);
 file.add(saveitem);
 file.add(saveasitem);
 
 edit.add(cutitem);
 edit.add(copyitem);
 edit.add(pasteitem);
 
 format.add(autowrap);
 format.add(fontitem);
 
 mb.add(file);
 mb.add(edit);
 mb.add(format);
 
 f.setmenubar(mb);
 f.addwindowlistener(new windowadapter() {
 public void windowclosing(windowevent we) {
 if(!(ta.gettext().equals(""))) {
  object[] options = {"保存","不保存","取消"};
  int a = joptionpane.showoptiondialog(null,"是否将更改保存到h:/programing/无标题.txt","记事本", joptionpane.yes_no_cancel_option,joptionpane.question_message, null, options, options[0]);
  if(a == joptionpane.ok_option ) {
  if(save()) {
  system.exit(0);
  }
  }else if(a == joptionpane.no_option) {
  system.exit(0);
  }
 }else {
  system.exit(0);
 }
 }
 });
 
 newitem.addactionlistener(new newlistener());
 openitem.addactionlistener(new openlistener());
 saveitem.addactionlistener(new savelistener());
 saveasitem.addactionlistener(new saveaslistener());
 cutitem.addactionlistener(new cutaslistener());
 copyitem.addactionlistener(new copyaslistener());
 pasteitem.addactionlistener(new pasteaslistener());
 autowrap.additemlistener(new autowrapaslistener());
 fontitem.addactionlistener(new fontlistener());
 
 ta.setfont(font);
 //使文本框处在屏幕中间
 int width = toolkit.getdefaulttoolkit().getscreensize().width;
 int height = toolkit.getdefaulttoolkit().getscreensize().height; 
 f.add(jsp);
 f.pack();
 f.setlocation(width/2-f.getwidth()/2, height/2-f.getheight()/2);
 f.setvisible(true);
 //避免关闭对话框直接关闭文本文件
 f.setdefaultcloseoperation(jframe.do_nothing_on_close);
 }
 public static void main(string[] args) {
 new crazy_15_4().init();
 }
 
 //是否保存
 public void saveornot() {
 if(!(ta.gettext().equals(""))) {
 object[] options = {"保存","不保存","取消"};
 int a = joptionpane.showoptiondialog(null,"是否将更改保存到h:/programing/无标题.txt","记事本", joptionpane.yes_no_cancel_option,joptionpane.question_message, null, options, options[0]);
 if(a == joptionpane.ok_option ) {
 save();
 }
 }
 }
 //保存文本文件
 public boolean save() {
 boolean issaved = false;
 jfilechooser jf = new jfilechooser();
 filenameextensionfilter filter = new filenameextensionfilter("txt","txt");
 jf.setacceptallfilefilterused(false);
 jf.setfilefilter(filter);
 int value = jf.showsavedialog(null);
 if(value == jfilechooser.approve_option) {
 file file = jf.getselectedfile();
 string str = ta.gettext();
 byte[] buf = str.getbytes();
 try {
 outputstream os = new fileoutputstream(file);
 os.write(buf);
 os.flush();
 os.close();
 issaved = true;
 } catch (ioexception e) {
 e.printstacktrace();
 }
 }
 return issaved;
 }
 //新建监听器
 class newlistener implements actionlistener{
 public void actionperformed(actionevent e) {
 if(!(ta.gettext().equals(""))) {
 object[] options = {"保存","不保存","取消"};
 int a = joptionpane.showoptiondialog(null,"是否将更改保存到h:/programing/无标题.txt","记事本", joptionpane.yes_no_cancel_option,joptionpane.question_message, null, options, options[0]);
 if(a == joptionpane.ok_option ) {
  if(save()) {
  ta.settext("");
  }
 }else if(a == joptionpane.no_option) {
  ta.settext("");
 }
 }
 
 }
 }
 
 //打开监听器,从硬盘目录打开文本文件
 class openlistener implements actionlistener{
 public void actionperformed(actionevent e) {
 jfilechooser jf = new jfilechooser();
 filenameextensionfilter filter = new filenameextensionfilter("txt","txt");
 jf.setacceptallfilefilterused(false);
 jf.setfilefilter(filter);
 int value = jf.showopendialog(null);
 if(value == jfilechooser.approve_option) {
 file file = jf.getselectedfile();
 try {
  string encoding = "gbk";//考虑编码格式
  inputstreamreader isr = new inputstreamreader(new fileinputstream(file),encoding);
  bufferedreader br = new bufferedreader(isr);
  string str = null;
  while((str = br.readline()) != null) {
  ta.append(str);
  }
 } catch (filenotfoundexception e1) {
  // todo auto-generated catch block
  e1.printstacktrace();
 } catch (ioexception e1) {
  // todo auto-generated catch block
  e1.printstacktrace();
 }
 }
 }
 }
 
 //保存监听器,以电脑时间命名
 class savelistener implements actionlistener{
 public void actionperformed(actionevent e) {
 string name = null;
 simpledateformat df = new simpledateformat("yyyy-mm-dd hh-mm-ss");
 name = df.format(system.currenttimemillis());
 file file = new file("h:/programing/workspace/"+name+".txt");
 string str = ta.gettext();
 byte[] buf = str.getbytes();
 if(!str.equals("")) {
 try {
  outputstream os = new fileoutputstream(file);
  os.write(buf);
  os.flush();
  os.close();
 } catch (ioexception e2) {
  e2.printstacktrace();
 }
 }
 }
 }
 
 //另存为监听器 
 class saveaslistener implements actionlistener{
 public void actionperformed(actionevent e) {
 save();
 }
 }
 
 //剪切监听器 
 class cutaslistener implements actionlistener{
 public void actionperformed(actionevent e) {
 shearplate = ta.getselectedtext();
 int startposi = ta.getselectionstart();
 int endposi = ta.getselectionend();
 stringbuffer sb = new stringbuffer(ta.gettext());
 sb.delete(startposi, endposi);
 ta.settext(sb.tostring());
 }
 }
 
 //复制监听器 
 class copyaslistener implements actionlistener{
 public void actionperformed(actionevent e) {
 shearplate = ta.getselectedtext();
 }
 }
 
 //粘贴监听器 
 class pasteaslistener implements actionlistener{
 public void actionperformed(actionevent e) {
 int position = ta.getcaretposition();
 stringbuilder sb = new stringbuilder(ta.gettext());
 sb.insert(position, shearplate);
 ta.settext(sb.tostring());
 ta.setcaretposition(position+shearplate.length());
 }
 }
 
 //自动换行监听器 
 class autowrapaslistener implements itemlistener{
 
 @override
 public void itemstatechanged(itemevent e) {
 if(autowrap.getstate()) {
  isautowrap = true;
  ta.setlinewrap(isautowrap);
  jsp.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_never);
  jsp.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_as_needed);
 }else {
  isautowrap = false;
  ta.setlinewrap(isautowrap);
  jsp.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_as_needed);
  jsp.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_never);
 }
 }
 }
 
 //字体监听器 
 class fontlistener implements actionlistener{
 jframe jf = new jframe("字体");
 jpanel jp = new jpanel();
 buttonpanel fontbutton = new buttonpanel("字体",new string[] {"宋体","黑体","楷体","吴祎晗1.0"});
 buttonpanel shapebutton = new buttonpanel("字形",new string[] {"常规","粗体","斜体","粗体&斜体"});
 buttonpanel sizebutton = new buttonpanel("字号",new string[] {"12","16","20","24","26"});
 public void actionperformed(actionevent e) {
 jp.setborder(new titledborder(new etchedborder(),"字体选择",titledborder.center,titledborder.top));
 jp.add(fontbutton);
 jp.add(shapebutton);
 jp.add(sizebutton);
 jf.add(jp);
 jf.pack();
 jf.setlocationrelativeto(f);
 jf.setvisible(true);
 jf.addwindowlistener(new windowadapter() {
  public void windowclosing(windowevent e) {
  fontstr = fontbutton.getselec();
  sizestr = sizebutton.getselec();
  font = new font(fontstr, shapeval(shapebutton.getselec()), integer.parseint(sizestr));
  ta.setfont(font);
  }
 });
 }
 
 public int shapeval(string str) {
 int a = 0;
 switch (str) {
 case "常规":
  a = 0;
  break;
 case "粗体":
  a = 1;
  break;
 case "斜体":
  a = 2;
  break;
 case "粗体&斜体":
  a = 3;
  break;
 }
 return a;
 }
 }
 
}
 
//定义一个jpanel类扩展类,该类的对象包含多个纵向排列的
//jradiobutton控件,且panel扩展类可以指定一个字符串作为titledborder
class buttonpanel extends jpanel{
 private buttongroup group;
 public buttonpanel(string title, string[] options) {
 setborder(borderfactory.createtitledborder(borderfactory.createetchedborder(),title));
 setlayout(new boxlayout(this, boxlayout.y_axis));
 group = new buttongroup();
 for(int i=0; options!=null&&i<options.length; i++) {
 jradiobutton b = new jradiobutton(options[i]);
 b.setactioncommand(options[i]);
 this.add(b);
 group.add(b);
 if(title.equals("字号")) {
 b.setselected(i == 2);
 }else {
 b.setselected(i == 0);
 }
 }
 }
 public string getselec() {
 return group.getselection().getactioncommand();
 }
}

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

上一篇:

下一篇: