收藏 分销(赏)

Java记事本源代码(完整).doc

上传人:二*** 文档编号:4576795 上传时间:2024-09-30 格式:DOC 页数:12 大小:67KB 下载积分:5 金币
下载 相关 举报
Java记事本源代码(完整).doc_第1页
第1页 / 共12页
本文档共12页,全文阅读请下载到手机保存,查看更方便
资源描述
. . . . /** * 作品:记事本 * *** * 功能:简单的文字编辑 */ import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; classNotePadextends JFrame{ private JMenuBar menuBar; private JMenu fielMenu,editMenu,formMenu,aboutMenu; private JMenuItem newMenuItem,openMenuItem,saveMenuItem,exitMenuItem; private JMenuItem cutMenuItem,copyMenuItem,pasteMenuItem,foundItem,replaceItem,selectAll; private JMenuItem font,about; private JTextArea textArea; private JFrame foundFrame,replaceFrame; private JCheckBoxMenuItem wrapline; private JTextField textField1=new JTextField(15); private JTextField textField2=new JTextField(15); private utton startButton,replaceButton,reallButton; intstart=0; String value; File file=null; JFileChooser fileChooser=new JFileChooser(); booleanwrap=false; public NotePad(){ //创建文本域 textArea=new JTextArea(); add(new JScrollPane(textArea),BorderLayout.CENTER); //创建文件菜单与文件菜单项 fielMenu=new JMenu("文件"); fielMenu.setFont(new Font("微软雅黑",0,15)); newMenuItem=new JMenuItem("新建",new ImageIcon("icons\\new24.gif")); newMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); newMenuItem.addActionListener(listener); openMenuItem=new JMenuItem("打开",new ImageIcon("icons\\open24.gif")); openMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); openMenuItem.addActionListener(listener); saveMenuItem=new JMenuItem("保存",new ImageIcon("icons\\save.gif")); saveMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); saveMenuItem.addActionListener(listener); exitMenuItem=new JMenuItem("退出",new ImageIcon("icons\\exit24.gif")); exitMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK)); exitMenuItem.addActionListener(listener); //创建编辑菜单与菜单项 editMenu=new JMenu("编辑"); editMenu.setFont(new Font("微软雅黑",0,15)); cutMenuItem=new JMenuItem("剪切",new ImageIcon("icons\\cut24.gif")); cutMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); cutMenuItem.addActionListener(listener); copyMenuItem=new JMenuItem("复制",new ImageIcon("icons\\copy24.gif")); copyMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); copyMenuItem.addActionListener(listener); pasteMenuItem=new JMenuItem("粘贴",new ImageIcon("icons\\paste24.gif")); pasteMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); pasteMenuItem.addActionListener(listener); foundItem=new JMenuItem("查找"); foundItem.setFont(new Font("微软雅黑",Font.BOLD,13)); foundItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK)); foundItem.addActionListener(listener); replaceItem=new JMenuItem("替换"); replaceItem.setFont(new Font("微软雅黑",Font.BOLD,13)); replaceItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK)); replaceItem.addActionListener(listener); selectAll=new JMenuItem("全选"); selectAll.setFont(new Font("微软雅黑",Font.BOLD,13)); selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK)); selectAll.addActionListener(listener); //创建格式菜单与菜单项 formMenu=new JMenu("格式"); formMenu.setFont(new Font("微软雅黑",0,15)); wrapline=new JCheckBoxMenuItem("自动换行"); wrapline.setFont(new Font("微软雅黑",Font.BOLD,13)); wrapline.addActionListener(listener); wrapline.addChangeListener(new ChangeListener() { publicvoid stateChanged(ChangeEvent e) { if(wrapline.isSelected()){ textArea.setLineWrap(true); } else textArea.setLineWrap(false); } }); font=new JMenuItem("字体"); font.setFont(new Font("微软雅黑",Font.BOLD,13)); font.addActionListener(listener); //创建关于菜单 aboutMenu=new JMenu("关于"); aboutMenu.setFont(new Font("微软雅黑",0,15)); about=new JMenuItem("记事本……"); about.setFont(new Font("微软雅黑",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=new JMenuBar(); menuBar.add(fielMenu); menuBar.add(editMenu); menuBar.add(formMenu); menuBar.add(aboutMenu); setJMenuBar(menuBar); //创建两个框架,用作查找和替换 foundFrame=new JFrame(); replaceFrame=new JFrame(); //创建两个文本框 textField1=new JTextField(15); textField2=new JTextField(15); startButton=new utton("开始"); startButton.addActionListener(listener); replaceButton=new utton("替换为"); replaceButton.addActionListener(listener); reallButton=new utton("全部替换"); reallButton.addActionListener(listener); } //创建菜单项事件监听器 ActionListener listener=new ActionListener() { publicvoid actionPerformed(ActionEvent e) { String name=e.getActionCommand(); if(e.getSource() instanceof JMenuItem){ if("新建".equals(name)){ textArea.setText(""); file=null; } if("打开".equals(name)){ if(file!=null){ fileChooser.setSelectedFile(file); } int returnVal=fileChooser.showOpenDialog(NotePad.this); if(returnVal==JFileChooser.APPROVE_OPTION){ file=fileChooser.getSelectedFile(); } try{ FileReader reader=new FileReader(file); int len=(int)file.length(); char[] array=newchar[len]; reader.read(array,0,len); reader.close(); textArea.setText(new String(array)); } catch(Exception e_open){ e_open.printStackTrace(); } } } if("保存".equals(name)){ if(file!=null){ fileChooser.setSelectedFile(file); } int returnVal=fileChooser.showSaveDialog(NotePad.this); if(returnVal==JFileChooser.APPROVE_OPTION){ file=fileChooser.getSelectedFile(); } try{ FileWriter writer=new FileWriter(file); writer.write(textArea.getText()); writer.close(); } catch (Exception e_save) { e_save.getStackTrace(); } } if("退出".equals(name)){ System.exit(0); } if("剪切".equals(name)){ textArea.cut(); } if("复制".equals(name)){ textArea.copy(); } if("粘贴".equals(name)){ textArea.paste(); } if("查找".equals(name)){ value=textArea.getText(); foundFrame.add(textField1,BorderLayout.CENTER); foundFrame.add(startButton,BorderLayout.SOUTH); foundFrame.setLocation(300,300); foundFrame.setTitle("查找"); foundFrame.pack(); foundFrame.setVisible(true); foundFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } if("替换".equals(name)){ value=textArea.getText(); JLabel label1=new JLabel("查找容:"); JLabel label2=new JLabel("替换为:"); JPanel panel1=new JPanel(); panel1.setLayout(new GridLayout(2,2)); JPanel panel2=new JPanel(); panel2.setLayout(new GridLayout(1,3)); replaceFrame.add(panel1,BorderLayout.NORTH); replaceFrame.add(panel2,BorderLayout.CENTER); panel1.add(label1); panel1.add(textField1); panel1.add(label2); panel1.add(textField2); panel2.add(startButton); panel2.add(replaceButton); panel2.add(reallButton); replaceFrame.setTitle("替换"); replaceFrame.setLocation(300,300); replaceFrame.pack(); replaceFrame.setVisible(true); replaceFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } if("开始".equals(name)||"下一个".equals(name)){ String temp=textField1.getText(); int s=value.indexOf(temp,start); if(value.indexOf(temp,start)!=-1){ textArea.setSelectionStart(s); textArea.setSelectionEnd(s+temp.length()); textArea.setSelectedTextColor(Color.GREEN); start=s+1; startButton.setText("下一个"); }else { JOptionPane.showMessageDialog(foundFrame, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif")); foundFrame.dispose(); } } if("替换为".equals(name)){ String temp=textField1.getText(); int s=value.indexOf(temp,start); if(value.indexOf(temp,start)!=-1){ textArea.setSelectionStart(s); textArea.setSelectionEnd(s+temp.length()); textArea.setSelectedTextColor(Color.GREEN); start=s+1; textArea.replaceSelection(textField2.getText()); }else { JOptionPane.showMessageDialog(foundFrame, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif")); foundFrame.dispose(); } } if("全部替换".equals(name)){ String temp=textArea.getText(); temp=temp.replaceAll(textField1.getText(),textField2.getText()); textArea.setText(temp); } if("全选".equals(name)){ textArea.selectAll(); } if("字体".equals(name)){ FontDialog fontDialog=new FontDialog(NotePad.this); fontDialog.setVisible(true); if(textArea.getFont()!=fontDialog.getFont()){ textArea.setFont(fontDialog.getFont()); } } if("记事本……".equals(name)){ AboutDialog aboutDialog=new AboutDialog(NotePad.this); aboutDialog.setVisible(true); } } }; //创建字体设置对话面板,并添加相应事件监听器 classFontDialogextends JDialog implements ItemListener, ActionListener, WindowListener{ public JCheckBox Bold=new JCheckBox("Bold",false); public JCheckBox Italic=new JCheckBox("Italic",false); public List Size,Name; publicintFontName; publicintFontStyle; publicintFontSize; public utton OK=new utton("OK"); public utton Cancel=new utton("Cancel"); public JTextArea Text=new JTextArea("字体预览文本域\n0123456789\nAaBbCcXxYyZz"); public FontDialog(JFrame owner) { super(owner,"字体设置",true); GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment(); String name[]=g.getAvailableFontFamilyNames(); Name=new List(); Size=new List(); FontName=0; FontStyle=0; FontSize=8; int i=0; Name.add("Default Value"); for(i=0;i<name.length;i++) Name.add(name[i]); for(i=8;i<257;i++) Size.add(String.valueOf(i)); this.setLayout(null); this.setBounds(250,200,480, 306); this.setResizable(false); OK.setFocusable(false); Cancel.setFocusable(false); Bold.setFocusable(false); Italic.setFocusable(false); Name.setFocusable(false); Size.setFocusable(false); Name.setBounds(10, 10, 212, 259); this.add(Name); Bold.setBounds(314, 10, 64, 22); this.add(Bold); Italic.setBounds(388, 10, 64, 22); this.add(Italic); Size.setBounds(232, 10, 64, 259); this.add(Size); Text.setBounds(306, 40, 157, 157); this.add(Text); OK.setBounds(306, 243, 74, 26); this.add(OK); Cancel.setBounds(390, 243, 74, 26); this.add(Cancel); Name.select(FontName); Size.select(FontSize); Text.setFont(getFont()); Name.addItemListener(this); Size.addItemListener(this); Bold.addItemListener(this); Italic.addItemListener(this); OK.addActionListener(this); Cancel.addActionListener(this); this.addWindowListener(this); } publicvoid itemStateChanged(ItemEvent e) { Text.setFont(getFont()); } publicvoid actionPerformed(ActionEvent e) { if(e.getSource()==OK){ FontName=Name.getSelectedIndex(); FontStyle=getStyle(); FontSize=Size.getSelectedIndex(); this.setVisible(false); } else cancel(); } publicvoid windowClosing(WindowEvent e) { cancel(); } public Font getFont(){ if(Name.getSelectedIndex()==0) returnnew Font("新宋体",getStyle(),Size.getSelectedIndex()+8); elsereturnnew Font(Name.getSelectedItem(),getStyle(),Size.getSelectedIndex()+8); } publicvoid cancel(){ Name.select(FontName); Size.select(FontSize); setStyle(); Text.setFont(getFont()); this.setVisible(false); } publicvoid setStyle(){ if(FontStyle==0 || FontStyle==2) Bold.setSelected(false); elseBold.setSelected(true); if(FontStyle==0 || FontStyle==1) Italic.setSelected(false); elseItalic.setSelected(true); } publicint getStyle(){ int bold=0,italic=0; if(Bold.isSelected()) bold=1; if(Italic.isSelected()) italic=1; return bold+italic*2; } publicvoid windowActivated(WindowEvent arg0) {} publicvoid windowClosed(WindowEvent arg0) {} publicvoid windowDeactivated(WindowEvent arg0) {} publicvoid windowDeiconified(WindowEvent arg0) {} publicvoid windowIconified(WindowEvent arg0) {} publicvoid windowOpened(WindowEvent arg0) {} } //创建关于对话框 classAboutDialogextends JDialog implements ActionListener{ public utton OK,Icon; public JLabel Name,Version,Author,Java; public JPanel Panel; AboutDialog(JFrame owner) { super(owner,"关于",true); OK=new utton("OK"); Icon=new utton(new ImageIcon("icons\\edit.gif")); Name=new JLabel("Notepad"); Version=new JLabel("Version 1.0"); Java=new JLabel("JRE Version 6.0"); Author=new JLabel("Copyright (c) 11-5-2012 By Jianmin Chen"); Panel=new JPanel(); Color c=new Color(0,95,191); Name.setForeground(c); Version.setForeground(c); Java.setForeground(c); Author.setForeground(c); Panel.setBackground(Color.white); OK.setFocusable(false); this.setBounds(250,200,280, 180); this.setResizable(false); this.setLayout(null); Panel.setLayout(null); OK.addActionListener(this); Icon.setFocusable(false); Icon.setBorderPainted(false); Author.setFont(new Font(null,Font.PLAIN,11)); Panel.add(Icon); Panel.add(Name); Panel.add(Version); Panel.add(Author); Panel.add(Java); this.add(Panel); this.add(OK); Panel.setBounds(0, 0, 280, 100); OK.setBounds(180, 114, 72, 26); Name.setBounds(80, 10, 160, 20); Version.setBounds(80, 27, 160, 20); Author.setBounds(15, 70, 250, 20); Java.setBounds(80, 44, 160, 20); Icon.setBounds(16, 14, 48, 48); } publicvoid actionPerformed(ActionEvent e) { this.setVisible(false); } } } //创建记事本 publicclass ChenJianmin { publicstaticvoid main(String[] args){ EventQueue.invokeLater(new Runnable() { publicvoid run() { NotePad notePad=new NotePad(); notePad.setTitle("记事本"); notePad.setVisible(true); notePad.setBounds(100,100,800,600); notePad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }); } } 12 / 12
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服