记事本java_java实现Windows记事本

import java.awt.*;import javax.swing.*;import javax.swing.event.*; //导入菜单

importjavax.swing.filechooser.FileNameExtensionFilter;import java.util.Date; //获取系统的时间

import java.text.SimpleDateFormat; //控制系统的时间格式

import java.awt.event.*;import java.io.*;import java.util.regex.*;public class JF_Notpad extends JFrame implementsActionListener,WindowListener{//menuBar

privateJMenuBar mb;privateJMenu file,edit,format,view,help;//menuToolBar

privateJToolBar tool;private static intBUTTONWIDTH,BUTTONHEIGHT;privateJButton create,unfold,copy,cut,paste,pasteShortcut,delete;//File//JMenu下还可以有二级菜单

privateJMenu newNot;//JMenuItem不可以有其他菜单

privateJMenuItem notpad,model,open,save,saveAs,pageSet,print,exit;//Edit

privateJMenuItem undoE,cutE,copyE,pasteE,deleteE,findE,findNextE,replaceE,goToE,selectAllE,timeDateE;//Format

privateJMenuItem wordWrap,font;//View

privateJMenuItem statusBar;//Help

privateJMenuItem viewHelp,about;//leftSpace

privateJPanel leftSpace;//TextArea

privateJTextArea content;privateJScrollPane scroll;//openUrl,saveUrl

private static String openUrl="E:/notpad.txt"; //保存之前打开文件的路径

private static String saveUrl="E:/notpad.txt"; //保存之前保存文件的路径

private boolean saved=false; //判断文件是否保存过//contentFont 字体

privateFont contentFont;//是否在打开保存面板后,还应该打开选择文件面板

private boolean openFileBool=false;public static voidmain(String[] args) {

JF_Notpad not=newJF_Notpad();

}public static JButton changeIconSize(JButton button,String url,int width,intheight,String tip){

button.setBounds(0,0,width,height);

ImageIcon buttonImg=newImageIcon(url);//改变图片的大小

Image temp=buttonImg.getImage().getScaledInstance(button.getWidth(), button.getHeight(), buttonImg.getImage().SCALE_DEFAULT);

button=new JButton(newImageIcon(temp));

button.setToolTipText(tip);//提示

returnbutton;

}public static Image changeImageSize(String url,int width,intheight){

ImageIcon Img=newImageIcon(url);//改变图片的大小

Image temp=Img.getImage().getScaledInstance(width, height, Img.getImage().SCALE_DEFAULT);returntemp;

}

JF_Notpad(){//menubar

mb=newJMenuBar();

file=new JMenu("File(F)");

file.setMnemonic('F');

edit=new JMenu("Edit(E)");

edit.setMnemonic('E');

format=new JMenu("Format(O)");

format.setMnemonic('O');

view=new JMenu("View(V)");

view.setMnemonic('V');

help=new JMenu("Help(H)");

help.setMnemonic('H');//menuToolBar

tool=newJToolBar();

BUTTONWIDTH=22;

BUTTONHEIGHT=22;

create=newJButton();

create=JF_Notpad.changeIconSize(create, "img/notpad/news.png", BUTTONWIDTH, BUTTONWIDTH, "New");

unfold=newJButton();

unfold=JF_Notpad.changeIconSize(unfold, "img/notpad/open.png", BUTTONWIDTH, BUTTONWIDTH, "Open");

copy=newJButton();

copy=JF_Notpad.changeIconSize(copy, "img/notpad/copy.png", BUTTONWIDTH, BUTTONWIDTH, "Copy");

cut=newJButton();

cut=JF_Notpad.changeIconSize(cut, "img/notpad/cut.png", BUTTONWIDTH, BUTTONWIDTH, "Cut");

paste=newJButton();

paste=JF_Notpad.changeIconSize(unfold, "img/notpad/paste.png", BUTTONWIDTH, BUTTONWIDTH, "Paste");

pasteShortcut=newJButton();

pasteShortcut=JF_Notpad.changeIconSize(pasteShortcut, "img/notpad/pasteShortcut.png", BUTTONWIDTH, BUTTONWIDTH, "PasteShortcut");

delete=newJButton();

delete=JF_Notpad.changeIconSize(delete, "img/notpad/delete.png", BUTTONWIDTH, BUTTONWIDTH, "Delete");//menuToolBarListener

create.setActionCommand("notpad");

create.addActionListener(this);

unfold.setActionCommand("open");

unfold.addActionListener(this);

copy.setActionCommand("copyE");

copy.addActionListener(this);

cut.setActionCommand("cutE");

cut.addActionListener(this);

paste.setActionCommand("pasteE");

paste.addActionListener(this);

pasteShortcut.setActionCommand("pasteShortcut");

pasteShortcut.addActionListener(this);

delete.setActionCommand("deleteE");

delete.addActionListener(this);//file

newNot=new JMenu("New(N)");

newNot.setMnemonic('N');

ImageIcon icon=new ImageIcon(this.changeImageSize("img/notpad/logo.png", BUTTONWIDTH-5,BUTTONHEIGHT-5));

notpad=new JMenuItem("Notpad",icon);

model=new JMenuItem("Model");

icon=new ImageIcon(this.changeImageSize("img/notpad/open.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));

open=new JMenuItem("Open",icon);

icon=new ImageIcon(this.changeImageSize("img/notpad/save.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));

save=new JMenuItem("Save",icon);

icon=new ImageIcon(this.changeImageSize("img/notpad/saveAs.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));

saveAs=new JMenuItem("Save As...",icon);

pageSet=new JMenuItem("PageSet");

print=new JMenuItem("Print");

icon=new ImageIcon(this.changeImageSize("img/notpad/exit.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));

exit=new JMenuItem("Exit",icon);//FileListener

notpad.setActionCommand("notpad");

notpad.addActionListener(this);

model.setActionCommand("model");

model.addActionListener(this);

open.setActionCommand("open");

open.addActionListener(this);

save.setActionCommand("save");

save.addActionListener(this);

saveAs.setActionCommand("saveAs");

saveAs.addActionListener(this);

pageSet.setActionCommand("pageSet");

pageSet.addActionListener(this);

print.setActionCommand("print");

print.addActionListener(this);

exit.setActionCommand("exit");

exit.addActionListener(this);//Edit

undoE=new JMenuItem("Undo");

icon=new ImageIcon(this.changeImageSize("img/notpad/cut.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));

cutE=new JMenuItem("Cut",icon);

icon=new ImageIcon(this.changeImageSize("img/notpad/copy.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));

copyE=new JMenuItem("Copy",icon);

icon=new ImageIcon(this.changeImageSize("img/notpad/paste.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));

pasteE=new JMenuItem("Paste",icon);

icon=new ImageIcon(this.changeImageSize("img/notpad/delete.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));

deleteE=new JMenuItem("Delete",icon);

findE=new JMenuItem("Find...");

findNextE=new JMenuItem("Find Next");

replaceE=new JMenuItem("Replace...");

goToE=new JMenuItem("Go To..");

selectAllE=new JMenuItem("Select All");

timeDateE=new JMenuItem("Time/Data");//EditListener

undoE.setActionCommand("undoE");

undoE.addActionListener(this);

cutE.setActionCommand("cutE");

cutE.addActionListener(this);

copyE.setActionCommand("copyE");

copyE.addActionListener(this);

pasteE.setActionCommand("pasteE");

pasteE.addActionListener(this);

deleteE.setActionCommand("deleteE");

deleteE.addActionListener(this);

findE.setActionCommand("findE");

findE.addActionListener(this);

findNextE.setActionCommand("findNextE");

findNextE.addActionListener(this);

replaceE.setActionCommand("replaceE");

replaceE.addActionListener(this);

goToE.setActionCommand("goToE");

goToE.addActionListener(this);

selectAllE.setActionCommand("selectAllE");

selectAllE.addActionListener(this);

timeDateE.setActionCommand("timeDateE");

timeDateE.addActionListener(this);//Format

icon=new ImageIcon(this.changeImageSize("img/notpad/wordWrap.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));

wordWrap=new JMenuItem("Word Wrap",icon);

icon=new ImageIcon(this.changeImageSize("img/notpad/font.png", BUTTONWIDTH-8, BUTTONHEIGHT-8));

font=new JMenuItem("Font...",icon);//FormatListener

wordWrap.setActionCommand("wordWrap");

wordWrap.addActionListener(this);

font.setActionCommand("font");

font.addActionListener(this);//View

statusBar=new JMenuItem("Status Bar");//ViewListener

statusBar.setActionCommand("statusBar");

statusBar.addActionListener(this);//Help

viewHelp=new JMenuItem("View Help");

icon=new ImageIcon(this.changeImageSize("img/notpad/about.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));

about=new JMenuItem("About NotePad",icon);//HelpListener

viewHelp.setActionCommand("viewHelp");

viewHelp.addActionListener(this);

about.setActionCommand("about");

about.addActionListener(this);//leftSpace

leftSpace=newJPanel();//contentFont

contentFont=new Font("KaiTi",Font.PLAIN,20);//textArea

content=newJTextArea();

content.setFont(contentFont);

scroll=newJScrollPane(content);//scroll.setHorizontalScrollBar(content);//openUrl,saveUrl//openUrl="E:/notpad.txt";//saveUrl="E:/notpad.txt";//saved=false;//saveFilePanel//Save saveF=new Save("Save",this);//添加组件,从下一级开始装

newNot.add(notpad); newNot.add(model);

file.add(newNot); file.add(open); file.add(save); file.add(saveAs); file.addSeparator(); file.add(pageSet); file.add(print); file.addSeparator(); file.add(exit);

edit.add(undoE);

edit.addSeparator();

edit.add(cutE);

edit.add(copyE);

edit.add(pasteE);

edit.add(deleteE);

edit.addSeparator();

edit.add(findE);

edit.add(findNextE);

edit.add(replaceE);

edit.add(goToE);

edit.addSeparator();

edit.add(selectAllE);

edit.add(timeDateE);

format.add(wordWrap);

format.add(font);

view.add(statusBar);

help.add(viewHelp);

help.addSeparator();

help.add(about);

mb.add(file); mb.add(edit); mb.add(format); mb.add(view); mb.add(help);

tool.add(create);

tool.add(unfold);

tool.add(copy);

tool.add(cut);

tool.add(paste);

tool.add(pasteShortcut);

tool.add(delete);this.setJMenuBar(mb);this.add(tool,BorderLayout.NORTH);this.add(leftSpace,BorderLayout.WEST);this.add(scroll);this.setTitle("Notpad");this.setIconImage(new ImageIcon("img/notpad/logo.png").getImage());this.setLocation(300,300);this.setSize(400,400);this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);this.setVisible(true);

}private String origContent=null; //保存一开始的文件或者保存过的文件

private String copyContent=null;

@Overridepublic voidactionPerformed(ActionEvent e) {switch(e.getActionCommand()){case "notpad":{

System.out.println("notpad");this.isChangeFile("txt");

}break;case "model":{

System.out.println("model");this.isChangeFile("java");

}break;case "open":{

System.out.println("open");//防止第一次打开空文件时,还没保存

if(this.origContent==null){this.saved=true;

}if(saved){this.openFilePanel();

}else{

Save saveF=new Save("Save",this);this.openFilePanel();

}

}break;case "save":{

System.out.println("save");this.saveFile();//this.setSaved(true);

}break;case "saveAs":{

System.out.println("saveAs");

String str=content.getText();//选择保存文件的路径

JFileChooser fileChoose=null;if(saveUrl==null){

fileChoose=newJFileChooser();

}else{

fileChoose=newJFileChooser(saveUrl);

}

FileNameExtensionFilter filter=new FileNameExtensionFilter("txt & java","java","txt"); //设置可以识别的文件

fileChoose.setFileFilter(filter);

fileChoose.setDialogTitle("Save File");

fileChoose.showSaveDialog(save);

fileChoose.setVisible(true);

File outFile=fileChoose.getSelectedFile();

saveUrl=fileChoose.getSelectedFile().getPath();

PrintStream p=null;try{

p=newPrintStream(outFile);

System.setOut(p);

System.out.print(str);

}catch(Exception a){

System.out.println("SaveAS File Error!!");

}finally{

p.close();

}

}break;case "pageSet":{

System.out.println("pageSet");

}break;case "print":{

System.out.println("print");

}break;case "exit":{

System.out.println("exit");

System.exit(-1);

}break;case "undoE":{

System.out.println("undoE");

}break;case "cutE":{

System.out.println("cutE");if(this.content.getSelectedText()!=null){this.copyContent=this.content.getSelectedText();

}this.content.replaceSelection("");

}break;case "copyE":{

System.out.println("copyE");if(this.content.getSelectedText()!=null){this.copyContent=this.content.getSelectedText();

}

}break;case "pasteE":{

System.out.println("pasteE");if(copyContent!=null){this.content.replaceRange("", content.getSelectionStart(), content.getSelectionEnd());

}this.content.insert(copyContent, this.content.getSelectionStart());

}break;case "deleteE":{

System.out.println("deleteE");this.content.replaceSelection("");

}break;case "findE":{

System.out.println("findE");

}break;case "findNextE":{

System.out.println("findNextE");

}break;case "replaceE":{

System.out.println("replaceE");

}break;case "goToE":{

System.out.println("goToE");

}break;case "selectAllE":{

System.out.println("selectAllE");this.content.selectAll();

}break;case "timeDateE":{

System.out.println("timeDateE");

SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//System.out.println(df.format(new Date()));

this.content.insert(df.format(newDate()), content.getSelectionStart());

}break;case "wordWrap":{

System.out.println("wordWrap");

ImageIcon icon;if(this.content.getLineWrap()){this.content.setLineWrap(false);

icon=new ImageIcon(this.changeImageSize("img/notpad/wordWrap.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));this.wordWrap.setIcon(icon);

}else{this.content.setLineWrap(true);

icon=new ImageIcon(this.changeImageSize("img/notpad/wordWrapNo.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));this.wordWrap.setIcon(icon);

}

}break;case "font":{

System.out.println("font");

FontPanel f=new FontPanel("Font",this);//this.content.setFont(new Font(f.getFontN(),f.getFontSytleN(),f.getSizeN()));

}break;case "statusBar":{

System.out.println("statusBar");

}break;case "viewHelp":{

System.out.println("viewHelp");

}break;case "about":{

System.out.println("about");

About a=new About("About","1.0","feiquan","228332xxxxx@qq.com","xxx-xxxx-xxxx","https://www.baidu.com");

}break;case "pasteShortcut":{

System.out.println("pasteShortcut");

}break;

}

}

@Overridepublic voidwindowOpened(WindowEvent e) {//TODO Auto-generated method stub

}

@Overridepublic voidwindowClosing(WindowEvent e) {//TODO Auto-generated method stub

System.out .println("弹出是否选择保存的对话框");if(saved){

Save saveF=new Save("Save",this);

}

}

@Overridepublic voidwindowClosed(WindowEvent e) {//TODO Auto-generated method stub

}

@Overridepublic voidwindowIconified(WindowEvent e) {//TODO Auto-generated method stub

}

@Overridepublic voidwindowDeiconified(WindowEvent e) {//TODO Auto-generated method stub

}

@Overridepublic voidwindowActivated(WindowEvent e) {//TODO Auto-generated method stub

}

@Overridepublic voidwindowDeactivated(WindowEvent e) {//TODO Auto-generated method stub

}public staticString getOpenUrl() {returnopenUrl;

}public static voidsetOpenUrl(String openUrl) {

JF_Notpad.openUrl=openUrl;

}public staticString getSaveUrl() {returnsaveUrl;

}public static voidsetSaveUrl(String saveUrl) {

JF_Notpad.saveUrl=saveUrl;

}//保存文件其中包含了,判断文件是否存在

public voidsaveFile(){

String str=this.content.getText();

System.out.println("正在保存:\n"+str);//判断文件是否存在,不存在则创建文件

File file=newFile(openUrl);if(!file.exists()){try{

file.createNewFile();

}catch(Exception a){

System.out.println("创建文件 "+file.getAbsolutePath()+" 失败!!");

}

}

PrintStream p=null;try{

p=new PrintStream(newFile(openUrl));

System.setOut(p);

System.out.print(str);

saved=true;

}catch(Exception a){

System.out.println("Save File Error!!");

saved=false;

}finally{

p.close();

}

}//判断文件是否修改过,并根据传入的属性判断新建那个文件

public voidisChangeFile(String properties){

System.out.println("是否保存文件:"+saved);//判断文件是否修改过

if(!(this.content.getText().equals(origContent))){

saved=false;

}//如果内容为空,也视为已经保存

if(this.content.getText()==null||this.content.getText().equals(""))saved=true;

System.out.println("判断后是否保存文件:"+saved);if(saved){switch(properties){case "txt":{this.content.setText("");this.setTitle("notpad");

origContent="";

}break;case "java":{this.setTitle("notpad");

String str="public class Notpad {\n\tpublic static void main (String[] ages) {\n\t\t\n\t}\n}";this.content.setText(str);

origContent=str;

}break;

}

}else{

System.out .println("弹出是否选择保存的对话框");

Save saveF=new Save("Save",this);

}

System.out.println("经过保存面板后是否保存文件:"+saved);

}//打开选择打开文件面板,并选择文件

public voidopenFilePanel(){

origContent=this.content.getText();//显示选择文件面板

JFileChooser fileChoose=null;if(openUrl==null){

fileChoose=newJFileChooser();

}else{

fileChoose=newJFileChooser(openUrl);

}

FileNameExtensionFilter filter=new FileNameExtensionFilter("txt & java","java","txt");

fileChoose.setFileFilter(filter);

fileChoose.setDialogTitle("Open File");

fileChoose.showOpenDialog(null);

fileChoose.setVisible(true);//得到文件

String url=fileChoose.getSelectedFile().getAbsolutePath(); //得到选择文件的路径//设置保存面板显示的路径

JF_Notpad.setOpenUrl(url);

Save saveF=new Save("Save",this);

saveF.setVisible(false);

saveF.setUrl(url);

JLabel temp=saveF.getUrlL();

temp.setText(url+" ?");

saveF.setUrlL(temp);

String name=fileChoose.getSelectedFile().getName();this.setTitle(name+" - Notpad"); //设置主窗口标题//文件流

FileReader fRead=null; BufferedReader buffRead=null;try{

fRead=newFileReader(openUrl);

buffRead=newBufferedReader(fRead);

String str=""; String readLine="";

readLine=buffRead.readLine();while(readLine!=null){

str=str+readLine+"\r\n";

readLine=buffRead.readLine();

}

content.setText(str);

origContent=str;

}catch(Exception a){

System.out.println("Open file error!!");

}finally{try{

fRead.close();

buffRead.close();

}catch(Exception b){

System.out.println("Open file stream error!!");

}

}

}public booleanisSaved() {returnsaved;

}public void setSaved(booleansaved) {this.saved =saved;

}public booleangetOpenFileBool() {returnopenFileBool;

}public void setOpenFileBool(booleanopenFileBool) {this.openFileBool =openFileBool;

}publicString getOrigContent() {returnorigContent;

}public voidsetOrigContent(String origContent) {this.origContent =origContent;

}publicJTextArea getContent() {returncontent;

}public voidsetContent(JTextArea content) {this.content =content;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值