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

📄 notepad.java

📁 汉诺塔自动演示,可以由机器自动演示和人工自由进行实验.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
				{
					editArea.replaceRange("", 0, editArea.getText().length()); 
					statusLabel.setText(" 新建文件");
					this.setTitle("无标题 - 记事本");
					isNewFile=true;
					undo.discardAllEdits(); 
					editMenu_Undo.setEnabled(false);
					popupMenu_Undo.setEnabled(false);
					undoButton.setEnabled(false);
					editMenu_Redo.setEnabled(false); 
					popupMenu_Redo.setEnabled(false); 
					redoButton.setEnabled(false);
					oldValue=editArea.getText();	
				}
			}//新建处理结束
			
		//打开
		else if(e.getSource()==fileMenu_Open||e.getSource()==openButton)
			{	
				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){
							statusLabel.setText("您没有选择任何文件");
							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()+"  - 记事本");
									statusLabel.setText("当前打开文件:"+saveFileName.getAbsoluteFile());
								}
								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){
							statusLabel.setText("您没有选择任何文件");
							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");
								}//endwhile
								
								this.setTitle(fileName.getName()+"  - 记事本");
								statusLabel.setText(" 当前打开文件:"+fileName.getAbsoluteFile());
											
								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){
						statusLabel.setText(" 您没有选择任何文件");
						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");
							}//endwhile
								
							this.setTitle(fileName.getName()+"  - 记事本");
							statusLabel.setText(" 当前打开文件:"+fileName.getAbsoluteFile());
										
							fr.close();
							isNewFile=false;
							currentFile=fileName;
							oldValue=editArea.getText();
						}							
						catch(IOException ioException){
						}
					}
				
				}
			}//"打开"处理结束
		
		//保存	
		else if(e.getSource()==fileMenu_Save||e.getSource()==saveButton)
			{
				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){
						statusLabel.setText(" 您没有选择任何文件");
						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()+"  - 记事本");
							statusLabel.setText(" 当前打开文件:"+saveFileName.getAbsoluteFile());
						}							
						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||e.getSource()==saveAsButton)
			{
				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){
					statusLabel.setText(" 您没有选择任何文件");
					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()+"  - 记事本");
						statusLabel.setText(" 当前打开文件:"+saveFileName.getAbsoluteFile());
					}						
					catch(IOException ioException){					
					}				
				}
			
			}//"另存为"处理结束
		
		//页面设置
		else if(e.getSource()==fileMenu_PageSetup){	
			editArea.requestFocus();		
			JOptionPane.showMessageDialog(this,"对不起,此功能尚未添加!","提示",JOptionPane.WARNING_MESSAGE);
		}
		
		//打印
		else if(e.getSource()==fileMenu_Print||e.getSource()==printButton){
			editArea.requestFocus();
			JOptionPane.showMessageDialog(this,"对不起,此功能尚未添加!","提示",JOptionPane.WARNING_MESSAGE);
		}
		
		//退出
		else if(e.getSource()==fileMenu_Exit){
			int exitChoose=JOptionPane.showConfirmDialog(this,"确定要退出么?","退出提示",JOptionPane.OK_CANCEL_OPTION);
			if(exitChoose==JOptionPane.OK_OPTION){
				System.exit(0);
			}
			else{			
				return;
			}
		}
		
		//撤消
		else if(e.getSource()==editMenu_Undo||e.getSource()==popupMenu_Undo||e.getSource()==undoButton)
			{
				editArea.requestFocus();
				if(undo.canUndo()) { 
					try { 
						undo.undo(); 
						editMenu_Redo.setEnabled(true); 
						popupMenu_Redo.setEnabled(true);
						redoButton.setEnabled(true);
					}
					catch(CannotUndoException ex) { 
						System.out.println("Unable to undo: " + ex); 
						ex.printStackTrace(); 
					} 
					
					if(!undo.canUndo()){
						editMenu_Undo.setEnabled(false);
						popupMenu_Undo.setEnabled(false);
						undoButton.setEnabled(false);
						
						editMenu_Redo.setEnabled(true); 
						popupMenu_Redo.setEnabled(true); 
						redoButton.setEnabled(true);
					}	
				}
			}
		
		//重做
		else if(e.getSource()==editMenu_Redo||e.getSource()==popupMenu_Redo||e.getSource()==redoButton)
			{
				editArea.requestFocus();
				if(undo.canRedo()) { 
					try { 
						undo.redo();
						editMenu_Undo.setEnabled(true); 
						popupMenu_Undo.setEnabled(true);
						undoButton.setEnabled(true); 
					}
					catch(CannotRedoException ex) { 
						System.out.println("Unable to redo: " + ex); 
						ex.printStackTrace(); 
					} 
					
					if(!undo.canRedo()){
						editMenu_Redo.setEnabled(false);
						popupMenu_Redo.setEnabled(false); 
						redoButton.setEnabled(false);
						
						editMenu_Undo.setEnabled(true);
						popupMenu_Undo.setEnabled(true);
						undoButton.setEnabled(true);
					}	
				}	
			}
		
		//剪切
		else if(e.getSource()==editMenu_Cut||e.getSource()==popupMenu_Cut||e.getSource()==cutButton)
			{
				editArea.requestFocus();
				String text=editArea.getSelectedText();
       			StringSelection selection=new StringSelection(text);
       			clipBoard.setContents(selection,null);
       			editArea.replaceRange("",editArea.getSelectionStart(),editArea.getSelectionEnd());
       			checkMenuItemEnabled();	//设置剪切、复制、粘贴、删除等功能的可用性
			}
		
		//复制
		else if(e.getSource()==editMenu_Copy||e.getSource()==popupMenu_Copy||e.getSource()==copyButton)
			{
				editArea.requestFocus();
			    String text=editArea.getSelectedText();
       			StringSelection selection=new StringSelection(text);
       			clipBoard.setContents(selection,null);
       			checkMenuItemEnabled();	//设置剪切、复制、粘贴、删除等功能的可用性
			}
		
		//粘贴	
		else if(e.getSource()==editMenu_Paste||e.getSource()==popupMenu_Paste||e.getSource()==pasteButton)
			{
				editArea.requestFocus();
				Transferable contents=clipBoard.getContents(this);
		        if(contents==null) return;
		        String text;
		        text="";
		        
		        try{
		            text=(String)contents.getTransferData(DataFlavor.stringFlavor);
		        }
		        catch(Exception exception){
		        }
		        
		       	editArea.replaceRange(text,editArea.getSelectionStart(),editArea.getSelectionEnd());
		       	checkMenuItemEnabled();	//设置剪切、复制、粘贴、删除等功能的可用性
			}
		
		//删除
		else if(e.getSource()==editMenu_Delete||e.getSource()==popupMenu_Delete||e.getSource()==deleteButton)
			{
				editArea.requestFocus();
				editArea.replaceRange("",editArea.getSelectionStart(),editArea.getSelectionEnd());
				checkMenuItemEnabled();	//设置剪切、复制、粘贴、删除等功能的可用性
			}
		
		//查找
		else if(e.getSource()==editMenu_Find||e.getSource()==searchButton)
			{
				editArea.requestFocus();
				if(e.getSource()==searchButton){
					editArea.requestFocus();
					editArea.setCaretPosition(0);
				}
				mySearch();
			}
		
		//查找下一个(此功能尚未能很好实现,所以就先用查找功能来代替)	
		else if(e.getSource()==editMenu_FindNext)
			{
				mySearch();
			}
			
		//替换(与查找功能集成在一起了)
		else if(e.getSource()==editMenu_Replace)
		{
			mySearch();
		}
		
		//转到	
		else if(e.getSource()==editMenu_GoTo)
			{
				final JDialog gotoDialog=new JDialog(this,"转到下列行");
				JLabel gotoLabel=new JLabel("行数(L):");
				final JTextField linenum=new JTextField(20);
				linenum.setText("1");
				linenum.selectAll();
				
				JButton okButton=new JButton("确定");
				okButton.addActionListener(new ActionListener(){
					
					public void actionPerformed(ActionEvent e){
						int totalLine = editArea.getLineCount(); 
						int[] lineNumber = new int[totalLine + 1]; 
						String s = editArea.getText(); 
						int pos = 0, t = 0; 
						
						while(true) { 
							pos = s.indexOf('\12', pos); 
							if(pos == -1) 
							break; 
							lineNumber[t++] = pos++; 
						} 
						
						int gt = 1; 
						try { 
							gt = Integer.parseInt(linenum.getText()); 
						}
						catch(NumberFormatException efe) { 
							JOptionPane.showMessageDialog(null, "请输入行数!", "提示",JOptionPane.WARNING_MESSAGE); 
							linenum.requestFocus(true);
							return;
						} 

⌨️ 快捷键说明

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