📄 notepad.java
字号:
}); //创建"查找与替换"对话框的界面 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(410,210); findDialog.setResizable(false); findDialog.setLocation(230,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){ 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){ editArea.replaceRange("", 0, editArea.getText().length()); statusLabel.setText(" 新建文件"); this.setTitle("无标题 - 记事本"); isNewFile=true;// undo.discardAllEdits(); //撤消所有的"撤消"和"重做"操作 popupMenu_Undo.setEnabled(false); popupMenu_Redo.setEnabled(false); oldValue=editArea.getText(); } else if(saveChoose==JOptionPane.CANCEL_OPTION) { return; } } else { editArea.replaceRange("", 0, editArea.getText().length()); statusLabel.setText(" 新建文件"); this.setTitle("无标题 - 记事本"); isNewFile=true;// undo.discardAllEdits(); popupMenu_Undo.setEnabled(false); popupMenu_Redo.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){ 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) { 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) { 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_Exit){ 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{ System.exit(0); } } //剪切 else if(e.getSource()==editMenu_Cut||e.getSource()==popupMenu_Cut) { 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) { editArea.requestFocus(); String text=editArea.getSelectedText(); StringSelection selection=new StringSelection(text); clipBoard.setContents(selection,null);// }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -