Java记事本源代码(完整)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

/***作品:记事本*作者:****功能:简单的文字编辑*/importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjavax.swing.*;importjavax.swing.event.ChangeEvent;importjavax.swing.event.ChangeListener;classNotePadextendsJFrame{privateJMenuBarmenuBar;privateJMenufielMenu,editMenu,formMenu,aboutMenu;privateJMenuItemnewMenuItem,openMenuItem,saveMenuItem,exitMenuItem;privateJMenuItemcutMenuItem,copyMenuItem,pasteMenuItem,foundItem,replaceItem,selectAll;privateJMenuItemfont,about;privateJTextAreatextArea;privateJFramefoundFrame,replaceFrame;privateJCheckBoxMenuItemwrapline;privateJTextFieldtextField1=newJTextField(15);privateJTextFieldtextField2=newJTextField(15);privateJButtonstartButton,replaceButton,reallButton;intstart=0;Stringvalue;Filefile=null;JFileChooserfileChooser=newJFileChooser();booleanwrap=false;publicNotePad(){//创建文本域textArea=newJTextArea();add(newJScrollPane(textArea),BorderLayout.CENTER);//创建文件菜单及文件菜单项fielMenu=newJMenu(文件);fielMenu.setFont(newFont(微软雅黑,0,15));newMenuItem=newJMenuItem(新建,newImageIcon(icons\\new24.gif));newMenuItem.setFont(newFont(微软雅黑,Font.BOLD,13));newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));newMenuItem.addActionListener(listener);openMenuItem=newJMenuItem(打开,newImageIcon(icons\\open24.gif));openMenuItem.setFont(newFont(微软雅黑,Font.BOLD,13));openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));openMenuItem.addActionListener(listener);saveMenuItem=newJMenuItem(保存,newImageIcon(icons\\save.gif));saveMenuItem.setFont(newFont(微软雅黑,Font.BOLD,13));saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));saveMenuItem.addActionListener(listener);exitMenuItem=newJMenuItem(退出,newImageIcon(icons\\exit24.gif));exitMenuItem.setFont(newFont(微软雅黑,Font.BOLD,13));exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));exitMenuItem.addActionListener(listener);//创建编辑菜单及菜单项editMenu=newJMenu(编辑);editMenu.setFont(newFont(微软雅黑,0,15));cutMenuItem=newJMenuItem(剪切,newImageIcon(icons\\cut24.gif));cutMenuItem.setFont(newFont(微软雅黑,Font.BOLD,13));cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));cutMenuItem.addActionListener(listener);copyMenuItem=newJMenuItem(复制,newImageIcon(icons\\copy24.gif));copyMenuItem.setFont(newFont(微软雅黑,Font.BOLD,13));copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));copyMenuItem.addActionListener(listener);pasteMenuItem=newJMenuItem(粘贴,newImageIcon(icons\\paste24.gif));pasteMenuItem.setFont(newFont(微软雅黑,Font.BOLD,13));pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));pasteMenuItem.addActionListener(listener);foundItem=newJMenuItem(查找);foundItem.setFont(newFont(微软雅黑,Font.BOLD,13));foundItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));foundItem.addActionListener(listener);replaceItem=newJMenuItem(替换);replaceItem.setFont(newFont(微软雅黑,Font.BOLD,13));replaceItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK));replaceItem.addActionListener(listener);selectAll=newJMenuItem(全选);selectAll.setFont(newFont(微软雅黑,Font.BOLD,13));selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));selectAll.addActionListener(listener);//创建格式菜单及菜单项formMenu=newJMenu(格式);formMenu.setFont(newFont(微软雅黑,0,15));wrapline=newJCheckBoxMenuItem(自动换行);wrapline.setFont(newFont(微软雅黑,Font.BOLD,13));wrapline.addActionListener(listener);wrapline.addChangeListener(newChangeListener(){publicvoidstateChanged(ChangeEvente){if(wrapline.isSelected()){textArea.setLineWrap(true);}elsetextArea.setLineWrap(false);}});font=newJMenuItem(字体);font.setFont(newFont(微软雅黑,Font.BOLD,13));font.addActionListener(listener);//创建关于菜单aboutMenu=newJMenu(关于);aboutMenu.setFont(newFont(微软雅黑,0,15));about=newJMenuItem(记事本……);about.setFont(newFont(微软雅黑,Font.BOLD,13));about.addActionListener(listener);//添加文件菜单项fielMenu.add(newMenuItem);fielMenu.add(openMenuItem);fielMenu.add(saveMenuItem);fielMenu.addSeparator();fielMenu.add(exitMenuItem);//添加编辑菜单项editMenu.add(cutMenuItem);editMenu.add(copyMenuItem);editMenu.add(pasteMenuItem);editMenu.add(foundItem);editMenu.add(replaceItem);editMenu.addSeparator();editMenu.add(selectAll);//添加格式菜单项formMenu.add(wrapline);formMenu.add(font);//添加关于菜单项aboutMenu.add(about);//添加菜单menuBar=newJMenuBar();menuBar.add(fielMenu);menuBar.add(editMenu);menuBar.add(formMenu);menuBar.add(aboutMenu);setJMenuBar(menuBar);//创建两个框架,用作查找和替换foundFrame=newJFrame();replaceFrame=newJFrame();//创建两个文本框textField1=newJTextField(15);textField2=newJTextField(15);startButton=newJButton(开始);startButton.addActionListener(listener);replaceButton=newJButton(替换为);replaceButton.addActionListener(listener);reallButton=newJButton(全部替换);reallButton.addActionListener(listener);}//创建菜单项事件监听器ActionListenerlistener=newActionListener(){publicvoidactionPerformed(ActionEvente){Stringname=e.getActionCommand();if(e.getSource()instanceofJMenuItem){if(新建.equals(

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功