Java 记事本设计完整代码_第1页
Java 记事本设计完整代码_第2页
Java 记事本设计完整代码_第3页
Java 记事本设计完整代码_第4页
Java 记事本设计完整代码_第5页
已阅读5页,还剩3页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

功能界面展示:记事本完整代码:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.CaretEvent;importjavax.swing.event.CaretListener;importjava.io.*;@SuppressWarnings("serial")classmynotepadextendsJFrame{ Filefile=null; JTextAreacontent=newJTextArea(); JFileChooseropenfile=newJFileChooser(); JColorChooseropencolor=newJColorChooser(); JDialogabout=newJDialog(this); JMenuBarmenu=newJMenuBar(); JFramep=newJFrame(); booleanWordWrap=false; booleanstatus=false; mynotepad(){ initTextContent(); initMenu(); } voidinitTextContent(){ JScrollPanescroll=newJScrollPane(content); getContentPane().add(scroll); this.getContentPane().add(scroll,BorderLayout.CENTER); } JMenu[]menus=newJMenu[]{newJMenu("文件(F)"),newJMenu("编辑(E)"), newJMenu("格式(O)"),newJMenu("查找(V)"),newJMenu("帮助(H)")}; JMenuItemoptionofmenu[][]=newJMenuItem[][]{ {newJMenuItem("新建"),newJMenuItem("打开"),newJMenuItem("保存

"), newJMenuItem("退出")}, {newJMenuItem("复制"),newJMenuItem("剪切"),newJMenuItem("粘

贴"), newJMenuItem("全选"),newJMenuItem("查找"),new

JMenuItem("替换")}, {newJCheckBoxMenuItem("自动换行(W)"),newJMenuItem("字体(F)")}, {newJMenuItem("状态栏(S)")}, {newJMenuItem("帮助")}}; JLabeljs=newJLabel(); voidinitMenu(){ for(inti=0;i<menus.length;i++){ menu.add(menus[i]); for(intj=0;j<optionofmenu[i].length;j++){ menus[i].add(optionofmenu[i][j]); optionofmenu[i][j].addActionListener(action); } } menu.add(js); menus[0].setMnemonic(KeyEvent.VK_F); menus[1].setMnemonic(KeyEvent.VK_E); menus[2].setMnemonic(KeyEvent.VK_O); menus[3].setMnemonic(KeyEvent.VK_V); menus[4].setMnemonic(KeyEvent.VK_H); optionofmenu[2][0].setMnemonic(KeyEvent.VK_W); optionofmenu[2][1].setMnemonic(KeyEvent.VK_F); optionofmenu[3][0].setMnemonic(KeyEvent.VK_S); optionofmenu[1][4].addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ finalJDialogdialog=newJDialog(about,"查找字符串...",

true); dialog.setBounds(560,250,310,130); JLabelfind=newJLabel("请输入字符串:"); finalJTextFieldfindtext=newJTextField(1); JButtonjbu=newJButton("查找"); dialog.setLayout(null); find.setBounds(10,30,90,20); findtext.setBounds(100,30,90,20); jbu.setBounds(200,30,80,20); dialog.add(find); dialog.add(findtext); dialog.add(jbu); jbu.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ Stringtext=content.getText(); Stringstr=findtext.getText(); intend=text.length(); intlen=str.length(); intstart=content.getSelectionEnd(); if(start==end){ start=0; } for(;start<=end-len;start++){ if(text.substring(start,start+

len).equals(str)){ content.setSelectionStart

(start); content.setSelectionEnd

(start+len); return; } } //若找不到待查字符串,则将光标置于末尾 content.setSelectionStart(end); content.setSelectionEnd(end); } }); dialog.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ dialog.dispose(); } }); dialog.setResizable(false); dialog.setVisible(true); } }); optionofmenu[1][5].addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ finalJDialogdialog=newJDialog(about,"字符串替换...",

true); dialog.setBounds(560,250,310,180); finalJLabeltihuan=newJLabel("请输入要替换的字符串

:"); finalJLabelmubiao=newJLabel("请输入替换后的字符串

:"); finalJTextFieldjtf1=newJTextField(10); finalJTextFieldjtf2=newJTextField(10); JButtonjb=newJButton("替换"); dialog.setLayout(null); tihuan.setBounds(10,30,150,20); mubiao.setBounds(10,70,150,20); jtf1.setBounds(160,30,110,20); jtf2.setBounds(160,70,110,20); jb.setBounds(100,110,80,20); dialog.add(tihuan); dialog.add(mubiao); dialog.add(jtf1); dialog.add(jtf2); dialog.add(jb); jb.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ if(content.getText().contains

(jtf1.getText())) content.setText(content.getText

().replaceFirst( jtf1.getText(),

jtf2.getText())); else{ JOptionPane.showConfirmDialog(null, "没找到"+

jtf1.getText()); } } }); dialog.setResizable(false); dialog.setVisible(true); } }); JToolBarjb=newJToolBar(); jb.setBackground(newColor(236,233,216)); JLabeljl1=newJLabel(String.format("字符总长:0总行数:0")); jl1.setFont(newFont("宋体",0,12)); jb.add(jl1); JSeparators=newJSeparator(); s.setOrientation(SwingConstants.VERTICAL); jb.add(s); JLabeljl2=newJLabel(String.format("当前行:0当前列:0")); jl2.setFont(newFont("宋体",0,12)); jb.add(jl2); jb.setFloatable(false); jb.setVisible(status); this.getContentPane().add(jb,BorderLayout.SOUTH); content.addCaretListener(newCaretListener(){ @Override publicvoidcaretUpdate(CaretEvente){ //TODOAuto-generatedmethodstub JTextAreaeditArea=(JTextArea)e.getSource();intlinenum=1;intcolumnnum=1;try{intcaretpos=editArea.getCaretPosition();linenum=editArea.getLineOfOffset(caretpos);columnnum=caretpos-editArea.getLineStartOffset(linenum);linenum+=1;}catch(Exceptionex){}jl1.setText(String.format("字符总长:%d总行数:%d",editArea.getText

().length(),editArea.getLineCount()));jl2.setText(String.format("当前行:%d当前列:%d",linenum,columnnum)); }}); optionofmenu[3][0].addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){ status=!status; if(status){ jb.setVisible(status); } else{ jb.setVisible(status); }} }); this.setJMenuBar(menu); } ActionListeneraction=newActionListener(){ @SuppressWarnings("unused") publicvoidactionPerformed(ActionEvente){ Stringname=e.getActionCommand(); JMenuItemMI=(JMenuItem)e.getSource(); if("新建".equals(name)){ content.setText(""); file=null; }elseif("打开".equals(name)){ if(file!=null) openfile.setSelectedFile(file); intreturnVal=openfile.showOpenDialog(mynotepad.this); if(returnVal==JFileChooser.APPROVE_OPTION){ file=openfile.getSelectedFile(); unfold(); } }elseif("保存".equals(name)){ if(file!=null) openfile.setSelectedFile(file); intreturnVal=openfile.showSaveDialog(mynotepad.this); if(returnVal==JFileChooser.APPROVE_OPTION){ file=openfile.getSelectedFile(); saving(); } }elseif("退出".equals(name)){ mynotepadf=newmynotepad(); ints=JOptionPane.showConfirmDialog(f,"退出?","退出", JOptionPane.YES_NO_OPTION); if(s==JOptionPane.YES_OPTION) System.exit(0); }elseif("剪切".equals(name)){ content.cut(); }elseif("复制".equals(name)){ content.copy(); }elseif("粘贴".equals(name)){ content.paste(); }elseif(e.getActionCommand().equals("全选")){ content.selectAll(); }elseif(e.getActionCommand().equals("自动换行(W)")){ WordWrap=!(WordWrap); content.setLineWrap(WordWrap); }elseif(e.getActionComm

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论