⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dreamtimesnotepad.java

📁 能实现记事本的基本功能
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	    
	    JButton toolpaste = new JButton(Iconpaste);
	    toolpaste.setToolTipText("粘贴 (Ctrl+V)");
	    toolpaste.addActionListener(new ActionListener() { //加入事件监听
	      public void actionPerformed(ActionEvent e) {
	            text.paste();    //调用text的paste()方法实现粘贴
	        }
	    });
	    JButton toolcut = new JButton(Iconcut);
	    toolcut.setToolTipText("剪切 (Ctrl+X)");
	    toolcut.addActionListener(new ActionListener() { //加入事件监听
	      public void actionPerformed(ActionEvent e) {
	            text.cut();     //调用text的cut()方法实现剪切
	        }
	    });
	    JButton tooldelete = new JButton(Icondelete);
	    tooldelete.setToolTipText("删除 (Ctrl+D)");
	    tooldelete.addActionListener(new ActionListener() { //加入事件监听
	      public void actionPerformed(ActionEvent e) {
	            delete();       //调用 delete() 删除选定文本
	        }
	    });
	    JButton toolundo = new JButton(Iconundo);
	    toolundo.setToolTipText("撤消 (Ctrl+Z)");
	    toolundo.addActionListener(new ActionListener() {  //加入事件监听
	      public void actionPerformed(ActionEvent e) {
	            setundo();      //调用 setundo() 进行查找
	        }
	    });
	     
	    JButton toolexit = new JButton(Iconexit);
	    toolexit.setToolTipText("退出 (Alt+F4)");
	    toolexit.addActionListener(new ActionListener() { //加入事件监听
	      public void actionPerformed(ActionEvent e) {
	            exit();        //退出程序
	        }
	    }); 
	    
	    JButton toolabout = new JButton(Iconabout);
	    toolabout.setToolTipText("关于 (F1)");
	    toolabout.addActionListener(new ActionListener() {  //加入事件监听
	      public void actionPerformed(ActionEvent e) {
	            about();        //调用 find() 显示关于对话框
	        }
	    });
	     
	    //把文件菜单中的各个子菜单加入到文件菜单中 
	           
	    File.add(New);
	    File.add(Open);
	    File.addSeparator();   //加入菜单分隔条
	    File.add(Save);
	    File.add(SaveAs);
	    File.addSeparator();   //加入菜单分隔条
	    File.add(PageSetup);
	    File.add(Print);
	    File.addSeparator();   //加入菜单分隔条
	    File.add(Exit);
	     
	    //把编辑菜单中的各个子菜单加入到编辑菜单中       
	    Edit.add(Undo);
	    Edit.addSeparator();
	    Edit.add(Copy);
	    Edit.add(Paste);
	    Edit.add(Cut);
	    Edit.addSeparator();  //加入菜单分隔条
	    Edit.add(SelectAll);
	    Edit.add(Delete);
	    
	    //把查找菜单中的各个子菜单加入到查找菜单中
	    Search.add(Find);
	    Search.add(FindNext);
	    
	    //把格式菜单中的各个子菜单加入到格式菜单中
	    Format.add(mFont);
	    Format.add(fontColor);
	    Format.add(backColor);
	    
	    //把帮助菜单中的各个子菜单加入到帮助、菜单中
	    Help.add(About);
	    	        
	    mb.add(File);              //把文件菜单加入到菜单条中
	    mb.add(Edit);              //把编辑菜单加入到菜单条中
	    mb.add(Search);            //把查找菜单加入到菜单条中
	    mb.add(Format);            //把格式菜单加入到菜单条中
	    mb.add(Help);              //把帮助菜单加入到菜单条中
	    
	    toolbar.add(toolprint);    //把工具栏图标加入到工具条中
	    toolbar.addSeparator();    //加入工具栏分隔条
	    toolbar.add(toolnew);
	    toolbar.add(toolopen);
	    toolbar.add(toolsave);
	    toolbar.addSeparator();
	    toolbar.add(toolcopy);
	    toolbar.add(toolpaste);
	    toolbar.add(toolcut);
	    toolbar.add(tooldelete);
	    toolbar.addSeparator();
	    toolbar.add(toolundo);
	    toolbar.addSeparator();
	    toolbar.add(toolexit);
	    toolbar.addSeparator();
	    toolbar.add(toolabout);
	    
	    //将常用菜单加入到弹出菜单中
	    pm.add(pUndo);
	    pm.addSeparator();            
	    pm.add(pCopy);
	    pm.add(pPaste);
	    pm.add(pCut);
	    pm.add(pDelete);
	    pm.addSeparator();
	    pm.add(pSelectAll);
	    text.add(pm);
	    
	    f.addWindowListener(new WindowAdapter() {
	    	public void windowClosing(WindowEvent e) { 
	    	    exit();    //关闭窗口
	    	  }
	    });
	             
	    f.setJMenuBar(mb); //加入菜单栏
	    f.getContentPane().add("North",toolbar);   //加入工具条
	    f.getContentPane().add("Center",scroller); //加入文本域
	    f.getContentPane().add("South", p);        //加入状态栏
	    
	    f.setVisible(true);
	    
	}
	
	public void mouseReleased(MouseEvent e) {
	if(e.isPopupTrigger())
	  pm.show((Component)e.getSource(),e.getX(),e.getY());
	}
	
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mousePressed(MouseEvent e){}  
	
	//以下语句监视文本域的内容是否,改变则使文件状态为"*"
	//表示文件没有保存
	public void removeUpdate(DocumentEvent e) {
	String s;
	s = statusFile.getText();
	if(!s.endsWith("*") & beginTextListener & !isNewFile) {
	  statusFile.setText("    *   ");
	}
	Undo.setEnabled(true);
	pUndo.setEnabled(true);
	}
	public void insertUpdate(DocumentEvent e) {
	String s;
	s = statusFile.getText();
	if(!s.endsWith("*") & beginTextListener & !isNewFile) {
	  statusFile.setText("    *   ");
	}
	Undo.setEnabled(true);
	pUndo.setEnabled(true);
	}
	public void changedUpdate(DocumentEvent e) {
	String s;
	s = statusFile.getText();
	if(!s.endsWith("*") & beginTextListener & !isNewFile) {
	  statusFile.setText("    *   ");
	}
	Undo.setEnabled(true);
	pUndo.setEnabled(true);
	}
	
	//利用事件源判断被点击的菜单项并调用相应的方法响应菜单事件
	public void actionPerformed(ActionEvent e) {
	     
	 if(e.getSource() == New) {
	         setnew();               //调用newFile()新建文件           
	      }     
	 else if (e.getSource() == Open){
	         setopen();             //调用openFile()打开文件
	      } 
	 else if (e.getSource() == Save){
	  	     if(fns !="新文件") {
	            saveFile();
	          }      
	         else {
	          saveNewFile(); 
	  	     }
	      }     
	 else if (e.getSource() == SaveAs){
	          saveNewFile(); 
	  	  }
	 else if (e.getSource() == PageSetup){ 
	          prt.printDialog();      //显示打印设置对话框
	      } 
	 else if (e.getSource() == Print) {
	          print();                //调用print()实现打印
	      }       
	 else if (e.getSource() == Exit) {
	         exit();                  //调用System.exit(0)退出程序
	      }
	 else if (e.getSource() == Undo || e.getSource() == pUndo ) {
	 	     setundo();
	      }     
	 else if (e.getSource() == Copy || e.getSource() == pCopy ) {
	 	     text.copy();             //调用text.copy()实现复制
	      }
	 else if (e.getSource() == Paste || e.getSource() == pPaste) {
	 	     text.paste();            //调用text.paste()实现粘贴
	      }
	 else if (e.getSource() == Cut || e.getSource() == pCut) {
	 	     text.cut();              //调用text.cut()实现剪切
	      }
	 else if (e.getSource() == SelectAll || e.getSource() == pSelectAll) {
	 	     text.selectAll();        //调用text.selectAll()实现全选
	      }
	 else if (e.getSource() == Delete || e.getSource() == pDelete) {
	 	     delete();               //调用delete()实现删除
	 	  }    
	 else if (e.getSource() == Find) {
	 	      find();                //调用find()实现查找
	      }      
	 else if (e.getSource() == fontColor) {
	 	     fontcolor();            //调用fontcolor()  设置字体颜色
	      }
	 else if (e.getSource() == backColor) {
	 	     backcolor();            //调用backcolor()  设置背景颜色
	      }
	 else if (e.getSource() == About) {
	 	     about();                //调用about()显示关于对话框
	      }                             
	}  	         
	
	         
	void newFile() {                                      //新建文件
	  text.setText("");
	  fns = "新文件";
	  file = null;
	  //在窗体中显示标题和文件名
	  f.setTitle("记事本 - [新文件]");
	  statusFile.setText("新文件!");
	 }
	
	//判断当前打开文件是否是新文件,是否已经保存,并调用newFile()
	//实现新建文件  
	void setnew(){
	 if(statusFile.getText().endsWith("    *   ")) {
	    int option = JOptionPane.showConfirmDialog(null,"文件  "
	    +fns+" 文字已经被修改! \n 你是否要保存该文件再新建?","警告!",
	    JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
	    if(fns == "新文件") {
	      switch(option) {
	        case JOptionPane.NO_OPTION :
	          newFile();
	          break;
	        case JOptionPane.YES_OPTION :
	          saveNewFile();
	          newFile();
	          break;
	        default:
	          break;
	      }
	    }
	    else {
	      switch(option) {
	        case JOptionPane.NO_OPTION :
	          newFile();
	          break;
	        case JOptionPane.YES_OPTION :
	          saveFile();
	          newFile();
	          break;
	        default:
	          break;
	         }
	        }
	      }
	   else {
	  	newFile();
	    }   
	  }
	
	void  openFile() {                      //打开文件
	  String s = null;
	  int rv = fc.showOpenDialog(DreamTimesNotePad.this);
	  if (rv == JFileChooser.APPROVE_OPTION) {
	  file = fc.getSelectedFile();         //得到文件路径和文件名
	  fns = file.getName();
	  StringBuffer strPool = new StringBuffer();
	  BufferedReader br;
	  try {                                //抛出异常
	    br = new BufferedReader(new FileReader(file));
	    s = br.readLine();                  //用循环读入文本并存入缓存
	    while(s != null) {
	     strPool.append(s + "\12");         //
	     s = br.readLine();
	     }
	    br.close();                         //关闭文件对象
	    text.setText(strPool.toString());   //将文本内容置入文本域
	    statusFile.setText("已打开!");
	    } catch(Exception e) { e.printStackTrace(); }
	   //在窗体中显示标题和文件名
	    f.setTitle("梦想年华记事本 - [" + file.getName() + "]");
	    
	}
	}  
	
	//判断当前打开文件是否是新文件,是否已经保存,并调用openFile()
	//实现打开
	void setopen(){
	 if(statusFile.getText().endsWith("    *   ")) {
	    int option = JOptionPane.showConfirmDialog(null,"文件  "
	    +fns+" 文字已经被修改! \n 你是否要保存该文件再打开?","警告!",
	    JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
	    if(fns == "新文件") {
	      switch(option) {
	        case JOptionPane.NO_OPTION :
	          openFile();
	          break;
	        case JOptionPane.YES_OPTION :
	          saveNewFile();
	          openFile();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -