//查找与替换功能public void mySearch(){final JDialog findDialog=new JDialog(this,&"查找与替换&",false);//false时允许其他窗口同时处于激活状态(即无模式) Container con=findDialog.getContentPane();//返回此对话框的contentPane对象con.setLayout(new FlowLayout(FlowLayout.LEFT));JLabel searchContentLabel=new JLabel(&"查找内容(N) :&");JLabel replaceContentLabel=new JLabel(&"替换为(P) :&");final JTextField findText=new JTextField(30);final JTextField replaceText=new JTextField(30);final JCheckBox matchcase =new JCheckBox(&"区分大小写(C)&");ButtonGroup bGroup=new ButtonGroup();final JRadioButton up=new JRadioButton(&"向上(U)&");final JRadioButton down=new JRadioButton(&"向下(D)&");down.setSelected(true);bGroup.add(up);bGroup.add(down);JButton searchNext=new JButton(&"查找下一个(F)&");JButton replace=new JButton(&"替换(R)&");final JButton replaceAll=new JButton(&"全部替换(A)&");/*ButtonGroup此类用于为一组按钮创建一个多斥(multiple-exclusion)作用域。 使用相同的 ButtonGroup 对象创建一组按钮意味着“开启”其中一个按钮时,将关闭组中的其他所有按钮。*/ /*JRadioButton此类实现一个单选按钮,此按钮项可被选择或取消选择,并可为用户显示其状态。 与 ButtonGroup 对象配合使用可创建一组按钮,一次只能选择其中的一个按钮。 (创建一个 ButtonGroup 对象并用其 add 方法将 JRadioButton 对象包含在此组中。)*/ //&"替换&"按钮的事件处理replace.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){if(replaceText.getText().length() == 0 editArea.getSelectedText()!=null)editArea.replaceSelection(&"&");if(replaceText.getText().length() > 0 editArea.getSelectedText()!= null)editArea.replaceSelection(replaceText.getText());}});//&"替换全部&"按钮的事件处理replaceAll.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){editArea.setCaretPosition(0); //将光标放到编辑区开头 int a=0,b=0,replaceCount=0;if(findText.getText().length()==0){JOptionPane.showMessageDialog(findDialog,&"请填写查找内容!&",&"提示&",JOptionPane.WARNING_MESSAGE);findText.requestFocus(true);return;}while( a > -1) {int FindStartPos=editArea.getCaretPosition();String str1, str2, str3, str4, strA, strB;str1 = editArea.getText();str2 = str1.toLowerCase();str3 = findText.getText();str4 = str3.toLowerCase();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 {if(replaceCount==0){JOptionPane.showMessageDialog(findDialog, &"找不到您查找的内容!&", &"记事本&",JOptionPane.INFORMATION_MESSAGE);}else{JOptionPane.showMessageDialog(findDialog,&"成功替换&"+replaceCount+&"个匹配内容!&",&"替换成功&",JOptionPane.INFORMATION_MESSAGE);}}if(replaceText.getText().length() == 0 editArea.getSelectedText() != null){editArea.replaceSelection(&"&");replaceCount++;}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(530,270);findDialog.setResizable(false);findDialog.setLocation(230,280);findDialog.setVisible(true); } //实现ActionListener的事件处理方法public void actionPerformed(ActionEvent e)public void actionPerformed(ActionEvent e){//新建if(e.getSource()==fileMenu_New||e.getSource()==newButton){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;ldValue=editArea.getText();this.setTitle(saveFileName.getName()+&" - 记事本&");statusLabel.setText(&" 当前打开文件:&"+saveFileName.getAbsoluteFile());}catch(IOException ioException){ } } }else if(saveChoose==JOptionPane.NO_OPTION){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);ldValue=editArea.getText();}else if(saveChoose==JOptionPane.CANCEL_OPTION){return;}}else{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);ldValue=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;ldValue=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(&"确定&");fi代写php课程设计、Haskell程序帮做、代写Java实验、Java作业代写、帮做R编程leChooser.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&");}//end whilethis.setTitle(fileName.getName()+&" - 记事本&");statusLabel.setText(&" 当前打开文件:&"+fileName.getAbsoluteFile());fr.close();isNewFile=false;currentFile=fileName;ldValue=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&");}//end whilethis.setTitle(fileName.getName()+&" - 记事本&");statusLabel.setText(&" 当前打开文件:&"+fileName.getAbsoluteFile());fr.close();isNewFile=false;currentFile=fileName;ldValue=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;ldValue=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();ldValue=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 kButton=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;}if(gt = totalLine) {if(gt 欢迎访问CSDN博客&"+&"&"+&"https://blog.csdn.net/&");JEditorPane editPane=new JEditorPane(&"text/html&",helpTopicsStr);editPane.setEditable(false);editPane.addHyperlinkListener(new HyperlinkListener(){public void hyperlinkUpdate(HyperlinkEvent hle){if (hle.getEventType()==HyperlinkEvent.EventType.ACTIVATED){try{ Process process = Runtime.getRuntime().exec(&"cmd.exe /c start http://blog.csdn.net/&"); }catch(Exception e){e.printStackTrace();}}}});JFrame. helpTopicsFrame=new JFrame(&"帮助主题&");helpTopicsFrame.setContentPane(new JScrollPane(editPane));helpTopicsFrame.setSize(250,200);helpTopicsFrame.setLocation(300,300);helpTopicsFrame.setVisible(true);} & 转自:http://ass.3daixie.com/2018061616110207.html