Chinaunix首页 | 论坛 | 博客
  • 博客访问: 519896
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1172
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-21 13:40
个人简介

技术改变命运

文章分类

全部博文(184)

文章存档

2020年(16)

2017年(12)

2016年(156)

我的朋友

分类: Java

2016-12-15 17:45:32

题目描述:
编写仿windows记事本的小程序

点击(此处)折叠或打开

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;

  4. import javax.swing.*;
  5. import javax.swing.event.ChangeEvent;
  6. import javax.swing.event.ChangeListener;
  7. class notePad extends JFrame
  8. {
  9.     //private ActionListener listener = null;
  10.     //private static final Object BoderLayout = null;
  11.     private JMenuBar menuBar;
  12.     private JMenu fileMenu,editMenu,formatMenu,aboutMenu;
  13.     private JMenuItem openMenuItem,newMenuItem,saveMenuItem,exitMenuItem;
  14.     private JMenuItem cutMenu,copyMenu,findMenu,replaceMenu,selectAllMenu,pasteMenu;
  15.     private JTextArea textArea;
  16.     private JMenuItem fontMenuItem,aboutMenuItem;
  17.     private JTextField textField1 = new JTextField(15);
  18.     private JTextField textField2 = new JTextField(15);
  19.     private JButton startButton,replaceButton,replaceAllButton;
  20.     private JFrame findJframe,replaceJframe;
  21.     int start=0;
  22.     String value;
  23.     File file=null;
  24.     JFileChooser fileChooser=new JFileChooser();
  25.     JCheckBoxMenuItem checkMenu;
  26.     
  27.     boolean wrap=false;
  28.     public notePad()
  29.     {
  30.         textArea = new JTextArea();
  31.         add(new JScrollPane(textArea),BorderLayout.CENTER);
  32.         fileMenu = new JMenu("文件");
  33.         fileMenu.setFont(new Font("微软雅黑",0,15));
  34.         newMenuItem = new JMenuItem("新建");
  35.         newMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  36.         newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
  37.         newMenuItem.addActionListener(listener);
  38.             
  39.         openMenuItem=new JMenuItem("打开",new ImageIcon("icons\\open24.gif"));
  40.         openMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  41.         openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
  42.         openMenuItem.addActionListener(listener);
  43.         
  44.         saveMenuItem=new JMenuItem("保存",new ImageIcon("icons\\save.gif"));
  45.         saveMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  46.         saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
  47.         saveMenuItem.addActionListener(listener);
  48.         
  49.         exitMenuItem=new JMenuItem("退出",new ImageIcon("icons\\exit24.gif"));
  50.         exitMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  51.         exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
  52.         exitMenuItem.addActionListener(listener);
  53.         
  54.         editMenu = new JMenu("编辑");
  55.         editMenu.setFont(new Font("微软雅黑",0,15));
  56.         cutMenu = new JMenuItem("剪切");
  57.         cutMenu.setFont(new Font("微软雅黑",Font.BOLD,13));
  58.         cutMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
  59.         cutMenu.addActionListener(listener);
  60.         
  61.         copyMenu = new JMenuItem("复制");
  62.         copyMenu.setFont(new Font("微软雅黑",Font.BOLD,13));
  63.         copyMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
  64.         copyMenu.addActionListener(listener);
  65.         
  66.         //editMenu = new JMenu("编辑");
  67.         //editMenu.setFont(new Font("微软雅黑",0,15));
  68.         pasteMenu = new JMenuItem("粘贴");
  69.         pasteMenu.setFont(new Font("微软雅黑",Font.BOLD,13));
  70.         pasteMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
  71.         pasteMenu.addActionListener(listener);
  72.         
  73.         //editMenu = new JMenu("编辑");
  74.     //    editMenu.setFont(new Font("微软雅黑",0,15));
  75.         findMenu = new JMenuItem("查找");
  76.         findMenu.setFont(new Font("微软雅黑",Font.BOLD,13));
  77.         findMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));
  78.         findMenu.addActionListener(listener);
  79.         
  80.         //editMenu = new JMenu("编辑");
  81.         //editMenu.setFont(new Font("微软雅黑",0,15));
  82.         replaceMenu = new JMenuItem("替换");
  83.         replaceMenu.setFont(new Font("微软雅黑",Font.BOLD,13));
  84.         replaceMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK));
  85.         replaceMenu.addActionListener(listener);
  86.         
  87.         //editMenu = new JMenu("编辑");
  88.         //editMenu.setFont(new Font("微软雅黑",0,15));
  89.         selectAllMenu = new JMenuItem("全选");
  90.         selectAllMenu.setFont(new Font("微软雅黑",Font.BOLD,13));
  91.         selectAllMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
  92.         selectAllMenu.addActionListener(listener);
  93.         
  94.         formatMenu = new JMenu("格式");
  95.         formatMenu.setFont(new Font("微软雅黑",0,15));
  96.         
  97.         checkMenu = new JCheckBoxMenuItem("换行");
  98.         checkMenu.setFont(new Font("微软雅黑",Font.BOLD,13));
  99.         checkMenu.addChangeListener(new ChangeListener()
  100.         {
  101.             public void stateChanged(ChangeEvent e)
  102.             {
  103.                 if (checkMenu.isSelected())
  104.                 {
  105.                     
  106.                     textArea.setLineWrap(true);
  107.                 }
  108.                 else
  109.                 {
  110.                     
  111.                     textArea.setLineWrap(false);
  112.                 }
  113.             }
  114.         });
  115.         
  116.         
  117.         
  118.         fontMenuItem = new JMenuItem("字体");
  119.         fontMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  120.         fontMenuItem.addActionListener(listener);
  121.         
  122.         
  123.         fileMenu.add(newMenuItem);
  124.         fileMenu.add(openMenuItem);
  125.         fileMenu.add(saveMenuItem);
  126.         fileMenu.add(exitMenuItem);
  127.         editMenu.add(selectAllMenu);
  128.         editMenu.add(replaceMenu);
  129.         editMenu.add(findMenu);
  130.         editMenu.add(cutMenu);
  131.         editMenu.add(pasteMenu);
  132.         editMenu.add(copyMenu);
  133.         formatMenu.add(checkMenu);
  134.         formatMenu.add(fontMenuItem);
  135.         
  136.         
  137.         menuBar=new JMenuBar();
  138.         menuBar.add(fileMenu);
  139.         menuBar.add(editMenu);
  140.         menuBar.add(formatMenu);
  141.         setJMenuBar(menuBar);
  142.         findJframe = new JFrame("查找框");
  143.         startButton=new JButton("开始");
  144.         startButton.addActionListener(listener);
  145.         replaceJframe = new JFrame("替换框");
  146.         textField1=new JTextField(15);
  147.         textField2=new JTextField(15);
  148.         replaceButton = new JButton("替换为");
  149.         replaceButton.addActionListener(listener);
  150.         
  151.         replaceAllButton = new JButton("全部替换");
  152.         replaceAllButton.addActionListener(listener);
  153.     //    this.setVisible(true);
  154.         //this.setBounds(10, 10, 400, 300);
  155.     }
  156.         ActionListener listener=new ActionListener() {
  157.             public void actionPerformed(ActionEvent e) {
  158.                 String name=e.getActionCommand();
  159.                 if(e.getSource() instanceof JMenuItem){
  160.                     if("新建".equals(name)){
  161.                         textArea.setText("");
  162.                         file=null;
  163.                     }
  164.                     if("打开".equals(name)){
  165.                         if(file!=null){
  166.                             fileChooser.setSelectedFile(file);
  167.                         }
  168.                         int returnVal=fileChooser.showOpenDialog(notePad.this);
  169.                         if(returnVal==JFileChooser.APPROVE_OPTION){
  170.                             file=fileChooser.getSelectedFile();
  171.                             }
  172.                             try{
  173.                                 FileReader reader=new FileReader(file);
  174.                                 int len=(int)file.length();
  175.                                 char[] array=new char[len];
  176.                                 reader.read(array,0,len);
  177.                                 reader.close();
  178.                                 textArea.setText(new String(array));
  179.                             }
  180.                             catch(Exception e_open){
  181.                                 e_open.printStackTrace();
  182.                             }
  183.                         }
  184.                     }
  185.                     if("保存".equals(name)){
  186.                         if(file!=null){
  187.                             fileChooser.setSelectedFile(file);
  188.                         }
  189.                         int returnVal=fileChooser.showSaveDialog(notePad.this);
  190.                         if(returnVal==JFileChooser.APPROVE_OPTION){
  191.                             file=fileChooser.getSelectedFile();
  192.                         }
  193.                         try{
  194.                             FileWriter writer=new FileWriter(file);
  195.                             writer.write(textArea.getText());
  196.                             writer.close();
  197.                         }
  198.                         catch (Exception e_save) {
  199.                             e_save.getStackTrace();
  200.                         }
  201.                     }
  202.                     if("退出".equals(name)){
  203.                         System.exit(0);
  204.                     }
  205.                     if ("剪切".equals(name))
  206.                     {
  207.                         textArea.cut();
  208.                     }
  209.                     if ("复制".equals(name))
  210.                     {
  211.                         textArea.copy();
  212.                     }
  213.                     if ("粘贴".equals(name))
  214.                     {
  215.                         textArea.paste();
  216.                     }
  217.                     if ("全选".equals(name)){
  218.                         //System.out.println("全选");
  219.                         
  220.                         textArea.selectAll();
  221.                     }
  222.                     if ("查找".equals(name))
  223.                     {
  224.                         value = textArea.getText();
  225.                         findJframe.add(textField1,BorderLayout.CENTER);
  226.                         findJframe.add(startButton,BorderLayout.SOUTH);
  227.                         findJframe.setLocation(300, 300);
  228.                         findJframe.pack();
  229.                         findJframe.setVisible(true);
  230.                         findJframe.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  231. ;                        
  232.                     }
  233.                     if ("替换".equals(name))
  234.                     {
  235.                         value = textArea.getText();
  236.                         JLabel label1 = new JLabel("查找内容");
  237.                         JLabel label2 = new JLabel("替换为");
  238.                         JPanel panel1 = new JPanel();
  239.                         panel1.setLayout(new GridLayout(2,2));
  240.                         JPanel panel2 = new JPanel();
  241.                         panel2.setLayout(new GridLayout(1,3));
  242.                         replaceJframe.add(panel1,BorderLayout.NORTH);
  243.                         replaceJframe.add(panel2,BorderLayout.CENTER);
  244.                         panel1.add(label1);
  245.                         panel1.add(textField1);
  246.                         panel1.add(label2);
  247.                         panel1.add(textField2);
  248.                         panel2.add(startButton);
  249.                         panel2.add(replaceButton);
  250.                         panel2.add(replaceAllButton);
  251.                         replaceJframe.setLocation(300,300);
  252.                         replaceJframe.pack();
  253.                         replaceJframe.setVisible(true);
  254.                         replaceJframe.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  255.                     }
  256.                     if("开始".equals(name)||"下一个".equals(name)){
  257.                         String temp=textField1.getText();
  258.                          int s=value.indexOf(temp,start);
  259.                          if(s!=-1){
  260.                             textArea.setSelectionStart(s);
  261.                             textArea.setSelectionEnd(s+temp.length());
  262.                             textArea.setSelectedTextColor(Color.GREEN);
  263.                             start=s+1;
  264.                             startButton.setText("下一个");
  265.                         }else {
  266.                             JOptionPane.showMessageDialog(findJframe, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif"));
  267.                          findJframe.dispose();
  268.                         }    
  269.                     }
  270.                     if ("替换为".equals(name))
  271.                     {
  272.                         String temp = textField1.getText();
  273.                         int s = value.indexOf(temp,start);
  274.                         if (s != -1)
  275.                         {
  276.                             textArea.setSelectionStart(s);
  277.                             textArea.setSelectionEnd(s+temp.length());
  278.                             textArea.setSelectedTextColor(Color.blue);
  279.                             start = s+1;
  280.                             textArea.replaceSelection(textField2.getText());
  281.                         }
  282.                         else
  283.                         {
  284.                             JOptionPane.showMessageDialog(findJframe, "查找完毕","提示",0,new ImageIcon(""));
  285.                             findJframe.dispose();
  286.                             
  287.                         }
  288.                     }
  289.                     if ("全部替换".equals(name))
  290.                     {
  291.                         String temp = textArea.getText();
  292.                         temp.replaceAll(textField1.getText(), textField2.getText());
  293.                         textArea.setText(temp);    
  294.                     }
  295.                     if ("字体 ".equals(name))
  296.                     {
  297.                         FontDialog fontDialog = new FontDialog(notePad.this);
  298.                         fontDialog.setVisible(true);
  299.                         if (textArea.getFont() != fontDialog.getFont())
  300.                         {
  301.                             textArea.setFont(fontDialog.getFont());
  302.                         }
  303.                     }
  304.             }
  305.         
  306.         
  307.     };    
  308.     class FontDialog extends JDialog implements ItemListener, ActionListener, WindowListener
  309.     {
  310.         public JCheckBox Bold=new JCheckBox("Bold",false);
  311.         public JCheckBox Italic=new JCheckBox("Italic",false);
  312.         public List Size,Name;
  313.         public int FontName;
  314.         public int FontStyle;
  315.         public int FontSize;
  316.         public JButton OK=new JButton("OK");
  317.         public JButton Cancel=new JButton("Cancel");
  318.         public JTextArea Text=new JTextArea("字体预览文本域\n0123456789\nAaBbCcXxYyZz");
  319.         public FontDialog(JFrame owner) {
  320.             super(owner,"字体设置",true);
  321.             GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment();
  322.             String name[]=g.getAvailableFontFamilyNames();
  323.             Name=new List();
  324.             Size=new List();
  325.             FontName=0;
  326.             FontStyle=0;
  327.             FontSize=8;
  328.             int i=0;
  329.             Name.add("Default Value");
  330.             
  331.             for(i=0;i<name.length;i++)
  332.                 Name.add(name[i]);
  333.             for(i=8;i<257;i++)
  334.                 Size.add(String.valueOf(i));
  335.             
  336.             this.setLayout(null);
  337.             this.setBounds(250,200,480, 306);
  338.             this.setResizable(false);
  339.             
  340.             OK.setFocusable(false);
  341.             Cancel.setFocusable(false);
  342.             Bold.setFocusable(false);
  343.             Italic.setFocusable(false);
  344.             Name.setFocusable(false);
  345.             Size.setFocusable(false);
  346.         //    Calender
  347.             Name.setBounds(10, 10, 212, 259);
  348.             this.add(Name);
  349.             Bold.setBounds(314, 10, 64, 22);
  350.             this.add(Bold);
  351.             Italic.setBounds(388, 10, 64, 22);
  352.             this.add(Italic);
  353.             Size.setBounds(232, 10, 64, 259);
  354.             this.add(Size);
  355.             Text.setBounds(306, 40, 157, 157);
  356.             this.add(Text);
  357.             OK.setBounds(306, 243, 74, 26);
  358.             this.add(OK);
  359.             Cancel.setBounds(390, 243, 74, 26);
  360.             this.add(Cancel);
  361.             
  362.             Name.select(FontName);
  363.             Size.select(FontSize);
  364.             Text.setFont(getFont());
  365.             
  366.             Name.addItemListener(this);
  367.             Size.addItemListener(this);
  368.             Bold.addItemListener(this);
  369.             Italic.addItemListener(this);
  370.             OK.addActionListener(this);
  371.             Cancel.addActionListener(this);
  372.             this.addWindowListener(this);
  373.         }
  374.         public void itemStateChanged(ItemEvent e) {
  375.             Text.setFont(getFont());
  376.         }
  377.         public void actionPerformed(ActionEvent e) {
  378.             if(e.getSource()==OK){
  379.                 FontName=Name.getSelectedIndex();
  380.                 FontStyle=getStyle();
  381.                 FontSize=Size.getSelectedIndex();
  382.                 this.setVisible(false);
  383.             }
  384.             else cancel();
  385.         }
  386.         public void windowClosing(WindowEvent e) {
  387.             cancel();
  388.         }
  389.         public Font getFont(){
  390.             if(Name.getSelectedIndex()==0) return new Font("新宋体",getStyle(),Size.getSelectedIndex()+8);
  391.             else return new Font(Name.getSelectedItem(),getStyle(),Size.getSelectedIndex()+8);
  392.         }
  393.         public void cancel(){
  394.             Name.select(FontName);
  395.             Size.select(FontSize);
  396.             setStyle();
  397.             Text.setFont(getFont());
  398.             this.setVisible(false);
  399.         }
  400.         public void setStyle(){
  401.             if(FontStyle==0 || FontStyle==2) Bold.setSelected(false);
  402.             else Bold.setSelected(true);
  403.             if(FontStyle==0 || FontStyle==1) Italic.setSelected(false);
  404.             else Italic.setSelected(true);
  405.         }
  406.         public int getStyle(){
  407.             int bold=0,italic=0;
  408.             if(Bold.isSelected()) bold=1;
  409.             if(Italic.isSelected()) italic=1;
  410.             return bold+italic*2;
  411.         }
  412.         public void windowActivated(WindowEvent arg0) {}
  413.         public void windowClosed(WindowEvent arg0) {}
  414.         public void windowDeactivated(WindowEvent arg0) {}
  415.         public void windowDeiconified(WindowEvent arg0) {}
  416.         public void windowIconified(WindowEvent arg0) {}
  417.         public void windowOpened(WindowEvent arg0) {}
  418.     }
  419. }
  420. public class tengyanbo {
  421.     public static void main(String[] args)
  422.     {
  423.         EventQueue.invokeLater(new Runnable(){
  424.             public void run()
  425.             {
  426.                 notePad np = new notePad();
  427.                 np.setTitle("滕艳波的记事本");
  428.                 np.setVisible(true);
  429.                 np.setBounds(100,100,800,600);
  430.                 np.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  431.                 
  432.             }
  433.             
  434.         });
  435.     }
  436. }
