小型记事本

来源:互联网 发布:游戏服务端源码 编辑:程序博客网 时间:2024/06/10 05:53

import java.awt.CheckboxMenuItem;

import java.awt.Color;

import java.awt.Container;

import java.awt.FileDialog;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.GraphicsEnvironment;

import java.awt.Menu;

import java.awt.MenuBar;

import java.awt.MenuItem;

import java.awt.MenuShortcut;

import java.awt.TextArea;

import java.awt.Toolkit;

import java.awt.Window;

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.DataFlavor;

importjava.awt.datatransfer.StringSelection;

import java.awt.datatransfer.Transferable;

importjava.awt.datatransfer.UnsupportedFlavorException;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.logging.Level;

import java.util.logging.Logger;

 

import javax.swing.JButton;

import javax.swing.JColorChooser;

import javax.swing.JComboBox;

import javax.swing.JDialog;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

 

 

 

 

public class notebook {

   //记事本的具体实现类

  private static final long serialVersionUID = 1L;

  private  TextArea content;

  private  String filePath ="";//先让路径为空

  Color color=Color.red;

  Toolkit toolKit = Toolkit.getDefaultToolkit();

  Clipboard clipboard = toolKit.getSystemClipboard();

  public notebook(){

          //创建一个JFrame对象;并设置相关属性

          final JFrame jf = new JFrame("我的记事本");

         jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

          jf.setBounds(100,100,500,500);

          jf.setResizable(true);

          jf.setVisible(true);

          //创建菜单栏

          MenuBar menu = new MenuBar();

          jf.setMenuBar(menu);

          //创建并添加文本框

          content = newTextArea("",50,50,TextArea.SCROLLBARS_VERTICAL_ONLY);

          jf.add(content);

          content.setVisible(true);  

          content.requestFocusInWindow();

          //菜单栏添加内容

          Menufilemenu = new Menu("文件(F)");

          Menu editmenu = new Menu("编辑(E)");

          Menu formatmenu = new Menu("格式(O)");

          Menu viewmenu = new Menu("查看(V)");

          Menu helpmenu = new Menu("帮助(H)");

          menu.add(filemenu);

          menu.add(editmenu);

          menu.add(formatmenu);

          menu.add(viewmenu);

          menu.add(helpmenu);

          //创建文件菜单上的各个菜单项并添加到菜单上

          MenuItem newitem = new MenuItem("新建(N)");

          newitem.setShortcut(newMenuShortcut(KeyEvent.VK_N,false));

          filemenu.add(newitem);

          MenuItem openitem = new MenuItem("打开(O)");

          openitem.setShortcut(newMenuShortcut(KeyEvent.VK_O,false));

          filemenu.add(openitem);

          MenuItem saveitem = new MenuItem("保存(S)");

          saveitem.setShortcut(newMenuShortcut(KeyEvent.VK_S,false));

          filemenu.add(saveitem);

          MenuItem saveasitem = new MenuItem("另存为(A)");

          saveasitem.setShortcut(newMenuShortcut(KeyEvent.VK_A,false));

          filemenu.add(saveasitem);

          MenuItem setitem = new MenuItem("页面设置(U)");

          setitem.setShortcut(newMenuShortcut(KeyEvent.VK_U,false));

          filemenu.add(setitem);

          setitem.setEnabled(false);

          MenuItem printitem = new MenuItem("打印(P)");

          printitem.setShortcut(newMenuShortcut(KeyEvent.VK_P,false));

          filemenu.add(printitem);

          printitem.setEnabled(false);

          filemenu.addSeparator();

          MenuItem exititem = new MenuItem("退出(X)");

          exititem.setShortcut(newMenuShortcut(KeyEvent.VK_X,false));

          filemenu.add(exititem);

          //添加监听器来实现文件菜单上的各个菜单项的功能

          //新建菜单项的功能实现

          newitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                  String con = content.getText();

                  if(!con.equals("")){//文本域里文本不为空

                      int result =JOptionPane.showConfirmDialog(

                              null, ("是否要保存?"),("保存文件..."),JOptionPane.YES_NO_CANCEL_OPTION);

                      if(result ==JOptionPane.NO_OPTION){//不保存

                             content.setText("");

                      }

                     

                      else if(result ==JOptionPane.CANCEL_OPTION){//取消新建

                      }

                     

                      else if(result ==JOptionPane.YES_OPTION)//选择保存

                      {

                          JFileChooser jfc = newJFileChooser();//用于选择保存路径的文件名

                          int bcf =jfc.showSaveDialog(jf);

 

                          if(bcf ==JFileChooser.APPROVE_OPTION){

                                   try {

                                       //保存文件

                                       BufferedWriterbfw = new BufferedWriter(

                                               newFileWriter(new File(jfc.getSelectedFile().getAbsolutePath()+".txt")));

                                       filePath =jfc.getSelectedFile().getAbsolutePath()+".txt";//获取文件保存的路径

                                      bfw.write(con);//向文件写出数据

                                       bfw.flush();

                                       bfw.close();//关闭输出流

                                   } catch(IOException ex) {

                                      Logger.getLogger(notebook.class.getName()).log(Level.SEVERE, null, ex);

                                   }

                              new notebook();//新建文本文件

                          }

                      }

                  }

              }

                

          });

          //打开菜单项的功能实现

           openitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

