基本信息
源码名称:基于JAVA的文本编辑器/记事本(含课程设计报告)
源码大小:1.21M
文件格式:.zip
开发语言:Java
更新时间:2019-07-16
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
附件包括报告,java源代码。工程文件夹名字叫NotePad,导入选择这个文件夹就可以运行整个文件,编译器是MyEclipse。
这是一个入门级示例,适合新手
import Dialog.AboutDialog; import Dialog.MyFontDialog; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.Calendar; import javax.swing.JFrame; public class NotePad extends JFrame implements ActionListener{ private String m_strDirectory=null; private String m_strFile=null; boolean isselect=false; boolean isChange = false; boolean isupselect=false; boolean wasChange = false; String btext=null; boolean btupdown=false; Font wasFont=new Font("宋体",Font.PLAIN,24); String wasText=null; String isText=null; JMenuBar menuBar = new JMenuBar(); static Dimension screen=Toolkit.getDefaultToolkit().getScreenSize(); JOptionPane helpAbout=new JOptionPane(); FileDialog Opentext=new FileDialog(this,"文件打开",FileDialog.LOAD); FileDialog Savetext=new FileDialog(this,"文件保存",FileDialog.SAVE); JPopupMenu fileAreaPopupMenu=new JPopupMenu(); JMenuItem fileAreaPopupMenuUndo = new JMenuItem("撒消(U)"); JMenuItem fileAreaPopupMenuCut = new JMenuItem("剪切(E)"); JMenuItem fileAreaPopupMenuCopy = new JMenuItem("复制(C)"); JMenuItem fileAreaPopupMenuPaste = new JMenuItem("粘贴(P)"); JMenuItem fileAreaPopupMenuDelete = new JMenuItem("删除(L)"); JMenuItem fileAreaPopupMenuSelectAll=new JMenuItem("全选(A)"); JMenu menuFile = new JMenu("文件(F)"); JMenuItem menuFileNew = new JMenuItem("新建(N) "); JMenuItem menuFileOpen = new JMenuItem("打开(O) "); JMenuItem menuFileSave = new JMenuItem("保存(S) "); JMenuItem menuFileSaveAs = new JMenuItem("另存为(A) "); JMenuItem menuFileExit = new JMenuItem("退出(X) "); JMenu menuEdit = new JMenu("编辑(E)"); JMenuItem menuEditundo = new JMenuItem("撒消(U)"); JMenuItem menuEditCut = new JMenuItem("剪切(T)"); JMenuItem menuEditCopy = new JMenuItem("复制(C)"); JMenuItem menuEditPaste = new JMenuItem("粘贴(P)"); JMenuItem menuEditDelete = new JMenuItem("删除(L)"); JMenuItem menuEditFind = new JMenuItem("查找(F)"); JMenuItem menuEditFindNext = new JMenuItem("查找下一个(N)"); JMenuItem menuEditReplace = new JMenuItem("替换(R)"); JMenuItem menuEditSelectAll=new JMenuItem("全选(A)"); JMenuItem menuEditTimeDate=new JMenuItem("时间/日期(D)"); JMenu menuFormat =new JMenu("格式(O)"); JCheckBoxMenuItem menuFormatLine=new JCheckBoxMenuItem("自行换行(W)",false); JMenuItem menuFormatfont=new JMenuItem("字体(F)"); JMenu menuHelp =new JMenu("帮助(H)"); JMenuItem menuHelpFind=new JMenuItem("查看帮助(H)"); JMenuItem menuHelpAbout=new JMenuItem("关于记事本(A)"); Container panel =getContentPane(); JTextArea fileArea = new JTextArea(); JScrollPane gdt=new JScrollPane(fileArea); public NotePad() { this.setTitle("记事本"); Toolkit tool = this.getToolkit();// 窗口图标! Image myimage = tool.getImage(""); this.setIconImage(myimage); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { exit(); } }); fileArea.getDocument().addDocumentListener(new FileAeraListener()); fileArea.setFont(wasFont); //---------------------------菜单栏------------------------------// menuBar.add(menuFile); menuFile.setMnemonic('F'); menuBar.add(menuEdit); menuEdit.setMnemonic('E'); menuBar.add(menuFormat); menuFormat.setMnemonic('O'); menuBar.add(menuHelp); menuHelp.setMnemonic('H'); //-----------------------------文件-------------------------------// menuFile.add(menuFileNew); //新建 menuFileNew.setMnemonic('N'); menuFile.add(menuFileOpen); //打开 menuFileOpen.setMnemonic('O');// 访问健; menuFileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));// 快捷健; menuFile.add(menuFileSave); //保存 menuFileSaveAs.setMnemonic('A'); menuFile.add(menuFileSaveAs); //另保存 menuFileSave.setMnemonic('S'); menuFileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); menuFile.addSeparator(); menuFile.add(menuFileExit); //退出 menuFileExit.setMnemonic('X'); //-----------------------编辑--------------------------// menuEdit.add(menuEditundo); //撤销 menuEditundo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK)); menuEditundo.setMnemonic('U'); menuEdit.addSeparator(); menuEdit.add(menuEditCut); //剪切 menuEditCut.setMnemonic('T'); menuEditCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); menuEditCopy.setMnemonic('C'); menuEdit.add(menuEditCopy); //复制 menuEditCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); menuEdit.add(menuEditPaste); //粘贴 menuEditPaste.setMnemonic('P'); menuEditPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); menuEditDelete.setMnemonic('L'); menuEdit.add(menuEditDelete); //删除 menuEditDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK)); menuEdit.addSeparator(); menuEditFind.setMnemonic('F'); menuEdit.add(menuEditFind); //查找 menuEditFind.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK)); menuEditFindNext.setMnemonic('N'); menuEdit.add(menuEditFindNext); //查找下一个 menuEditFindNext.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, Event.ALT_MASK)); menuEditReplace.setMnemonic('R'); menuEdit.add(menuEditReplace); //替换 menuEditReplace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK)); menuEdit.addSeparator(); menuEditSelectAll.setMnemonic('A'); menuEdit.add(menuEditSelectAll); //全选 menuEditSelectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); menuEdit.add(menuEditTimeDate); //时间/日期 menuEditTimeDate.setMnemonic('D'); menuEditTimeDate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, Event.ALT_MASK)); //--------------格式------------------------------------// menuFormatLine.setMnemonic('W'); menuFormat.add(menuFormatLine); //自动换行 menuFormatfont.setMnemonic('F'); menuFormat.add(menuFormatfont); //字体 //--------------帮助-----------------------------------// menuHelpFind.setMnemonic('H'); menuHelp.add(menuHelpFind); //帮助查找 menuHelpAbout.setMnemonic('A'); menuHelp.add(menuHelpAbout); //关于 //--------------文本区弹出菜单-------------------------// fileArea.setComponentPopupMenu(fileAreaPopupMenu); fileArea.add(fileAreaPopupMenu); fileAreaPopupMenu.add(fileAreaPopupMenuUndo); fileAreaPopupMenu.addSeparator(); fileAreaPopupMenu.add(fileAreaPopupMenuCut); fileAreaPopupMenu.add(fileAreaPopupMenuCopy); fileAreaPopupMenu.add(fileAreaPopupMenuPaste); fileAreaPopupMenu.add(fileAreaPopupMenuDelete); fileAreaPopupMenu.addSeparator(); fileAreaPopupMenu.add(fileAreaPopupMenuSelectAll); //---------------增加各种控件的消息响应------------------// menuFileNew.addActionListener(this); menuFileOpen.addActionListener(this); menuFileSave.addActionListener(this); menuFileSaveAs.addActionListener(this); menuFileExit.addActionListener(this); menuEditundo.addActionListener(this); menuEditCopy.addActionListener(this); menuEditCut.addActionListener(this); menuEditSelectAll.addActionListener(this); menuEditDelete.addActionListener(this); menuEditFind.addActionListener(this); // menuEditFindNext.addActionListener(this); menuEditReplace.addActionListener(this); menuEditPaste.addActionListener(this); menuEditTimeDate.addActionListener(this); menuFormatLine.addActionListener(this); menuFormatfont.addActionListener(this); menuHelpFind.addActionListener(this); menuHelpAbout.addActionListener(this); fileAreaPopupMenuUndo.addActionListener(this); fileAreaPopupMenuCut.addActionListener(this); fileAreaPopupMenuCopy.addActionListener(this); fileAreaPopupMenuPaste.addActionListener(this); fileAreaPopupMenuDelete.addActionListener(this); fileAreaPopupMenuSelectAll.addActionListener(this); menuEditFindNext.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(btext==null) Search(); else{ int a = 0, b = 0; int FindStartPos = fileArea.getCaretPosition(); String str1, str2, str3, str4, strA, strB; str1 = fileArea.getText(); str2 = str1.toLowerCase(); str3 = btext; str4 = str3.toLowerCase(); //"区分大小写"的CheckBox被选中 if (isselect) //不区分大小写 { strA = str1; strB = str3; } else //区分大小写 { strA = str2; strB = str4; } if (isupselect) //向上搜索 { if (fileArea.getSelectedText() == null) { a = strA.lastIndexOf(strB, FindStartPos - 1); } else { a = strA.lastIndexOf(strB,fileArea.getSelectionStart()-strB.length()); // FindStartPos-findText.getText().length()-1 } } else if (!isupselect) { System.out.println("kkkkkkkk"); if (fileArea.getSelectedText() == null) { a = strA.indexOf(strB, FindStartPos ); } else { System.out.println("ffffff"); a = strA.indexOf(strB, fileArea.getSelectionEnd() ); //- findText.getText().length() 1 } } System.out.println(a); if (a > -1) { if (isupselect) { fileArea.setCaretPosition(a); b = btext.length(); fileArea.select(a, a b); } else { fileArea.setCaretPosition(a); b = btext.length(); fileArea.select(a, a b); } } else { JOptionPane.showMessageDialog(null, "找不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE); } } } }); setSize(770, 520); this.setLocation(300,150); panel.add(gdt,BorderLayout.CENTER); setJMenuBar(menuBar); wasText=fileArea.getText(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource()==menuFileNew){ //新建 startNewDocument(); } else if (e.getSource()==menuFileOpen){ //打开 Open(); } else if (e.getSource()==menuFileSave){ //保存 save(); } else if (e.getSource()==menuFileSaveAs){ //另存为 saveAs(); } else if(e.getSource()==menuFileExit){ //退出 exit(); } else if(e.getSource()==menuEditundo||e.getSource()==fileAreaPopupMenuUndo){ //撒消 undo(); } else if(e.getSource()==menuEditCut||e.getSource()==fileAreaPopupMenuCut){ //剪切 fileArea.cut(); } else if(e.getSource()==menuEditCopy||e.getSource()==fileAreaPopupMenuCopy){ //复制 fileArea.copy(); } else if(e.getSource()==menuEditPaste||e.getSource()==fileAreaPopupMenuPaste){ //粘贴 fileArea.paste(); } else if(e.getSource()==menuEditDelete||e.getSource()==fileAreaPopupMenuDelete){ //删除 Delete(); } else if(e.getSource()==menuEditSelectAll||e.getSource()==fileAreaPopupMenuSelectAll){ //选择全部 selectAll(); } else if(e.getSource()==menuEditFind){ //查找 Search(); } else if(e.getSource()==menuEditReplace){ //替换 Replace(); } else if(e.getSource()==menuEditTimeDate) { //时间/日期 setTimeDate(); } else if (e.getSource()==menuFormatLine){ //查找 if(menuFormatLine.isSelected()){ fileArea.setLineWrap(true); }else{ fileArea.setLineWrap(false); } } else if(e.getSource()==menuHelpFind){ //查看帮助 try { // String filePath = "C://Windows//winhlp32.exe"; String filePath = "C://Windows//winhlp32.exe"; Runtime.getRuntime().exec("cmd.exe /c " filePath); } catch(IOException ee) { JOptionPane.showMessageDialog(this,"打开系统的记事本帮助文件出错!","错误信息",JOptionPane.INFORMATION_MESSAGE); }catch(SecurityException ee){ ee.printStackTrace(); }catch(NullPointerException ee){ ee.printStackTrace(); }catch(IllegalArgumentException ee){ ee.printStackTrace(); } } else if(e.getSource()==menuHelpAbout){ //关于记事本 About(); }else if(e.getSource()==menuFormatfont){ //字体 setfont(); } } private class FileAeraListener implements DocumentListener{ public void insertUpdate(DocumentEvent e) { wasChange = isChange; isChange = true; isText=fileArea.getText(); } public void removeUpdate(DocumentEvent e) { wasChange = isChange; isChange = true; isText=fileArea.getText(); } public void changedUpdate(DocumentEvent e) { wasChange = isChange; isChange = true; isText=fileArea.getText(); } } public void startNewDocument() { if(isChange){ int decision = JOptionPane.showConfirmDialog(this, "文件已更改 !\n" "是否选择保存?", "注意", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (decision == JOptionPane.YES_OPTION){ saveDocument();fileArea.setText("");isChange = false;} else if (decision == JOptionPane.NO_OPTION) fileArea.setText(""); else if (decision == JOptionPane.CANCEL_OPTION); } else{ fileArea.setText(""); } } public void Open(){ if(isChange){ int decision = JOptionPane.showConfirmDialog(this, "文件已更改 !\n" "是否选择保存?", "注意", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (decision == JOptionPane.YES_OPTION){ saveDocument();isChange = false;} else if (decision == JOptionPane.NO_OPTION){openDocument();} else if (decision == JOptionPane.CANCEL_OPTION); } else openDocument(); } public void openDocument() { Opentext.setVisible(true); String Stext=null; try { if (Opentext.getFile() != null) { m_strDirectory =Opentext.getDirectory(); m_strFile=Opentext.getFile(); File f = new File(m_strDirectory m_strFile); System.out.println(m_strDirectory m_strFile); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String s = br.readLine(); while (s != null) { if(Stext==null)Stext=s "\n"; else Stext = Stext s "\n"; s = br.readLine(); } br.close(); fileArea.setText(Stext); isChange = false; } } catch (FileNotFoundException e1) { System.out.println("File not found: "); } catch (IOException e2) { e2.printStackTrace(); } } public void saveDocument() { Savetext.setVisible(true); BufferedWriter dataOut=null; try { if(Savetext.getFile()!=null) { String save=Savetext.getFile(); String save2=null; if(save.length()>3) { save2=save.substring(save.length()-4); } else{ save2=save; } if(save2.equals(".txt")) { m_strDirectory=Savetext.getDirectory(); m_strFile=Savetext.getFile(); dataOut=new BufferedWriter(new FileWriter(m_strDirectory m_strFile)); } else { m_strDirectory=Savetext.getDirectory(); m_strFile=Savetext.getFile() ".txt"; dataOut=new BufferedWriter(new FileWriter(m_strDirectory m_strFile)); } dataOut.write(fileArea.getText()); dataOut.close(); } } catch (IOException e) { System.out.println("File not found: "); } } public void save(){ if(m_strFile!=null&&m_strDirectory!=null) { try { BufferedWriter dataout=new BufferedWriter(new FileWriter(m_strDirectory m_strFile)); dataout.write(fileArea.getText()); dataout.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else saveDocument(); } public void saveAs() { saveDocument(); } public void Delete() { fileArea.replaceRange("", fileArea.getSelectionStart(), fileArea.getSelectionEnd()); } public void setTimeDate() { Calendar c1 =Calendar.getInstance(); int y = c1.get(Calendar.YEAR); int m = c1.get(Calendar.MONTH); int d = c1.get(Calendar.DATE); int h = c1.get(Calendar.HOUR); int m1 = c1.get(Calendar.MINUTE); int m2 = m 1; fileArea.setText(fileArea.getText() h ":" m1 " " y "/" m2 "/" d); } public void setfont(){ JFrame notepad; notepad=new JFrame("Notepad"); MyFontDialog font=new MyFontDialog(notepad,screen.width/2-140,screen.height/2-100); font.DialogShow(); if(font.getFont()!=null) { fileArea.setFont(font.getFont()); } } public void Replace(){ final JDialog findDialog = new JDialog(this, "查找与替换", false); Container con = findDialog.getContentPane(); 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)"); JButton searchNext = new JButton("查找下一个(F)"); JButton replace = new JButton("替换(R)"); final JButton replaceAll = new JButton("全部替换(A)"); JButton cancel = new JButton("取消"); JLabel Direction=new JLabel("方向"); searchNext.setFont(new Font("宋体",Font.BOLD,10)); Down.setSelected(true); //默认向下搜索 bGroup.add(Up); bGroup.add(Down); //"替换"按钮的事件处理 replace.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (replaceText.getText().length() == 0 && fileArea.getSelectedText() != null) fileArea.replaceSelection(""); if (replaceText.getText().length() > 0 && fileArea.getSelectedText() != null) fileArea.replaceSelection(replaceText.getText()); } }); //"替换全部"按钮的事件处理 replaceAll.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fileArea.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 = fileArea.getCaretPosition();//获取光标位置 String str1, str2, str3, str4, strA, strB; str1 = fileArea.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 (fileArea.getSelectedText() == null) { a = strA.lastIndexOf(strB, FindStartPos - 1); } else { a = strA.lastIndexOf(strB, FindStartPos- findText.getText().length() - 1); } } else //向下搜索 { if (fileArea.getSelectedText() == null) { a = strA.indexOf(strB, FindStartPos); } else { a = strA.indexOf(strB, FindStartPos- findText.getText().length() 1); } } if (a > -1) { if (Up.isSelected()) { fileArea.setCaretPosition(a); b = findText.getText().length(); fileArea.select(a, a b); } else if (Down.isSelected()) { fileArea.setCaretPosition(a); b = findText.getText().length(); fileArea.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&& fileArea.getSelectedText() != null) //用空字符代替选定内容 { fileArea.replaceSelection(""); replaceCount ; } if (replaceText.getText().length() > 0&& fileArea.getSelectedText() != null) //用指定字符代替选定内容 { fileArea.replaceSelection(replaceText.getText()); replaceCount ; } }//end while } }); //"查找下一个"按钮事件处理 searchNext.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int a = 0, b = 0; int FindStartPos = fileArea.getCaretPosition(); String str1, str2, str3, str4, strA, strB; str1 = fileArea.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 (fileArea.getSelectedText() == null) { a = strA.lastIndexOf(strB, FindStartPos - 1); } else { a = strA.lastIndexOf(strB, FindStartPos - findText.getText().length() - 1); } } else if (Down.isSelected()) { if (fileArea.getSelectedText() == null) { a = strA.indexOf(strB, FindStartPos); } else { a = strA.indexOf(strB, FindStartPos - findText.getText().length() 1); } } if (a > -1) { if (Up.isSelected()) { fileArea.setCaretPosition(a); b = findText.getText().length(); fileArea.select(a, a b); } else if (Down.isSelected()) { fileArea.setCaretPosition(a); b = findText.getText().length(); fileArea.select(a, a b); } } else { JOptionPane.showMessageDialog(null, "找不到您查找的内容!", "记事本", JOptionPane.INFORMATION_MESSAGE); } } }); //"取消"按钮及事件处理 cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { findDialog.dispose(); } }); //创建"查找与替换"对话框的界面 findDialog.setSize(390, 160); findDialog.setResizable(false); searchNext.setFocusable(false); replace.setFocusable(false); replaceAll.setFocusable(false); matchcase.setFocusable(false); cancel.setFocusable(false); Up.setFocusable(false); Down.setFocusable(false); bGroup=new ButtonGroup(); findDialog.setLayout(null); searchContentLabel.setBounds(10,12,80,22); replaceContentLabel.setBounds(10,42,80,22); findText.setBounds(95, 12, 160, 22); replaceText.setBounds(95, 42, 160, 22); searchNext.setBounds(265, 12, 108, 22); replace.setBounds(265, 42, 108, 22); replaceAll.setBounds(265, 72, 108, 22); cancel.setBounds(265, 102, 108, 22); Direction.setBounds(10, 72, 80, 22); matchcase.setBounds(6, 102, 98, 22); Down.setBounds(95, 72, 80, 22); Up.setBounds(175, 72, 80, 22); bGroup.add(Up); bGroup.add(Down); findDialog.add(searchContentLabel); findDialog.add(matchcase); findDialog.add(findText); findDialog.add(searchNext); findDialog.add(Direction); findDialog.add(replaceContentLabel); findDialog.add(replaceText); findDialog.add(replace); findDialog.add(replaceAll); findDialog.add(cancel); findDialog.add(Down); findDialog.add(Up); findDialog.setLocation(500, 300); findDialog.setVisible(true); } public void Search(){ final JDialog findDialog = new JDialog(this, "查找下一个", false); Container con = findDialog.getContentPane(); con.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel searchContentLabel = new JLabel(" 查找内容(N) :"); final JTextField findText = new JTextField(17); 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)"); //"查找下一个"按钮事件处理 Up.setMnemonic('U'); Down.setMnemonic('D'); searchNext.setMnemonic('F'); searchNext.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int a = 0, b = 0; int FindStartPos = fileArea.getCaretPosition(); String str1, str2, str3, str4, strA, strB; str1 = fileArea.getText(); str2 = str1.toLowerCase(); str3 = findText.getText(); btext=str3; str4 = str3.toLowerCase(); //"区分大小写"的CheckBox被选中 if (matchcase.isSelected()) //不区分大小写 { isselect=true; strA = str1; strB = str3; } else //区分大小写 { isselect=false; strA = str2; strB = str4; } if (Up.isSelected()) //向上搜索 { isupselect=true; if (fileArea.getSelectedText() == null) { a = strA.lastIndexOf(strB, FindStartPos - 1); } else { a = strA.lastIndexOf(strB,fileArea.getSelectionStart()-findText.getText().length()); // FindStartPos-findText.getText().length()-1 } } else if (Down.isSelected()) { isupselect=false; if (fileArea.getSelectedText() == null) { a = strA.indexOf(strB, FindStartPos ); } else { a = strA.indexOf(strB, fileArea.getSelectionEnd() ); //- findText.getText().length() 1 } } if (a > -1) { if (Up.isSelected()) { fileArea.setCaretPosition(a); b = findText.getText().length(); fileArea.select(a, a b); } else if (Down.isSelected()) { fileArea.setCaretPosition(a); b = findText.getText().length(); fileArea.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); topPanel.add(searchContentLabel); topPanel.add(findText); topPanel.add(searchNext); bottomPanel.add(matchcase); bottomPanel.add(direction); bottomPanel.add(cancel); con.add(topPanel); con.add(centerPanel); con.add(bottomPanel); //设置"替换"对话框的大小、可更改大小(否)、位置和可见性 findDialog.setSize(425, 200); findDialog.setResizable(true); findDialog.setLocation(430, 280); findDialog.setVisible(true); } public void undo(){ String tempText=null; tempText=isText; fileArea.setText(wasText); fileArea.selectAll(); wasText=tempText; } public void selectAll() { fileArea.selectAll(); } public void fileExit_actionPerformed(ActionEvent e) { System.exit(0); } public void About(){ JFrame notepad; notepad=new JFrame("Notepad"); AboutDialog about=new AboutDialog(notepad,screen.width/2-140,screen.height/2-100); about.Dialog.setVisible(true); } public void exit() { if (isChange == false) System.exit(1); else { int decision = JOptionPane.showConfirmDialog(this, "文件已更改 !\n" "是否选择保存?", "注意", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (decision == JOptionPane.YES_OPTION) saveDocument(); else if (decision == JOptionPane.NO_OPTION) System.exit(1); else if (decision == JOptionPane.CANCEL_OPTION); } } public static void main(String arg[]) { NotePad note = new NotePad(); note.show(); } }