参考程序:

点击(此处)折叠或打开

  1. /**
  2.  * 作品:记事本
  3.  * 作者:***
  4.  * 功能:简单的文字编辑
  5.  */
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import java.io.*;
  9. import javax.swing.*;
  10. import javax.swing.event.ChangeEvent;
  11. import javax.swing.event.ChangeListener;

  12. class NotePad extends JFrame{
  13.     
  14.     private JMenuBar menuBar;
  15.     private JMenu fielMenu,editMenu,formMenu,aboutMenu;
  16.     private JMenuItem newMenuItem,openMenuItem,saveMenuItem,exitMenuItem;
  17.     private JMenuItem cutMenuItem,copyMenuItem,pasteMenuItem,foundItem,replaceItem,selectAll;
  18.     private JMenuItem font,about;
  19.     private JTextArea textArea;
  20.     private JFrame foundFrame,replaceFrame;
  21.     private JCheckBoxMenuItem wrapline;
  22.     private JTextField textField1=new JTextField(15);//JTextField(int columns) 构造一个具有指定列数的新的空 TextField。
  23.     private JTextField textField2=new JTextField(15);
  24.     private JButton startButton,replaceButton,reallButton;
  25.     
  26.     int start=0;
  27.     String value;
  28.     File file=null;
  29.     JFileChooser fileChooser=new JFileChooser();
  30.     boolean wrap=false;
  31.     
  32.     public NotePad(){
  33.         //创建文本域
  34.         textArea=new JTextArea();//JTextArea() 构造新的 TextArea。
  35.         add(new JScrollPane(textArea),BorderLayout.CENTER);
  36.         //创建文件菜单及文件菜单项
  37.         fielMenu=new JMenu("文件");
  38.         fielMenu.setFont(new Font("微软雅黑",0,15));
  39.         
  40.         newMenuItem=new JMenuItem("新建",new ImageIcon(""));
  41.         newMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  42.         newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
  43.         newMenuItem.addActionListener(listener);
  44.             
  45.         openMenuItem=new JMenuItem("打开",new ImageIcon("icons\\open24.gif"));
  46.         openMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  47.         openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
  48.         openMenuItem.addActionListener(listener);
  49.         
  50.         saveMenuItem=new JMenuItem("保存",new ImageIcon("icons\\save.gif"));
  51.         saveMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  52.         saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
  53.         saveMenuItem.addActionListener(listener);
  54.         
  55.         exitMenuItem=new JMenuItem("退出",new ImageIcon("icons\\exit24.gif"));
  56.         exitMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  57.         exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
  58.         exitMenuItem.addActionListener(listener);
  59.         //创建编辑菜单及菜单项
  60.         editMenu=new JMenu("编辑");
  61.         editMenu.setFont(new Font("微软雅黑",0,15));
  62.         
  63.         cutMenuItem=new JMenuItem("剪切",new ImageIcon("icons\\cut24.gif"));
  64.         cutMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  65.         cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
  66.         cutMenuItem.addActionListener(listener);
  67.         
  68.         copyMenuItem=new JMenuItem("复制",new ImageIcon("icons\\copy24.gif"));
  69.         copyMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  70.         copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
  71.         copyMenuItem.addActionListener(listener);
  72.         
  73.         pasteMenuItem=new JMenuItem("粘贴",new ImageIcon("icons\\paste24.gif"));
  74.         pasteMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  75.         pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
  76.         pasteMenuItem.addActionListener(listener);
  77.         
  78.         foundItem=new JMenuItem("查找");
  79.         foundItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  80.         foundItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));
  81.         foundItem.addActionListener(listener);
  82.         
  83.         replaceItem=new JMenuItem("替换");
  84.         replaceItem.setFont(new Font("微软雅黑",Font.BOLD,13));
  85.         replaceItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK));
  86.         replaceItem.addActionListener(listener);
  87.         
  88.         selectAll=new JMenuItem("全选");
  89.         selectAll.setFont(new Font("微软雅黑",Font.BOLD,13));
  90.         selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
  91.         selectAll.addActionListener(listener);
  92.         //创建格式菜单及菜单项
  93.         formMenu=new JMenu("格式");
  94.         formMenu.setFont(new Font("微软雅黑",0,15));
  95.         
  96.         wrapline=new JCheckBoxMenuItem("自动换行");
  97.         wrapline.setFont(new Font("微软雅黑",Font.BOLD,13));
  98.         //wrapline.addActionListener(listener);
  99.         wrapline.addChangeListener(new ChangeListener() {
  100.             public void stateChanged(ChangeEvent e) {
  101.                 if(wrapline.isSelected()){
  102.                     textArea.setLineWrap(true);
  103.                 }
  104.                 else
  105.                     textArea.setLineWrap(false);
  106.             }
  107.         });
  108.         
  109.         font=new JMenuItem("字体");
  110.         font.setFont(new Font("微软雅黑",Font.BOLD,13));
  111.         font.addActionListener(listener);
  112.         //创建关于菜单
  113.         aboutMenu=new JMenu("关于");
  114.         aboutMenu.setFont(new Font("微软雅黑",0,15));
  115.         
  116.         about=new JMenuItem("记事本……");
  117.         about.setFont(new Font("微软雅黑",Font.BOLD,13));
  118.         about.addActionListener(listener);
  119.         //添加文件菜单项
  120.         fielMenu.add(newMenuItem);
  121.         fielMenu.add(openMenuItem);
  122.         fielMenu.add(saveMenuItem);
  123.         fielMenu.addSeparator();
  124.         fielMenu.add(exitMenuItem);
  125.         //添加编辑菜单项
  126.         editMenu.add(cutMenuItem);
  127.         editMenu.add(copyMenuItem);
  128.         editMenu.add(pasteMenuItem);
  129.         editMenu.add(foundItem);
  130.         editMenu.add(replaceItem);
  131.         editMenu.addSeparator();
  132.         editMenu.add(selectAll);
  133.         //添加格式菜单项
  134.         formMenu.add(wrapline);
  135.         formMenu.add(font);
  136.         //添加关于菜单项
  137.         aboutMenu.add(about);
  138.         //添加菜单
  139.         menuBar=new JMenuBar();
  140.         menuBar.add(fielMenu);
  141.         menuBar.add(editMenu);
  142.         menuBar.add(formMenu);
  143.         menuBar.add(aboutMenu);
  144.         setJMenuBar(menuBar);
  145.         //创建两个框架,用作查找和替换
  146.         foundFrame=new JFrame();
  147.         replaceFrame=new JFrame();
  148.         //创建两个文本框
  149.         textField1=new JTextField(15);
  150.         textField2=new JTextField(15);
  151.         
  152.         startButton=new JButton("开始");
  153.         startButton.addActionListener(listener);
  154.         replaceButton=new JButton("替换为");
  155.         replaceButton.addActionListener(listener);
  156.         reallButton=new JButton("全部替换");
  157.         reallButton.addActionListener(listener);    
  158.     }
  159.     //创建菜单项事件监听器
  160.     ActionListener listener=new ActionListener() {
  161.         public void actionPerformed(ActionEvent e) {
  162.             String name=e.getActionCommand();//返回引发某个事件的命令按钮的名字,如果名字为空,那么返回标签值。
  163.             if(e.getSource() instanceof JMenuItem){
  164.                 if("新建".equals(name)){
  165.                     textArea.setText("");
  166.                     file=null;
  167.                 }
  168.                 if("打开".equals(name)){
  169.                     if(file!=null){
  170.                         fileChooser.setSelectedFile(file);
  171.                     }
  172.                     int returnVal=fileChooser.showOpenDialog(NotePad.this);
  173.                     if(returnVal==JFileChooser.APPROVE_OPTION){
  174.                         file=fileChooser.getSelectedFile();
  175.                         }
  176.                         try{
  177.                             FileReader reader=new FileReader(file);
  178.                             int len=(int)file.length();
  179.                             char[] array=new char[len];
  180.                             reader.read(array,0,len);
  181.                             reader.close();
  182.                             textArea.setText(new String(array));
  183.                         }
  184.                         catch(Exception e_open){
  185.                             e_open.printStackTrace();
  186.                         }
  187.                     }
  188.                 }
  189.                 if("保存".equals(name)){
  190.                     if(file!=null){
  191.                         fileChooser.setSelectedFile(file);
  192.                     }
  193.                     int returnVal=fileChooser.showSaveDialog(NotePad.this);
  194.                     if(returnVal==JFileChooser.APPROVE_OPTION){
  195.                         file=fileChooser.getSelectedFile();
  196.                     }
  197.                     try{
  198.                         FileWriter writer=new FileWriter(file);
  199.                         writer.write(textArea.getText());
  200.                         writer.close();
  201.                     }
  202.                     catch (Exception e_save) {
  203.                         e_save.getStackTrace();
  204.                     }
  205.                 }
  206.                 if("退出".equals(name)){
  207.                     System.exit(0);
  208.                 }
  209.                 if("剪切".equals(name)){
  210.                     textArea.cut();
  211.                 }
  212.                 if("复制".equals(name)){
  213.                     textArea.copy();
  214.                 }
  215.                 if("粘贴".equals(name)){
  216.                     textArea.paste();
  217.                 }
  218.                 if("查找".equals(name)){
  219.                     value=textArea.getText();
  220.                     foundFrame.add(textField1,BorderLayout.CENTER);
  221.                     foundFrame.add(startButton,BorderLayout.SOUTH);
  222.                     foundFrame.setLocation(300,300);
  223.                     foundFrame.setTitle("查找");
  224.                     foundFrame.pack();
  225.                     foundFrame.setVisible(true);
  226.                     foundFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  227.                 }
  228.     //            foundFrame.
  229.                 if("替换".equals(name)){
  230.                     value=textArea.getText();
  231.                     JLabel label1=new JLabel("查找内容:");
  232.                     JLabel label2=new JLabel("替换为:");
  233.                     JPanel panel1=new JPanel();
  234.                     panel1.setLayout(new GridLayout(2,2));
  235.                     JPanel panel2=new JPanel();
  236.                     panel2.setLayout(new GridLayout(1,3));
  237.                     replaceFrame.add(panel1,BorderLayout.NORTH);
  238.                     replaceFrame.add(panel2,BorderLayout.CENTER);
  239.                     panel1.add(label1);
  240.                     panel1.add(textField1);
  241.                     panel1.add(label2);
  242.                     panel1.add(textField2);
  243.                  panel2.add(startButton);
  244.                     panel2.add(replaceButton);
  245.                     panel2.add(reallButton);
  246.                     replaceFrame.setTitle("替换");
  247.                     replaceFrame.setLocation(300,300);
  248.                     replaceFrame.pack();
  249.                     replaceFrame.setVisible(true);
  250.                     replaceFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  251.                 }
  252.                 if("开始".equals(name)||"下一个".equals(name)){
  253.                      String temp=textField1.getText();
  254.                      int s=value.indexOf(temp,start);
  255.                      if(value.indexOf(temp,start)!=-1){
  256.                         textArea.setSelectionStart(s);
  257.                         textArea.setSelectionEnd(s+temp.length());
  258.                         textArea.setSelectedTextColor(Color.GREEN);
  259.                         start=s+1;
  260.                         startButton.setText("下一个");
  261.                     }else {
  262.                         JOptionPane.showMessageDialog(foundFrame, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif"));
  263.                      foundFrame.dispose();
  264.                     }
  265.                 }
  266.                 if("替换为".equals(name)){
  267.                         String temp=textField1.getText();
  268.                         int s=value.indexOf(temp,start);
  269.                         if(value.indexOf(temp,start)!=-1){
  270.                             textArea.setSelectionStart(s);
  271.                             textArea.setSelectionEnd(s+temp.length());
  272.                             textArea.setSelectedTextColor(Color.GREEN);
  273.                             start=s+1;
  274.                             textArea.replaceSelection(textField2.getText());
  275.                         }else {
  276.                             JOptionPane.showMessageDialog(foundFrame, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif"));
  277.                             foundFrame.dispose();
  278.                         }
  279.                 }
  280.                 if("全部替换".equals(name)){
  281.                     String temp=textArea.getText();
  282.                     temp=temp.replaceAll(textField1.getText(),textField2.getText());
  283.                     textArea.setText(temp);
  284.                     
  285.                 }
  286.                 if("全选".equals(name)){
  287.                     textArea.selectAll();
  288.                 }
  289.                 if("字体".equals(name)){
  290.                     FontDialog fontDialog=new FontDialog(NotePad.this);
  291.                     fontDialog.setVisible(true);
  292.                     if(textArea.getFont()!=fontDialog.getFont()){
  293.                         textArea.setFont(fontDialog.getFont());
  294.                     }
  295.                 }
  296.                 if("记事本……".equals(name)){
  297.                     AboutDialog aboutDialog=new AboutDialog(NotePad.this);
  298.                     aboutDialog.setVisible(true);
  299.                 }    
  300.          }    
  301.     };
  302.     
  303.     //创建字体设置对话面板,并添加相应事件监听器
  304.     class FontDialog extends JDialog implements ItemListener, ActionListener, WindowListener{
  305.         public JCheckBox Bold=new JCheckBox("Bold",false);
  306.         public JCheckBox Italic=new JCheckBox("Italic",false);
  307.         public List Size,Name;
  308.         public int FontName;
  309.         public int FontStyle;
  310.         public int FontSize;
  311.         public JButton OK=new JButton("OK");
  312.         public JButton Cancel=new JButton("Cancel");
  313.         public JTextArea Text=new JTextArea("字体预览文本域\n0123456789\nAaBbCcXxYyZz");
  314.         public FontDialog(JFrame owner) {
  315.             super(owner,"字体设置",true);
  316.             GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment();
  317.             String name[]=g.getAvailableFontFamilyNames();
  318.             Name=new List();
  319.             Size=new List();
  320.             FontName=0;
  321.             FontStyle=0;
  322.             FontSize=8;
  323.             int i=0;
  324.             Name.add("Default Value");
  325.             
  326.             for(i=0;i<name.length;i++)
  327.                 Name.add(name[i]);
  328.             for(i=8;i<257;i++)
  329.                 Size.add(String.valueOf(i));
  330.             
  331.             this.setLayout(null);
  332.             this.setBounds(250,200,480, 306);
  333.             this.setResizable(false);
  334.             
  335.             OK.setFocusable(false);
  336.             Cancel.setFocusable(false);
  337.             Bold.setFocusable(false);
  338.             Italic.setFocusable(false);
  339.             Name.setFocusable(false);
  340.             Size.setFocusable(false);
  341.         //    Calender
  342.             Name.setBounds(10, 10, 212, 259);
  343.             this.add(Name);
  344.             Bold.setBounds(314, 10, 64, 22);
  345.             this.add(Bold);
  346.             Italic.setBounds(388, 10, 64, 22);
  347.             this.add(Italic);
  348.             Size.setBounds(232, 10, 64, 259);
  349.             this.add(Size);
  350.             Text.setBounds(306, 40, 157, 157);
  351.             this.add(Text);
  352.             OK.setBounds(306, 243, 74, 26);
  353.             this.add(OK);
  354.             Cancel.setBounds(390, 243, 74, 26);
  355.             this.add(Cancel);
  356.             
  357.             Name.select(FontName);
  358.             Size.select(FontSize);
  359.             Text.setFont(getFont());
  360.             
  361.             Name.addItemListener(this);
  362.             Size.addItemListener(this);
  363.             Bold.addItemListener(this);
  364.             Italic.addItemListener(this);
  365.             OK.addActionListener(this);
  366.             Cancel.addActionListener(this);
  367.             this.addWindowListener(this);
  368.         }
  369.         public void itemStateChanged(ItemEvent e) {
  370.             Text.setFont(getFont());
  371.         }
  372.         public void actionPerformed(ActionEvent e) {
  373.             if(e.getSource()==OK){
  374.                 FontName=Name.getSelectedIndex();
  375.                 FontStyle=getStyle();
  376.                 FontSize=Size.getSelectedIndex();
  377.                 this.setVisible(false);
  378.             }
  379.             else cancel();
  380.         }
  381.         public void windowClosing(WindowEvent e) {
  382.             cancel();
  383.         }
  384.         public Font getFont(){
  385.             if(Name.getSelectedIndex()==0) return new Font("新宋体",getStyle(),Size.getSelectedIndex()+8);
  386.             else return new Font(Name.getSelectedItem(),getStyle(),Size.getSelectedIndex()+8);
  387.         }
  388.         public void cancel(){
  389.             Name.select(FontName);
  390.             Size.select(FontSize);
  391.             setStyle();
  392.             Text.setFont(getFont());
  393.             this.setVisible(false);
  394.         }
  395.         public void setStyle(){
  396.             if(FontStyle==0 || FontStyle==2) Bold.setSelected(false);
  397.             else Bold.setSelected(true);
  398.             if(FontStyle==0 || FontStyle==1) Italic.setSelected(false);
  399.             else Italic.setSelected(true);
  400.         }
  401.         public int getStyle(){
  402.             int bold=0,italic=0;
  403.             if(Bold.isSelected()) bold=1;
  404.             if(Italic.isSelected()) italic=1;
  405.             return bold+italic*2;
  406.         }
  407.         public void windowActivated(WindowEvent arg0) {}
  408.         public void windowClosed(WindowEvent arg0) {}
  409.         public void windowDeactivated(WindowEvent arg0) {}
  410.         public void windowDeiconified(WindowEvent arg0) {}
  411.         public void windowIconified(WindowEvent arg0) {}
  412.         public void windowOpened(WindowEvent arg0) {}
  413.     }
  414.     
  415.     //创建关于对话框
  416.     class AboutDialog extends JDialog implements ActionListener{
  417.         public JButton OK,Icon;
  418.         public JLabel Name,Version,Author,Java;
  419.         public JPanel Panel;
  420.         AboutDialog(JFrame owner) {
  421.             super(owner,"关于",true);
  422.             OK=new JButton("OK");
  423.             Icon=new JButton(new ImageIcon("icons\\edit.gif"));
  424.             Name=new JLabel("Notepad");
  425.             Version=new JLabel("Version 1.0");
  426.             Java=new JLabel("JRE Version 6.0");
  427.             Author=new JLabel("Copyright (c) 11-5-2012 By Jianmin Chen");
  428.             Panel=new JPanel();
  429.             Color c=new Color(0,95,191);
  430.             Name.setForeground(c);
  431.             Version.setForeground(c);
  432.             Java.setForeground(c);
  433.             Author.setForeground(c);
  434.             Panel.setBackground(Color.white);
  435.             OK.setFocusable(false);
  436.             this.setBounds(250,200,280, 180);
  437.         
  438.             this.setResizable(false);
  439.             this.setLayout(null);
  440.             Panel.setLayout(null);
  441.             OK.addActionListener(this);
  442.             Icon.setFocusable(false);
  443.             Icon.setBorderPainted(false);
  444.             Author.setFont(new Font(null,Font.PLAIN,11));
  445.             Panel.add(Icon);
  446.             Panel.add(Name);
  447.             Panel.add(Version);
  448.             Panel.add(Author);
  449.             Panel.add(Java);
  450.             this.add(Panel);
  451.             this.add(OK);
  452.             Panel.setBounds(0, 0, 280, 100);
  453.             OK.setBounds(180, 114, 72, 26);
  454.             Name.setBounds(80, 10, 160, 20);
  455.             Version.setBounds(80, 27, 160, 20);
  456.             Author.setBounds(15, 70, 250, 20);
  457.             Java.setBounds(80, 44, 160, 20);
  458.             Icon.setBounds(16, 14, 48, 48);
  459.         }
  460.         public void actionPerformed(ActionEvent e) {
  461.             this.setVisible(false);
  462.         }
  463.     }
  464. }

  465. //创建记事本
  466. public class ChenJianmin {
  467.     public static void main(String[] args){
  468.         EventQueue.invokeLater(new Runnable() {
  469.             public void run() {
  470.                 NotePad notePad=new NotePad();
  471.                 notePad.setTitle("记事本");
  472.                 notePad.setVisible(true);
  473.                 notePad.setBounds(100,100,800,600);
  474.                 notePad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  475.             }
  476.         });
  477.     }
  478. }

阅读(1068) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~