//                   JFileChooserjfile = new JFileChooser();

//                   FileNameExtensionFilterfilter = new FileNameExtensionFilter("*.txt","txt");

//                   jfile.setFileFilter(filter);

//                   jfile.setVisible(true);

//                   intreturnval = jfile.showOpenDialog(jfile);

//                   filePath= jfile.getDialogTitle()+jfile.getSelectedFile().getName();

//                   System.out.println(jfile.getSelectedFile());

                     FileDialogdialog = new FileDialog(new JFrame(),"打开....",FileDialog.LOAD);

                     dialog.setVisible(true);

                     filePath= dialog.getDirectory() + dialog.getFile();

           System.out.println(filePath);

                     Filefile = new File(filePath);

                     BufferedReaderbr = null;

                     StringBuildersb = new StringBuilder();

                     try{

                            br= new BufferedReader (new FileReader(file));

                            Stringstr = null;

                            while((str = br.readLine()) != null){

                                          sb.append(str).append("\n");

                                   }

                            content.setText(sb.toString());

                     }

                     catch(FileNotFoundExceptione1){

                            e1.printStackTrace();

                     }

                     catch(IOExceptione1){

                            e1.printStackTrace();

                     }

                     finally{

                            if(br!= null){

                                   try{

                                          br.close();

                                   }

                                   catch(IOExceptione1){

                                          e1.printStackTrace();

                                   }

                            }

                     }

              }              

          });

          //保存菜单项的功能实现

          saveitem.addActionListener(newActionListener(){

                     publicvoid actionPerformed(ActionEvent e) {

//                          FileDialogdialog = new FileDialog(new JFrame(),"保存....",FileDialog.SAVE);

//                          dialog.setVisible(true);

//                          filePath= dialog.getDirectory() + dialog.getFile();

//                          Filefile = new File(filePath);

//                          BufferedWriterbw = null;

//                                               try{

//                                                      bw= new BufferedWriter(new FileWriter(file));

//                                                      bw.write(content.getText());

//                                               }

//                                               catch(FileNotFoundExceptione1){

//                                                      e1.printStackTrace();

//                                               }

//                                               catch(IOExceptione1){

//                                                      e1.printStackTrace();

//                                               }

//                                               finally{

//                                                      if(bw!= null){

//                                                             try{

//                                                                    bw.close();

//                                                             }

//                                                             catch(IOExceptione1){

//                                                                    e1.printStackTrace();

//                                                             }

//                                                      }

//                                               }

                            FileDialogdialog = new FileDialog(new JFrame(),"保存....",FileDialog.SAVE);

                            dialog.setVisible(true);

                            filePath= dialog.getDirectory() + dialog.getFile();

                            if(filePath.equals("")){//没有路径时,就另存为

                             JFileChooser jfc = newJFileChooser();//用于选择保存路径的文件名

                                 int bcf =jfc.showSaveDialog(jf);//弹出保存窗口

 

                                 if(bcf ==JFileChooser.APPROVE_OPTION){

                                          try {

                                              //保存文件

                                              BufferedWriterbfw = new BufferedWriter(

                                                      newFileWriter(newFile(jfc.getSelectedFile().getAbsolutePath()+".txt")));

                                              filePath =jfc.getSelectedFile().getAbsolutePath();

                                             bfw.write(content.getText());//向文件写出数据

                                              bfw.flush();

                                              bfw.close();//关闭输出流

                                          } catch (IOException ex) {

                                             Logger.getLogger(notebook.class.getName()).log(Level.SEVERE, null, ex);

                                          }

                                 }

                         }

                         else{//路径不为空时,保存在原来的路径下

                             try {

                                 //保存文件

                                 BufferedWriter bfw = newBufferedWriter(

                                         new FileWriter(

                                         newFile(filePath)));

                                bfw.write(content.getText());//向文件写出数据

                                 bfw.flush();

                                 bfw.close();//关闭输出流

                             } catch (IOException ex) {

                                Logger.getLogger(notebook.class.getName()).log(Level.SEVERE, null, ex);

                             }

                         }

                     }

                      

                 });

          //另存为菜单项的功能实现

          saveasitem.addActionListener(newActionListener(){

                     publicvoid actionPerformed(ActionEvent e) {

                             JFileChooser jfc = new JFileChooser();//用于选择保存路径的文件名

                 int bcf =jfc.showSaveDialog(jf);//弹出保存窗口

 

                if(bcf ==JFileChooser.APPROVE_OPTION){

                          try {

                              //保存文件

                              BufferedWriterbfw = new BufferedWriter(

                                      newFileWriter(new File(jfc.getSelectedFile().getAbsolutePath()+".txt")));

                              filePath =jfc.getSelectedFile().getAbsolutePath();

                             bfw.write(content.getText());//向文件写出数据

                              bfw.flush();

                              bfw.close();//关闭输出流

                          } catch (IOExceptionex) {

                             Logger.getLogger(notebook.class.getName()).log(Level.SEVERE, null, ex);

                          }

                 }

                     }

                       

                 });

          //页面设置菜单项的功能实现

          setitem.addActionListener(newActionListener(){

                     publicvoid actionPerformed(ActionEvent e) {

                           

                     }

                       

                 });

          //打印菜单项的功能实现

          printitem.addActionListener(newActionListener(){

                     publicvoid actionPerformed(ActionEvent e) {

                           

                     }

                       

                 });

          //退出菜单项的功能实现

          exititem.addActionListener(newActionListener(){

                     publicvoid actionPerformed(ActionEvent e) {

                            Object[]options = { "是的,我要退出", "不好意思,点错了" };

                            intoption = JOptionPane.showOptionDialog(null, "您确定要退出吗?",

                                           "退出提示....",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE,

                                            null,options, options[0]);    

                             if(option == JOptionPane.OK_OPTION){

                             System.exit(0);

                             }

                     }

                    

                       

                 });

        //创建编辑菜单上的各个菜单项并添加到菜单上

          MenuItem undoitem = new MenuItem("撤销(U)");

          undoitem.setShortcut(newMenuShortcut(KeyEvent.VK_Z,false));

          editmenu.add(undoitem); 

          MenuItem cutitem = new MenuItem("剪切(T)");

          cutitem.setShortcut(newMenuShortcut(KeyEvent.VK_X,false));

          editmenu.add(cutitem); 

          MenuItem copyitem = new MenuItem("复制(C)");

          copyitem.setShortcut(newMenuShortcut(KeyEvent.VK_C,false));

          editmenu.add(copyitem); 

          MenuItem pasteitem = new MenuItem("粘贴(P)");

          pasteitem.setShortcut(newMenuShortcut(KeyEvent.VK_V,false));

          editmenu.add(pasteitem); 

          MenuItem deleteitem = new MenuItem("删除(L)");

          deleteitem.setShortcut(newMenuShortcut(KeyEvent.VK_DELETE,false));

          editmenu.add(deleteitem); 

          editmenu.addSeparator();

          MenuItem finditem = new MenuItem("查找(F)");

          finditem.setShortcut(newMenuShortcut(KeyEvent.VK_F,false));

          editmenu.add(finditem); 

          MenuItem nextitem = new MenuItem("查找下一个(N)");

          nextitem.setShortcut(newMenuShortcut(KeyEvent.VK_3,false));

          editmenu.add(nextitem); 

          MenuItem replaceitem = new MenuItem("替换(R)");

          replaceitem.setShortcut(newMenuShortcut(KeyEvent.VK_H,false));

          editmenu.add(replaceitem); 

          MenuItem turntoitem = new MenuItem("转到(G)");

          turntoitem.setShortcut(newMenuShortcut(KeyEvent.VK_G,false));

          editmenu.add(turntoitem); 

          editmenu.addSeparator();

          //复选菜单项

          Menu choicemenu = new Menu("选择(C)");

          MenuItem allitem = new MenuItem("全选(A)");

          allitem.setShortcut(newMenuShortcut(KeyEvent.VK_A,false));

          choicemenu.add(allitem);

          MenuItem fanxiangitem = new MenuItem("反向选择(B)");

          fanxiangitem.setShortcut(newMenuShortcut(KeyEvent.VK_B,false));

          choicemenu.add(fanxiangitem);

          MenuItem chieseitem = new MenuItem("选择汉字(C)");

          chieseitem.setShortcut(new MenuShortcut(KeyEvent.VK_C,false));

          choicemenu.add(chieseitem);

          editmenu.add(choicemenu); 

          //编辑菜单项的时间/日期项

          MenuItem dateitem = new MenuItem("时间/日期(D)");

          dateitem.setShortcut(newMenuShortcut(KeyEvent.VK_5,false));

          editmenu.add(dateitem);

         

         //添加监听器来实现编辑菜单上的各个菜单项的功能

         //撤销菜单项的功能实现

          undoitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                    

              }

                

          });

        //剪切菜单项的功能实现

          cutitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                     Stringtext = content.getSelectedText();

                     StringSelectionselection = new StringSelection(text);

                     clipboard.setContents(selection,null);

                     if(text.length()== 0){

                            return;

                     }

                     else{

                     content.replaceRange("",content.getSelectionStart(),content.getSelectionEnd());

                     }

              }

                

          });

        //复制菜单项的功能实现

          copyitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                     Stringtext = content.getSelectedText();

                     StringSelectionselection = new StringSelection(text);

                     clipboard.setContents(selection,null);

              }

                

          });

        //粘贴菜单项的功能实现

          pasteitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                     Transferablecontents = clipboard.getContents(this);

                     Stringstr =null;

                     try{

                            str= (String) contents.getTransferData(DataFlavor.stringFlavor);

                     }catch (UnsupportedFlavorException e1) {

                            e1.printStackTrace();

                     }catch (IOException e1) {

                            e1.printStackTrace();

                     }

                     if(str == null)

                            return;

                     try{

                            content.replaceRange(str,content.getSelectionStart(),content.getSelectionEnd());

                     }

                     catch(Exception e1) {

                            e1.printStackTrace();

                     }

              }

                

          });

        //删除菜单项的功能实现

          deleteitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                     content.replaceRange("",content.getSelectionStart(),content.getSelectionEnd());

              }

                

          });

        //查找菜单项的功能实现

          finditem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                     finalJDialog dialog = new JDialog(jf,"查找字符串...",true);

                     dialog.setBounds(560,250,310,130);

                     JLabelfind = new JLabel("请输入字符串 :");

                     finalJTextField findtext = new JTextField(1);

                     JButtonjbu = new JButton("查找");

                     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(){

                            publicvoid actionPerformed(ActionEvent e) {

                                   Stringtext = content.getText();

                             String str = findtext.getText();

                             int end = text.length();

                             int len = str.length();

                             int start = 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(){

                   public voidwindowClosing(WindowEvent e){

                       dialog.dispose();

                   }

               });

               dialog.setResizable(false);

                     dialog.setVisible(true);

              }

                

          });

        //查找下一个菜单项的功能实现

          nextitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                    

              }

                

          });

        //替换菜单项的功能实现

          replaceitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                       final JDialog dialog = new JDialog(jf,"字符串替换...",true);

                       dialog.setBounds(560,250,310,180);

                       final JLabel tihuan = new JLabel("请输入要替换的字符串 :");

                       JLabel mubiao = new JLabel("请输入替换后的字符串 :");

                       JTextField jtf1 = new JTextField(10);

                       JTextField jtf2 = new JTextField(10);

                       JButton jb = new JButton("替换");

                       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);

                 final String text =content.getText();

                 final String str1 = tihuan.getText();

                       final String str2 = mubiao.getText();

                       jb.addActionListener(new ActionListener(){

                       public void actionPerformed(ActionEvent e) {

                  if(content.getSelectedText().equals(tihuan.getText())){

                             content.replaceRange(str2,content.getSelectionStart(),content.getSelectionEnd());

                        }

                   else {

                        int end=text.length();

                        int len=str1.length();

                        intstart=content.getSelectionEnd();

                        if(start==end) start=0;

                       for(;start<=end-len;start++){

                            if(text.substring(start,start+len).equals(str1)){

                                   content.setSelectionStart(start);

                                   content.setSelectionEnd(start+len);

                                return;

                            }

                         }

                         //若找不到待查字符串,则将光标置于末尾

                        content.setSelectionStart(end);

                         content.setSelectionEnd(end);

                        }

                      

                            }

                       

                      });

                       dialog.setResizable(false);

              dialog.setVisible(true);

              }

                

          });

        //转到菜单项的功能实现

          turntoitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                    

              }

                

          });

        //全选菜单项的功能实现

          allitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                     content.selectAll();

              }

                

          });

        //反向选择菜单项的功能实现

          fanxiangitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                    

              }

                

          });

        //选择汉字菜单项的功能实现

          chieseitem.addActionListener(new ActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                    

              }

                

          });

          //时间菜单项的功能实现

          dateitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                    

              }

                

          });

        //创建格式菜单上的各个菜单项并添加到菜单上 

          CheckboxMenuItem lineturnitem = newCheckboxMenuItem("自动换行(W)");

          formatmenu.add(lineturnitem);

          formatmenu.addSeparator();

          MenuItem worditem = new MenuItem("字体(F)");

          formatmenu.add(worditem);

          worditem.setEnabled(true);

          formatmenu.addSeparator();

          MenuItem coloritem = new MenuItem("字体颜色(C)");

          formatmenu.add(coloritem);

         

        //添加监听器来实现格式菜单上的各个菜单项的功能

       //自动换行菜单项的功能实现

          lineturnitem.addActionListener(newActionListener(){

                     publicvoid actionPerformed(ActionEvent e) {

                           

                     }

                       

                 });

       //字体菜单项的功能实现

          worditem.addActionListener(newActionListener(){

                     publicvoid actionPerformed(ActionEvent e) {

                             final JFrame ztsz = new JFrame("字体设置...");//字体设置窗口

                         //字体

                         ztsz.setLocation(150, 200);

                         jf.setEnabled(false);//文本编辑窗体不可用!

                         final JComboBox jc = new JComboBox(

                                GraphicsEnvironment.getLocalGraphicsEnvironment()

                                .getAvailableFontFamilyNames());

                         jc.setLocation(30, 80);

                         Container c = ztsz.getContentPane();

                         JPanel jp = new JPanel();

                         jp.add(jc,new FlowLayout());

 

                         //字形

                         String[]   faceString={"正常","粗体","斜体","粗斜体"};

                         String[]   sizeString={"初号","小初","一号","小一","二号","小二",

                                "三号","小三","四号","小四","五号","小五","六号","小六","七号",

                                "八号","5","8","9","10","11","12","14","16","18","20","22","24",

                               "26","28","36","48","72"};

                         final JComboBox zx = newJComboBox(faceString);

                         final JComboBox dx = newJComboBox(sizeString);

                         final JButton sure = newJButton("确定");

                         final JButton cancel = newJButton("取消");

                        

                         jp.add(zx);

                         jp.add(dx);

                         jp.add(sure);

                         jp.add(cancel);

                         c.add(jp);

 

 

                         //确定

                         sure.addActionListener(newActionListener(){

                             public voidactionPerformed(ActionEvent e){//将文本设置成所选的字体

                                     if(!content.getText().equals("")){

                                            content.setFont(newFont(

                                            jc.getActionCommand(),zx.getSelectedIndex(),

                                            dx.getSelectedIndex()));

                                                jf.setEnabled(true);//文本编辑窗体可用

                                                ztsz.dispose();

                                     }

                                     else{

                                        JOptionPane.showMessageDialog(null,

                                                 "您的文本中还没有内容,请输入内容后重新设置!"

                                                 ,"消息...",JOptionPane.INFORMATION_MESSAGE);

                                         jf.setEnabled(true);

                                         ztsz.dispose();

                                     }

                                 }

                         });

                         cancel.addActionListener(newActionListener(){//取消

                             public voidactionPerformed(ActionEvent e){

                                     jf.setEnabled(true);//文本编辑窗体可用

                                     ztsz.dispose();//关闭字体设置窗体

                             }

                         });

                         ztsz.setSize(360, 100);//设置窗体长度100和宽度360

                         ztsz.setVisible(true);//窗体可见

                         ztsz.setResizable(false);//禁止放大窗体

                     }

                });

        //字体颜色菜单项的功能实现

          coloritem.addActionListener(newActionListener(){

                     publicvoid actionPerformed(ActionEvent e) {

                            color=JColorChooser.showDialog(jf,"",color);

                content.setForeground(color);

                     }

                       

                 });

         

   //添加监听器来实现查看菜单上的各个菜单项的功能

       //字数统计菜单项的功能实现 

              MenuItemcountitem = new MenuItem("字数统计(C)"); 

              viewmenu.add(countitem);

              countitem.setEnabled(true);

              countitem.addActionListener(newActionListener(){

              publicvoid actionPerformed(ActionEvent e) {

                                  

                            }

                           

              });

       //状态栏菜单项的功能实现 

              MenuItemstateitem = new MenuItem("状态栏(S)"); 

              viewmenu.add(stateitem);

              stateitem.setEnabled(false);

              stateitem.addActionListener(newActionListener(){

                     publicvoid actionPerformed(ActionEvent e) {

                           

                     }

                    

              });

             

       //创建帮助菜单上的各个菜单项并添加到菜单上

              MenuItemfindhelpitem = new MenuItem("查看帮助(H)"); 

              helpmenu.add(findhelpitem);

              findhelpitem.setEnabled(false);

              helpmenu.addSeparator();

              MenuItemaboutboxitem = new MenuItem("关于记事本(A)"); 

              helpmenu.add(aboutboxitem);

              helpmenu.addSeparator();

              MenuItemwriteritem = new MenuItem("关于作者(S)"); 

              helpmenu.add(writeritem);

       //添加监听器来实现帮助菜单上的各个菜单项的功能

       //查看帮助菜单项的功能实现

              findhelpitem.addActionListener(newActionListener(){

                     publicvoid actionPerformed(ActionEvent e){

                           

                     }

              });

       //关于记事本菜单项的功能实现 

              aboutboxitem.addActionListener(newActionListener(){

                     publicvoid actionPerformed(ActionEvent e){

                            JOptionPane.showMessageDialog(jf,"本软件由孤独的野狼制作!\n如需要源代码,随时欢迎联系作者!\n" +

                                          "作者邮箱:sunchuanhui212@126.com\nQQ号:2442701497\n本程序基本上实现了Microsoft记事本的功能\n" +

                                          "并新增了“反向选择”,“选择汉字”\n" +

                                          "“字数统计”,“自动保存”等功能  ...\n希望您喜欢!\n" +

                                          "如有任何疑问及改善意见,随时欢迎指出,\n我们将尽最大的努力满足您的需求!\n" +

                                          "最后谢谢您的使用!\n版权所有,请勿侵权!","关于记事本...",JOptionPane.INFORMATION_MESSAGE);

                     }

              });

       //关于作者菜单项的功能实现 

              writeritem.addActionListener(newActionListener(){

                     publicvoid actionPerformed(ActionEvent e){

                            JOptionPane.showMessageDialog(jf,"作者:孤独的野狼\n性别:男\n籍贯:湖南邵阳\n出生日:1990年11月9日\n" +

                                          "本科院校:上海应用技术学院\n现居地:上海\n自我介绍:不帅也不丑\n偶像:爱因斯坦\n" +

                                          "最喜欢的歌手:刀郎\n最向往的地方:北京\n座右铭:疯狂源自梦想\n" +

                                          "                 勤奋铸就辉煌\n最喜欢的话:我愿变成一座石桥,受五百年风吹,五百年雨打,\n" +

                                          "                          五百年日晒,只求你从上面走过...\n" +

                                          "梦想:天地有多大,梦有多潇洒\n","关于作者...",JOptionPane.INFORMATION_MESSAGE);

                     }

              });

   //关闭程序事件

    jf.addWindowListener(new WindowAdapter(){

            //程序关闭时的方法

            public void windowClosing(WindowEvent e){

                   int option = JOptionPane.showConfirmDialog(null,"您确定关闭吗?",

                                    "关闭提示....",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);

                      if(option == JOptionPane.OK_OPTION){

                      ((Window) e.getComponent()).dispose();

                      System.exit(0);

                      }

          }    

    });  

   }

   public static void main(String[] args){

          new notebook();

   }

}


0 0
原创粉丝点击