java記事本源代碼

<span style="font-size:24px;">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;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.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 = new TextArea("",50,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
	   jf.add(content);
	   content.setVisible(true);	
	   content.requestFocusInWindow();
	   //菜單欄添加內容
	   Menu filemenu = 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(new MenuShortcut(KeyEvent.VK_N,false));
	   filemenu.add(newitem);
	   MenuItem openitem = new MenuItem("打開(O)");
	   openitem.setShortcut(new MenuShortcut(KeyEvent.VK_O,false));
	   filemenu.add(openitem);
	   MenuItem saveitem = new MenuItem("保存(S)");
	   saveitem.setShortcut(new MenuShortcut(KeyEvent.VK_S,false));
	   filemenu.add(saveitem);
	   MenuItem saveasitem = new MenuItem("另存爲(A)");
	   saveasitem.setShortcut(new MenuShortcut(KeyEvent.VK_A,false));
	   filemenu.add(saveasitem);
	   MenuItem setitem = new MenuItem("頁面設置(U)");
	   setitem.setShortcut(new MenuShortcut(KeyEvent.VK_U,false));
	   filemenu.add(setitem);
	   setitem.setEnabled(false);
	   MenuItem printitem = new MenuItem("打印(P)");
	   printitem.setShortcut(new MenuShortcut(KeyEvent.VK_P,false));
	   filemenu.add(printitem);
	   printitem.setEnabled(false);
	   filemenu.addSeparator();
	   MenuItem exititem = new MenuItem("退出(X)");
	   exititem.setShortcut(new MenuShortcut(KeyEvent.VK_X,false));
	   filemenu.add(exititem);
	   //添加監聽器來實現文件菜單上的各個菜單項的功能
	   //新建菜單項的功能實現
	   newitem.addActionListener(new ActionListener(){
		public void 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 = new JFileChooser();//用於選擇保存路徑的文件名
	                   int bcf = jfc.showSaveDialog(jf);

	                   if(bcf == JFileChooser.APPROVE_OPTION){
	                            try {
	                                //保存文件
	                                BufferedWriter bfw = new BufferedWriter(
	                                        new FileWriter(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(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
//			JFileChooser jfile = new JFileChooser();
//			FileNameExtensionFilter filter = new FileNameExtensionFilter("*.txt","txt");
//			jfile.setFileFilter(filter);
//			jfile.setVisible(true);
//			int returnval = jfile.showOpenDialog(jfile);
//			filePath = jfile.getDialogTitle()+jfile.getSelectedFile().getName();
//			System.out.println(jfile.getSelectedFile());
			FileDialog dialog = new FileDialog(new JFrame(),"打開....",FileDialog.LOAD);
			dialog.setVisible(true);
			filePath = dialog.getDirectory() + dialog.getFile();
            System.out.println(filePath);
			File file = new File(filePath);
			BufferedReader br = null;
			StringBuilder sb = new StringBuilder();
			try{
				br = new BufferedReader (new FileReader(file));
				String str = null;
				while ((str = br.readLine()) != null){
						sb.append(str).append("\n");
					}
				content.setText(sb.toString());
			}
			catch(FileNotFoundException e1){
				e1.printStackTrace();
			}
			catch(IOException e1){
				e1.printStackTrace();
			}
			finally{
				if(br != null){
					try{
						br.close();
					}
					catch(IOException e1){
						e1.printStackTrace();
					}
				}
			}
		}		   
	   });
	   //保存菜單項的功能實現
	   saveitem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
//				FileDialog dialog = new FileDialog(new JFrame(),"保存....",FileDialog.SAVE);
//				dialog.setVisible(true);
//				filePath = dialog.getDirectory() + dialog.getFile();
//				File file = new File(filePath);
//				BufferedWriter bw = null;
//							try{
//								bw = new BufferedWriter(new FileWriter(file));
//								bw.write(content.getText());
//							}
//							catch(FileNotFoundException e1){
//								e1.printStackTrace();
//							}
//							catch(IOException e1){
//								e1.printStackTrace();
//							}
//							finally{
//								if(bw != null){
//									try{
//										bw.close();
//									}
//									catch(IOException e1){
//										e1.printStackTrace();
//									}
//								}
//							}
				FileDialog dialog = new FileDialog(new JFrame(),"保存....",FileDialog.SAVE);
				dialog.setVisible(true);
				filePath = dialog.getDirectory() + dialog.getFile();
				if(filePath.equals("")){//沒有路徑時,就另存爲
		               JFileChooser jfc = new JFileChooser();//用於選擇保存路徑的文件名
		                   int bcf = jfc.showSaveDialog(jf);//彈出保存窗口

		                   if(bcf == JFileChooser.APPROVE_OPTION){
		                            try {
		                                //保存文件
		                                BufferedWriter bfw = new BufferedWriter(
		                                        new FileWriter(new File(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 = new BufferedWriter(
		                           new FileWriter(
		                           new File(filePath)));
		                   bfw.write(content.getText());//向文件寫出數據
		                   bfw.flush();
		                   bfw.close();//關閉輸出流
		               } catch (IOException ex) {
		                   Logger.getLogger(notebook.class.getName()).log(Level.SEVERE, null, ex);
		               }
		           }
			}
			  
		   });
	   //另存爲菜單項的功能實現
	   saveasitem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				 JFileChooser jfc = new JFileChooser();//用於選擇保存路徑的文件名
                 int bcf = jfc.showSaveDialog(jf);//彈出保存窗口

                 if(bcf == JFileChooser.APPROVE_OPTION){
                          try {
                              //保存文件
                              BufferedWriter bfw = new BufferedWriter(
                                      new FileWriter(new File(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);
                          }
                 }
			}
			   
		   });
	   //頁面設置菜單項的功能實現
	   setitem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				
			}
			   
		   });
	   //打印菜單項的功能實現
	   printitem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				
			}
			   
		   });
	   //退出菜單項的功能實現
	   exititem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Object[] options = { "是的,我要退出", "不好意思,點錯了" };
				int option = 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(new MenuShortcut(KeyEvent.VK_Z,false));
	   editmenu.add(undoitem);  
	   MenuItem cutitem = new MenuItem("剪切(T)");
	   cutitem.setShortcut(new MenuShortcut(KeyEvent.VK_X,false));
	   editmenu.add(cutitem);  
	   MenuItem copyitem = new MenuItem("複製(C)");
	   copyitem.setShortcut(new MenuShortcut(KeyEvent.VK_C,false));
	   editmenu.add(copyitem);  
	   MenuItem pasteitem = new MenuItem("粘貼(P)");
	   pasteitem.setShortcut(new MenuShortcut(KeyEvent.VK_V,false));
	   editmenu.add(pasteitem);  
	   MenuItem deleteitem = new MenuItem("刪除(L)");
	   deleteitem.setShortcut(new MenuShortcut(KeyEvent.VK_DELETE,false));
	   editmenu.add(deleteitem);  
	   editmenu.addSeparator();
	   MenuItem finditem = new MenuItem("查找(F)");
	   finditem.setShortcut(new MenuShortcut(KeyEvent.VK_F,false));
	   editmenu.add(finditem);  
	   MenuItem nextitem = new MenuItem("查找下一個(N)");
	   nextitem.setShortcut(new MenuShortcut(KeyEvent.VK_3,false));
	   editmenu.add(nextitem);  
	   MenuItem replaceitem = new MenuItem("替換(R)");
	   replaceitem.setShortcut(new MenuShortcut(KeyEvent.VK_H,false));
	   editmenu.add(replaceitem);  
	   MenuItem turntoitem = new MenuItem("轉到(G)");
	   turntoitem.setShortcut(new MenuShortcut(KeyEvent.VK_G,false));
	   editmenu.add(turntoitem);  
	   editmenu.addSeparator();
	   //複選菜單項
	   Menu choicemenu = new Menu("選擇(C)");
	   MenuItem allitem = new MenuItem("全選(A)");
	   allitem.setShortcut(new MenuShortcut(KeyEvent.VK_A,false));
	   choicemenu.add(allitem);
	   MenuItem fanxiangitem = new MenuItem("反向選擇(B)");
	   fanxiangitem.setShortcut(new MenuShortcut(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(new MenuShortcut(KeyEvent.VK_5,false));
	   editmenu.add(dateitem); 
	   
	  //添加監聽器來實現編輯菜單上的各個菜單項的功能
	  //撤銷菜單項的功能實現
	   undoitem.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			
		}
		   
	   });
	 //剪切菜單項的功能實現
	   cutitem.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			String text = content.getSelectedText();
			StringSelection selection = new StringSelection(text);
			clipboard.setContents(selection, null);
			if(text.length() == 0){
				return;
			}
			else{
			content.replaceRange("", content.getSelectionStart(),content.getSelectionEnd());
			}
		}
		   
	   });
	 //複製菜單項的功能實現
	   copyitem.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			String text = content.getSelectedText();
			StringSelection selection = new StringSelection(text);
			clipboard.setContents(selection, null);
		}
		   
	   });
	 //粘貼菜單項的功能實現
	   pasteitem.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			Transferable contents = clipboard.getContents(this);
			String str =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(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			content.replaceRange("",content.getSelectionStart(),content.getSelectionEnd());
		}
		   
	   });
	 //查找菜單項的功能實現
	   finditem.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			final JDialog dialog = new JDialog(jf,"查找字符串...",true);
			dialog.setBounds(560,250,310,130);
			JLabel find = new JLabel("請輸入字符串 :");
			final JTextField findtext = new JTextField(1);
			JButton jbu = 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(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					String text = 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(new WindowAdapter(){
	            public void windowClosing(WindowEvent e){
	                dialog.dispose();
	            }
	        });
	        dialog.setResizable(false);
			dialog.setVisible(true);
		}
		   
	   });
	 //查找下一個菜單項的功能實現
	   nextitem.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			
		}
		   
	   });
	 //替換菜單項的功能實現
	   replaceitem.addActionListener(new ActionListener(){
		public void 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();
	                 int start=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(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			
		}
		   
	   });
	 //全選菜單項的功能實現
	   allitem.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			content.selectAll();
		}
		   
	   });
	 //反向選擇菜單項的功能實現
	   fanxiangitem.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			
		}
		   
	   });
	 //選擇漢字菜單項的功能實現
	   chieseitem.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			
		}
		   
	   });
	   //時間菜單項的功能實現
	   dateitem.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			
		}
		   
	   });
	 //創建格式菜單上的各個菜單項並添加到菜單上  
	   CheckboxMenuItem lineturnitem = new CheckboxMenuItem("自動換行(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(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				
			}
			   
		   });
	//字體菜單項的功能實現
	   worditem.addActionListener(new ActionListener(){
			public void 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 = new JComboBox(faceString);
		           final JComboBox dx = new JComboBox(sizeString);
		           final JButton sure = new JButton("確定");
		           final JButton cancel = new JButton("取消");
		           
		           jp.add(zx);
		           jp.add(dx);
		           jp.add(sure);
		           jp.add(cancel);
		           c.add(jp);


		           //確定
		           sure.addActionListener(new ActionListener(){
		               public void actionPerformed(ActionEvent e){//將文本設置成所選的字體
		                       if(!content.getText().equals("")){
		                    	   content.setFont(new Font(
		                               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(new ActionListener(){//取消
		               public void actionPerformed(ActionEvent e){
		                       jf.setEnabled(true);//文本編輯窗體可用
		                       ztsz.dispose();//關閉字體設置窗體
		               }
		           });
		           ztsz.setSize(360, 100);//設置窗體長度100和寬度360
		           ztsz.setVisible(true);//窗體可見
		           ztsz.setResizable(false);//禁止放大窗體
			}
		  });
	 //字體顏色菜單項的功能實現
	   coloritem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				color=JColorChooser.showDialog(jf,"",color);
                content.setForeground(color);
			}
			   
		   });
	   
    //添加監聽器來實現查看菜單上的各個菜單項的功能
	//字數統計菜單項的功能實現  
		MenuItem countitem = new MenuItem("字數統計(C)");  
		viewmenu.add(countitem);
		countitem.setEnabled(true);
		countitem.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
					
				}
				
		});
	//狀態欄菜單項的功能實現  
		MenuItem stateitem = new MenuItem("狀態欄(S)");  
		viewmenu.add(stateitem);
		stateitem.setEnabled(false);
		stateitem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				
			}
			
		});
		
	//創建幫助菜單上的各個菜單項並添加到菜單上
		MenuItem findhelpitem = new MenuItem("查看幫助(H)");  
		helpmenu.add(findhelpitem);
		findhelpitem.setEnabled(false);
		helpmenu.addSeparator();
		MenuItem aboutboxitem = new MenuItem("關於記事本(A)");  
		helpmenu.add(aboutboxitem);
		helpmenu.addSeparator();
		MenuItem writeritem = new MenuItem("關於作者(S)");  
		helpmenu.add(writeritem);
	//添加監聽器來實現幫助菜單上的各個菜單項的功能
	//查看幫助菜單項的功能實現 
		findhelpitem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				
			}
		});
	//關於記事本菜單項的功能實現  
		aboutboxitem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				JOptionPane.showMessageDialog(jf,"本軟件由孤獨的野狼製作!\n如需要源代碼,隨時歡迎聯繫作者!\n" +
						"作者郵箱:[email protected]\nQQ號:2442701497\n本程序基本上實現了Microsoft記事本的功能\n" +
						"並新增了“反向選擇”,“選擇漢字”\n" +
						"“字數統計”,“自動保存”等功能  ...\n希望您喜歡!\n" +
						"如有任何疑問及改善意見,隨時歡迎指出,\n我們將盡最大的努力滿足您的需求!\n" +
						"最後謝謝您的使用!\n版權所有,請勿侵權!","關於記事本...",JOptionPane.INFORMATION_MESSAGE);
			}
		});
	//關於作者菜單項的功能實現  
		writeritem.addActionListener(new ActionListener(){
			public void 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();
   }
}
//新構想:
//記事本程序添加音樂盒功能,音樂在後臺播放
//程序並不實際保存音樂文件,只保存音樂文件的地址
//爲程序保留20M空間或20M的數據庫用於音樂播放
//爲本程序添加自動保存功能,每一分鐘保存一次,後臺保存
//可能要用到多線程
//如果用戶選擇的保存地址與默認的地址不同,則刪除默認地址保存的文件
//並且在本次操作中一直使用用戶選擇的保存地址
//但在下次操作時仍使用本程序默認的地址
//告訴用戶默認的地址,讓用戶能找到程序爲用戶保存的文件
//最好在“關於記事本”的文檔中插入相應的超鏈接,方便用戶
//這樣做能讓用戶在出現死機和突然斷電時勞動成果不至於付諸東流

</span>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章