Java记事本源代码(完整).doc_第1页
Java记事本源代码(完整).doc_第2页
Java记事本源代码(完整).doc_第3页
Java记事本源代码(完整).doc_第4页
Java记事本源代码(完整).doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

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

文档简介

/* * 作品:记事本 * 作者:* * 功能:简单的文字编辑 */import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;class NotePad extends JFrameprivate 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 JButton startButton,replaceButton,reallButton;int start=0;String value;File file=null;JFileChooser fileChooser=new JFileChooser();boolean wrap=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(iconsnew24.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(iconsopen24.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(iconssave.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(iconsexit24.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(iconscut24.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(iconscopy24.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(iconspaste24.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() public void stateChanged(ChangeEvent e) if(wrapline.isSelected()textArea.setLineWrap(true);elsetextArea.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 JButton(开始);startButton.addActionListener(listener);replaceButton=new JButton(替换为);replaceButton.addActionListener(listener);reallButton=new JButton(全部替换);reallButton.addActionListener(listener);/创建菜单项事件监听器ActionListener listener=new ActionListener() public void 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();tryFileReader reader=new FileReader(file);int len=(int)file.length();char array=new charlen;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();tryFileWriter 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(iconssearch.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(iconssearch.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); ;/创建字体设置对话面板,并添加相应事件监听器class FontDialog extends JDialog implements ItemListener, ActionListener, WindowListenerpublic JCheckBox Bold=new JCheckBox(Bold,false);public JCheckBox Italic=new JCheckBox(Italic,false);public List Size,Name;public int FontName;public int FontStyle;public int FontSize;public JButton OK=new JButton(OK);public JButton Cancel=new JButton(Cancel);public JTextArea Text=new JTextArea(字体预览文本域n0123456789nAaBbCcXxYyZz);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;iname.length;i+)Name.add(namei);for(i=8;i257;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);public void itemStateChanged(ItemEvent e) Text.setFont(getFont();public void actionPerformed(ActionEvent e) if(e.getSource()=OK)FontName=Name.getSelectedIndex();FontStyle=getStyle();FontSize=Size.getSelectedIndex();this.setVisible(false);else cancel();public void windowClosing(WindowEvent e) cancel();public Font getFont()if(Name.getSelectedIndex()=0) return new Font(新宋体,getStyle(),Size.getSelectedIndex()+8);else return new Font(Name.getSelectedItem(),getStyle(),Size.getSelectedIndex()+8);public void cancel()Name.select(FontName);Size.select(FontSize);setStyle();Text.setFont(getFont();this.setVisible(false);public void setStyle()if(FontStyle=0 | FontStyle=2) Bold.setSelected(false);else Bold.setSelected(true);if(FontStyle=0 | FontStyle=1) Italic.setSelected(false);else Italic.setSelected(true);public int getStyle()int bold=0,italic=0;if(Bold.isSelected() bold=1;if(Italic.isSelected() italic=1;return bold+italic*2;public void windowActivated(WindowEvent arg0) public void windowClosed(WindowEvent arg0) public void windowDeactivated(WindowEvent arg0) public void windowDeiconified(WindowEvent arg0) public void windowIconified(WindowEvent arg0) public void windowOpened(WindowEvent arg0) /创建关于对话框class AboutDialog extends JDialog implements ActionListenerpublic JButton OK,Icon;public JLabel Name,Version,Author,Java;public JPanel Panel;AboutDialog(JFrame owner) super(owner,关于,true); OK=new JButton(OK);Icon=new JButton(new ImageIcon(iconsedit.gif);Name=new JLabel(No

温馨提示

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

评论

0/150

提交评论