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

📄 notebook.txt

📁 记事本的源代码
💻 TXT
📖 第 1 页 / 共 3 页
字号:
					if(replaceText.getText().length() > 0 && editArea.getSelectedText() != null) 
					{
						editArea.replaceSelection(replaceText.getText()); 
						replaceCount++;
					}
				}//end while
			}
		});	/*"替换全部"按钮的事件处理结束*/
		
		//"查找下一个"按钮事件处理
		searchNext.addActionListener(new ActionListener(){
				
				public void actionPerformed(ActionEvent e){
						
					int a = 0, b =0; 
					int FindStartPos=editArea.getCaretPosition();
					String str1, str2, str3, str4, strA, strB; 
					str1 = editArea.getText(); 
					str2 = str1.toLowerCase(); 
					str3 = findText.getText(); 
					str4 = str3.toLowerCase(); 
											
					//"区分大小写"的CheckBox被选中
					if(matchcase.isSelected()) { 
						strA = str1; 
						strB = str3; 
					} 
					else { 
						strA = str2; 
						strB = str4; 
					} 
				
					if(up.isSelected()){
						
						if(editArea.getSelectedText()==null){							
							a = strA.lastIndexOf(strB, FindStartPos-1);
						}
						else{
						a = strA.lastIndexOf(strB, FindStartPos-findText.getText().length()-1);	
						}
					}
					else if(down.isSelected()){
							
						if(editArea.getSelectedText()==null){
							a = strA.indexOf(strB, FindStartPos);
						}
						else{
							a=strA.indexOf(strB,FindStartPos-findText.getText().length()+1);
						}
							
					}
					
					if(a > -1) { 
						if(up.isSelected()){
							editArea.setCaretPosition(a);
							b = findText.getText().length(); 
							editArea.select(a, a + b); 
						}
						else if(down.isSelected()){
							editArea.setCaretPosition(a); 
							b = findText.getText().length(); 
							editArea.select(a, a + b);
						}
					}
					else { 
						JOptionPane.showMessageDialog(null, "找不到您查找的内容!", "记事本",JOptionPane.INFORMATION_MESSAGE); 
					} 
					
				}
		});/*"查找下一个"按钮事件处理结束*/
		
		//"取消"按钮及事件处理		
		JButton cancel=new JButton("取消");
		cancel.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					findDialog.dispose();
				}
		});
	
		//创建"查找与替换"对话框的界面
		JPanel bottomPanel=new JPanel();
		JPanel centerPanel=new JPanel();
		JPanel topPanel=new JPanel();
	
		JPanel direction=new JPanel();
		direction.setBorder(BorderFactory.createTitledBorder("方向"));
		direction.add(up);
		direction.add(down);
		
		JPanel replacePanel=new JPanel();
		replacePanel.setLayout(new GridLayout(2,1));
		replacePanel.add(replace);
		replacePanel.add(replaceAll);

		topPanel.add(searchContentLabel);
		topPanel.add(findText);
		topPanel.add(searchNext);
		centerPanel.add(replaceContentLabel);
		centerPanel.add(replaceText);
		centerPanel.add(replacePanel);
		bottomPanel.add(matchcase);
		bottomPanel.add(direction);
		bottomPanel.add(cancel);

		con.add(topPanel);
		con.add(centerPanel);
		con.add(bottomPanel);
		
		//设置"查找与替换"对话框的大小、可更改大小(否)、位置和可见性
		findDialog.setSize(480,210);
		findDialog.setResizable(false);
		findDialog.setLocation(220,280);//设置弹出对话框的位置
		findDialog.setVisible(true);
		
	}			/*方法mySearch()结束*/	
	
	//实现ActionListener的事件处理方法public void actionPerformed(ActionEvent e)
	public void actionPerformed(ActionEvent e)
	{
		//新建
		if(e.getSource()==fileMenu_New)
			{
				editArea.requestFocus();
				String currentValue=editArea.getText();
				boolean isTextChange=(currentValue.equals(oldValue))?false:true;
				
				if(isTextChange){
					
					int saveChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存。是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
					
					if(saveChoose==JOptionPane.YES_OPTION)
					{
						String str=null;
							
						JFileChooser fileChooser=new JFileChooser();
						fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
						fileChooser.setApproveButtonText("确定");
						fileChooser.setDialogTitle("另存为");
						
						int result=fileChooser.showSaveDialog(this);
						
						if(result==JFileChooser.CANCEL_OPTION){
							return;
						}
						
						File saveFileName=fileChooser.getSelectedFile();
					
						if(saveFileName==null||saveFileName.getName().equals(""))
							JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
						else {
								try{
									FileWriter fw=new FileWriter(saveFileName);
									BufferedWriter bfw=new BufferedWriter(fw);
									bfw.write(editArea.getText(),0,editArea.getText().length());
									bfw.flush();
									fw.close();
									
									isNewFile=false;
									currentFile=saveFileName;
									oldValue=editArea.getText();
									
									this.setTitle(saveFileName.getName()+"  - 记事本");
									
								}
								
								catch(IOException ioException){					
								}				
						}				
					}
								
					else if(saveChoose==JOptionPane.NO_OPTION){
						editArea.replaceRange("", 0, editArea.getText().length()); 
						
						this.setTitle("无标题 - 记事本");
						isNewFile=true;
									
						undo.discardAllEdits(); 
						editMenu_Undo.setEnabled(false);
						popupMenu_Undo.setEnabled(false);
                        oldValue=editArea.getText();
					}
					
					else if(saveChoose==JOptionPane.CANCEL_OPTION)
					{
						return;
					}
				}
				
				else
				{
					editArea.replaceRange("", 0, editArea.getText().length()); 

					this.setTitle("无标题 - 记事本");
					isNewFile=true;
		
					undo.discardAllEdits();	//撤消"撤消"操作						
					editMenu_Undo.setEnabled(false); 
					popupMenu_Undo.setEnabled(false);
				    oldValue=editArea.getText();	
				}
			}//新建处理结束
			
		//打开
		else if(e.getSource()==fileMenu_Open)
			{	
				editArea.requestFocus();
				String currentValue=editArea.getText();
				boolean isTextChange=(currentValue.equals(oldValue))?false:true;
				
				if(isTextChange){
					
					int saveChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存。是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
					
					if(saveChoose==JOptionPane.YES_OPTION)
					{			
						String str=null;
						
						JFileChooser fileChooser=new JFileChooser();
						fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
						fileChooser.setApproveButtonText("确定");
						fileChooser.setDialogTitle("另存为");
						
						int result=fileChooser.showSaveDialog(this);
						
						if(result==JFileChooser.CANCEL_OPTION){
	
							return;
						}					
			
						File saveFileName=fileChooser.getSelectedFile();
					
						if(saveFileName==null||saveFileName.getName().equals(""))
							JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
						else {
								try{
									FileWriter fw=new FileWriter(saveFileName);
									BufferedWriter bfw=new BufferedWriter(fw);
									bfw.write(editArea.getText(),0,editArea.getText().length());
									bfw.flush();
									fw.close();
									
									isNewFile=false;
									currentFile=saveFileName;
									oldValue=editArea.getText();
									
									this.setTitle(saveFileName.getName()+"  - 记事本");
									
								}
								catch(IOException ioException){					
								}				
						}
					}
					else if(saveChoose==JOptionPane.NO_OPTION){
						String str=null;
							
						JFileChooser fileChooser=new JFileChooser();
						fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
						fileChooser.setApproveButtonText("确定");
						fileChooser.setDialogTitle("打开文件");
						
						int result=fileChooser.showOpenDialog(this);
						
						if(result==JFileChooser.CANCEL_OPTION){
							
							return;
						}
							
						File fileName=fileChooser.getSelectedFile();
						
						if(fileName==null||fileName.getName().equals(""))
							JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
						else {
						
							try{
								FileReader fr=new FileReader(fileName);
								BufferedReader bfr=new BufferedReader(fr);
								
								editArea.setText("");
								
								while((str=bfr.readLine())!=null){//每次读取一行,直到文件结束
									editArea.append(str+"\15\12");
								}
								
								this.setTitle(fileName.getName()+"  - 记事本");
						
											
								fr.close();
								isNewFile=false;
								currentFile=fileName;
								oldValue=editArea.getText();
							}							
							catch(IOException ioException){
							}
						}
					}
					else{
							return;
					}
				}
					
				else{
					String str=null;
					
					JFileChooser fileChooser=new JFileChooser();
					fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
					fileChooser.setApproveButtonText("确定");
					fileChooser.setDialogTitle("打开文件");
					
					int result=fileChooser.showOpenDialog(this);
					
					if(result==JFileChooser.CANCEL_OPTION){
		
						return;
					}
						
					File fileName=fileChooser.getSelectedFile();
				
					if(fileName==null||fileName.getName().equals(""))
						JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
					else {						
						try{
							FileReader fr=new FileReader(fileName);
							BufferedReader bfr=new BufferedReader(fr);
							
							editArea.setText("");
							
							while((str=bfr.readLine())!=null){//每次读取一行,直到文件结束
								editArea.append(str+"\15\12");
							}
								
							this.setTitle(fileName.getName()+"  - 记事本");
							
										
							fr.close();
							isNewFile=false;
							currentFile=fileName;
							oldValue=editArea.getText();
						}							
						catch(IOException ioException){
						}
					}
				
				}
			}//"打开"处理结束
		
		//保存	
		else if(e.getSource()==fileMenu_Save)
			{
				editArea.requestFocus();
				if(isNewFile){
				
					String str=null;
					
					JFileChooser fileChooser=new JFileChooser();
					fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
					fileChooser.setApproveButtonText("确定");
					fileChooser.setDialogTitle("另存为");
					
					int result=fileChooser.showSaveDialog(this);
					
					if(result==JFileChooser.CANCEL_OPTION){

						return;
					}					
	
					File saveFileName=fileChooser.getSelectedFile();
				
					if(saveFileName==null||saveFileName.getName().equals(""))
						JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
					else {
						try{
							FileWriter fw=new FileWriter(saveFileName);
							BufferedWriter bfw=new BufferedWriter(fw);
							bfw.write(editArea.getText(),0,editArea.getText().length());
							bfw.flush();
							fw.close();
							
							isNewFile=false;
							currentFile=saveFileName;
							oldValue=editArea.getText();
							
							this.setTitle(saveFileName.getName()+"  - 记事本");
							
						}							
						catch(IOException ioException){					
						}				
					}
				}
				else
				{
					try{
						FileWriter fw=new FileWriter(currentFile);
						BufferedWriter bfw=new BufferedWriter(fw);
						bfw.write(editArea.getText(),0,editArea.getText().length());
						bfw.flush();
						fw.close();
					}							
					catch(IOException ioException){					
					}
				}
			}//"保存"处理结束
		
		//另存为	
		else if(e.getSource()==fileMenu_SaveAs)
			{
				editArea.requestFocus();
				String str=null;
				
				JFileChooser fileChooser=new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				fileChooser.setApproveButtonText("确定");
				fileChooser.setDialogTitle("另存为");
				
				int result=fileChooser.showSaveDialog(this);
				
				if(result==JFileChooser.CANCEL_OPTION){
					
					return;
				}				

				File saveFileName=fileChooser.getSelectedFile();
			
				if(saveFileName==null||saveFileName.getName().equals(""))
					JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
				else {
					try{
						FileWriter fw=new FileWriter(saveFileName);
						BufferedWriter bfw=new BufferedWriter(fw);
						bfw.write(editArea.getText(),0,editArea.getText().length());
						bfw.flush();
						fw.close();
						oldValue=editArea.getText();
						
						this.setTitle(saveFileName.getName()+"  - 记事本");
						

⌨️ 快捷键说明

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