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

📄 notebook.java

📁 用java写的记事本 基本功能与windows自带的相同
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            public void ChangeFont(Font font,Component[] components) {
            	for(Component component:components){
    	    	    component.setBackground(SystemColor.control);
    	    		component.setFont(font);
    	    	    		
			
			}
            }        
                                		
            
           
	        //Document侦听方法
			public void changedUpdate(DocumentEvent arg0) {				
				alreadychange = true;
				alreadysave = false;
				undoMenuItem.setEnabled(true);
				popMenuUndo.setEnabled(true);
				//System.out.println("changed");
			}

			
			public void insertUpdate(DocumentEvent arg0) {			
				alreadychange = true;
				alreadysave = false;
				undoMenuItem.setEnabled(true);
				popMenuUndo.setEnabled(true);
				//System.out.println("insert");
			}

			
			public void removeUpdate(DocumentEvent arg0) {				
			    alreadychange = true;	
			    alreadysave = false;
			    undoMenuItem.setEnabled(true);
			    popMenuUndo.setEnabled(true);
			   // System.out.println("remove");
			}
			
			
			
		//打开	
    	public void open(){
				textArea.getDocument().removeDocumentListener(this);
				jFileChooser = new JFileChooser();
      		    FileFilter filter = new FileNameExtensionFilter("文本文档(*.txt)","txt");
      		    jFileChooser.setFileFilter(filter);
      		    int i = jFileChooser.showOpenDialog(getContentPane());
      		    
     		 if(i == JFileChooser.APPROVE_OPTION){      			  
      			  selectFile = jFileChooser.getSelectedFile();      		     			  
      			  textArea.setText("");
      			  filename = selectFile.getName();
      			  String dsString = jFileChooser.getCurrentDirectory()+"\\"+filename;
      			  if(new File(dsString).exists()){
      			      NoteBook.this.setTitle(filename+"-记事本");       			  
      			      String string = null;      	            			
      			      try {
      				    bufferedReader = new BufferedReader(new FileReader(selectFile.getPath()));
      			        } catch (FileNotFoundException e1) {
      				     // TODO Auto-generated catch block
      				    e1.printStackTrace();
      		        	}
      			
      		        	try {
      			    	//string = bufferedReader.readLine();
      			    	while((string=bufferedReader.readLine())!=null){      					
      					this.textArea.append(string+ "\n");      					
       				     }
      			    	bufferedReader.close();  				
           			   } catch (IOException e) {
      				   // TODO Auto-generated catch block
      				   e.printStackTrace();
      			       }
      			     } else{
			    	  JOptionPane.showMessageDialog(null, "找不到该文件,请确认文件名是否正确!", "打开", 1);
			    	  
			         } 
			     }
      		    textArea.getDocument().addDocumentListener(this);
			}
			
			private File File(String string) {
				// TODO Auto-generated method stub
				return null;
			}

		//保存	
		public void save() {
				
		if(filename == "无标题"){
      			  saveas();
      			  
           }else{
        	   
				   FileWriter fw = null;
				  try {
					fw = new FileWriter(selectFile);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				try {
					fw.write(textArea.getText());
					fw.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					
					fw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				alreadysave = true;
           }
	 }
		
		   //另存为
			public void saveas() {
				   String filesString;
				  jFileChooser = new JFileChooser();
	      		  FileFilter filter = new FileNameExtensionFilter("文本文档(*.txt)","txt");
	      		  jFileChooser.setFileFilter(filter);
	      		  int i = jFileChooser.showSaveDialog(getContentPane());
	      		  if(i == JFileChooser.APPROVE_OPTION){
	      			  
	      			  selectFile = jFileChooser.getSelectedFile();
	      			  if (!selectFile.getName().contains(".txt")){
	      			  filename = selectFile.getName()+".txt";
	      			  filesString = jFileChooser.getCurrentDirectory()+"\\"+selectFile.getName()+".txt";
	      			  
	      			   		   
	      			  }else {
	      				  filename = selectFile.getName();
		      			  filesString = jFileChooser.getCurrentDirectory()+"\\"+selectFile.getName();
		      			  
		      			  
	      			  }
	      			       File file = new File(filesString); 
	      		               if(!file.exists()){	      			  
	      		     	  try {
	      				       FileWriter fWriter = new FileWriter(file);
						       fWriter.write(this.textArea.getText());
					        	fWriter.close();
						
					    } catch (IOException e) {					
						e.printStackTrace();
					    }
	      			  
	      			  
			                  } else{
			    	               int j = JOptionPane.showConfirmDialog(null, filesString+"已存在,想替换它吗?","另存为",JOptionPane.YES_NO_OPTION	, 3);
			    	                if(j == JOptionPane.YES_OPTION){
			    	                	FileWriter fWriter = null;
										try {
											fWriter = new FileWriter(file);
										} catch (IOException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
			    						try {
											fWriter.write(this.textArea.getText());
										} catch (IOException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
			    						try {
											fWriter.close();
										} catch (IOException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
			                       }else {
			    	                   	   return;
			    	                 }
			    	             
			           }
	      		             NoteBook.this.setTitle(filename+"-记事本");   
			       }
			}
			
			 //显示字体框体
			 public void showFontSetBox() {
			        FontDialog dlg = new FontDialog(this);
			        Dimension dlgSize = dlg.getPreferredSize();
			        Dimension frmSize = getSize();
			        Point loc = getLocation();
			        dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
			                        (frmSize.height - dlgSize.height) / 2 + loc.y);
			        dlg.setModal(true);
			        dlg.pack();
			        dlg.setVisible(true);
			 }
		
		//转到	 
		public void goToBox() {
			        GotoDialog dlg = new GotoDialog(this);
			        Dimension dlgSize = dlg.getPreferredSize();
			        Dimension frmSize = getSize();
			        Point loc = getLocation();
			        dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
			                        (frmSize.height - dlgSize.height) / 2 + loc.y);
			        dlg.setModal(true);
			        dlg.pack();
			        dlg.setVisible(true);

			    }
		
		//替换
		public void replaceString() {
	        ReplaceDialog dlg = new ReplaceDialog(this);
	        Dimension dlgSize = dlg.getPreferredSize();
	        Dimension frmSize = getSize();
	        Point loc = getLocation();
	        dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
	                        (frmSize.height - dlgSize.height) / 2 + loc.y);
	        dlg.setModal(true);
	        dlg.pack();
	        dlg.setVisible(true);

	    }
		//查找
		public void findFile() {
	        FindDialog dlg = new FindDialog(this);
	        Dimension dlgSize = dlg.getPreferredSize();
	        Dimension frmSize = getSize();
	        Point loc = getLocation();
	        dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
	                        (frmSize.height - dlgSize.height) / 2 + loc.y);
	        dlg.setModal(true);
	        dlg.pack();
	        dlg.setVisible(true);
	        alreadyfind = true;
	    }
		
		//查找下一个
		 public void findNextFile() {
		        if (alreadyfind == false) {
		            findFile();
		        } else {
		            int a = 0, b = 0;
		            String str1, str2, str3, str4, strA, strB;
		            str1 = textArea.getText();
		            str2 = strFind; //查找的字符串
		            a = str1.indexOf(str2, FindStartPos);
		            if (a > -1) {
		                textArea.setCaretPosition(a);
		                b = strFind.length();
		                textArea.select(a, a + b);
		                FindStartPos = a + b;
		            } else {
		                JOptionPane.showMessageDialog(null, "找不到\"" + str2 + "\".",
		                                              "记事本", 1);
		                FindStartPos = 0;
		            }
		        }
		    }
		 //撤销
		 public void undoDocument() {
		        if (undo.canUndo()) {
		            try {
		                undo.undo();
		            } catch (CannotUndoException ex) {
		                System.out.println("不能撤销: " + ex);
		                ex.printStackTrace();
		            }
		            if (!undo.canUndo()) {
		                this.undoMenuItem.setEnabled(false);
		                popupMenu.setEnabled(false);
		            }
		        }

		    }
		 
		 
	 

		    //退出
			class ExitSystem extends WindowAdapter {
		        public void windowClosing(WindowEvent e) {
		            noteBook = (NoteBook) e.getWindow();		           
		            if (alreadychange == true && alreadysave == false) {
		                int msg = JOptionPane.showConfirmDialog(null,
		                        "文件内容已改变,想保存文件吗?.",
		                        "记事本", JOptionPane.YES_NO_CANCEL_OPTION, 3);
		                if (msg == JOptionPane.YES_OPTION) {
		                    save();
		                    System.exit(0);
		                } else if (msg == JOptionPane.NO_OPTION) {
		                    System.exit(0);
		                } else if (msg == JOptionPane.CANCEL_OPTION) {
		                    return;
		                }
		            } else {
		                System.exit(0);
		            }
		        }

			}
			//撤销
			class UndoHandler implements UndoableEditListener {
		        public void undoableEditHappened(UndoableEditEvent e) {
		            undo.addEdit(e.getEdit());
		        }
		    }

		
		
			
			 
			 
}

			

		
	      
	

          
     





⌨️ 快捷键说明

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