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

📄 一个记事本的源代码.htm

📁 一个用Java编写的记事本源程序
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<BR>//menuEdit.add(menuEditRedo); <BR>menuEdit.addSeparator(); 
<BR>menuEdit.add(menuEditCut); <BR>menuEdit.add(menuEditCopy); 
<BR>menuEdit.add(menuEditPaste); <BR>menuEdit.add(menuEditDelete); 
<BR>menuEditDelete.add(menuEditDeleteSelection); 
<BR>menuEditDelete.add(menuEditDeleteLine); 
<BR>menuEditDelete.add(menuEditDeleteWord); <BR>menuEdit.addSeparator(); 
<BR>menuEdit.add(menuEditFind); <BR>menuEdit.add(menuEditReplace); 
<BR>menuEdit.add(menuEditGoTo); <BR>menuEdit.addSeparator(); 
<BR>menuEdit.add(menuEditSelectAll); <BR>mb.add(menuView); 
<BR>menuView.add(menuViewFont); <BR>menuView.add(menuViewColor); 
<BR>menuView.addSeparator(); <BR>menuView.add(menuViewClassic); 
<BR>menuView.add(menuViewStatus); <BR>menuView.add(menuViewWordWrap); 
<BR>menuView.addSeparator(); <BR>menuView.add(menuViewDoubleSpace); 
<BR>mb.add(menuHelp); <BR>menuHelp.add(menuHelpAbout); 
<BR>fr.addWindowListener(new WindowAdapter() { <BR>public void 
windowClosing(WindowEvent e) { <BR>System.exit(0); <BR>} <BR>}); 
<BR>fr.setSize(600,420); <BR>fr.setVisible(true); <BR>} <BR><BR>//ActionListener 
<BR>public void actionPerformed(ActionEvent ae) { <BR>if(ae.getSource() == 
menuFileNew) { <BR>ta.replaceRange("", 0, ta.getText().length()); <BR>fns = 
null; <BR>fileStatus.setText("New File"); <BR>undo.discardAllEdits(); 
<BR>menuEditUndo.setEnabled(false); <BR>} <BR>else if(ae.getSource() == 
menuFileOpen) { <BR>String s = null; <BR>StringBuffer strPool = new 
StringBuffer(); <BR>Frame openFileFrame = new Frame("Open file"); <BR>FileDialog 
fileDialog = new FileDialog(openFileFrame); 
<BR>fileDialog.setMode(FileDialog.LOAD); <BR>fileDialog.setFile("*.txt;*.java"); 
<BR>fileDialog.show(); <BR>String file = fileDialog.getFile(); <BR>String 
directory = fileDialog.getDirectory(); <BR>if(file != null) { <BR>fns = 
directory + file; <BR>ta.replaceRange("", 0, ta.getText().length()); 
<BR>BufferedReader br; <BR>try{ <BR>br = new BufferedReader(new 
FileReader(fns)); <BR>s = br.readLine(); <BR>while(s != null) { 
<BR>strPool.append(s + &acute;\15&acute; + "\12");//+0D+0A <BR>s = br.readLine(); <BR>} 
<BR>br.close(); <BR>ta.setText(strPool.toString()); <BR>} catch(IOException e) { 
<BR>} <BR>fileStatus.setText("file opened."); <BR>isNewFile = true; 
<BR>undo.discardAllEdits(); <BR>menuEditUndo.setEnabled(false); <BR>} <BR>} 
<BR>else if(ae.getSource() == menuFileClose) { <BR>ta.replaceRange("", 0, 
ta.getText().length()); <BR>fileStatus.setText("file closed without save"); 
<BR>undo.discardAllEdits(); <BR>menuEditUndo.setEnabled(false); <BR>fns = null; 
<BR>} <BR>else if(ae.getSource() == menuFilePrint) { 
<BR>JOptionPane.showMessageDialog(null, "TODO... Only the FIRST page now.", 
"Print", 1); <BR>ta.printIt(ta.getText(), ta.getFont()); <BR>//todo... <BR>} 
<BR>//else if(ae.getSource() == menuFilePrintSetup) { 
<BR>//ta.printItSetup(ta.getText(), ta.getFont()); <BR>//todo... <BR>//} 
<BR>else if(ae.getSource() == menuFileSave) { <BR>OutputStreamWriter osw; 
<BR>if(fns != null) { <BR>try{ <BR>osw = new OutputStreamWriter( <BR>new 
BufferedOutputStream( <BR>new FileOutputStream(fns))); 
<BR>osw.write(ta.getText(), 0, ta.getText().length()); <BR>osw.close(); 
<BR>fileStatus.setText("file saved"); <BR>} catch(IOException e) { <BR>} 
<BR>if(fileStatus.getText().endsWith("*")) { 
<BR>fileStatus.setText(fileStatus.getText().substring(0, 
<BR>fileStatus.getText().length() - 1)); <BR>} <BR>} <BR>else { <BR>Frame 
saveFileFrame = new Frame("Save file"); <BR>FileDialog fileDialog = new 
FileDialog(saveFileFrame); <BR>fileDialog.setMode(FileDialog.SAVE); 
<BR>fileDialog.setFile("*.txt;*.java"); <BR>fileDialog.show(); <BR>String file = 
fileDialog.getFile(); <BR>String directory = fileDialog.getDirectory(); 
<BR>if(file != null) { <BR>fns = directory + file; <BR>try{ <BR>osw = new 
OutputStreamWriter( <BR>new BufferedOutputStream( <BR>new 
FileOutputStream(fns))); <BR>osw.write(ta.getText(), 0, ta.getText().length()); 
<BR>osw.close(); <BR>fileStatus.setText("file saved"); <BR>} catch(IOException 
e) { <BR>} <BR>} <BR>} <BR>} <BR>else if(ae.getSource() == menuFileSaveAs) { 
<BR>OutputStreamWriter osw; <BR>Frame saveFileFrame = new Frame("Save file"); 
<BR>FileDialog fileDialog = new FileDialog(saveFileFrame); 
<BR>fileDialog.setMode(FileDialog.SAVE); <BR>fileDialog.setFile("*.txt;*.java"); 
<BR>fileDialog.show(); <BR>String file = fileDialog.getFile(); <BR>String 
directory = fileDialog.getDirectory(); <BR>if(file != null) { <BR>fns = 
directory + file; <BR>try{ <BR>osw = new OutputStreamWriter( <BR>new 
BufferedOutputStream( <BR>new FileOutputStream(fns))); 
<BR>osw.write(ta.getText(), 0, ta.getText().length()); <BR>osw.close(); 
<BR>fileStatus.setText("file saved"); <BR>} catch(IOException e) { <BR><BR>} 
<BR>} <BR>else if(ae.getSource() == menuFileExit) { <BR>System.exit(0); <BR>} 
<BR>else if(ae.getSource() == menuEditUndo) { <BR>if(undo.canUndo()) { <BR>try { 
<BR>undo.undo(); <BR>} catch(CannotUndoException ex) { 
<BR>System.out.println("Unable to undo: " + ex); <BR>ex.printStackTrace(); <BR>} 
<BR>if(!undo.canUndo()) <BR>menuEditUndo.setEnabled(false); <BR>} <BR>//else 
<BR>// menuEditUndo.setEnabled(false); <BR>} <BR>else if(ae.getSource() == 
menuEditRedo) { <BR>//todo... <BR>} <BR>else if(ae.getSource() == menuEditCut) { 
<BR>ta.cut(); <BR>} <BR>else if(ae.getSource() == menuEditCopy) { <BR>ta.copy(); 
<BR>} <BR>else if(ae.getSource() == menuEditPaste) { <BR>ta.paste(); <BR>} 
<BR>else if(ae.getSource() == menuEditDeleteSelection) { <BR>ta.replaceRange("", 
ta.getSelectionStart(), ta.getSelectionEnd()); <BR>} <BR>else if(ae.getSource() 
== menuEditDeleteLine) { <BR>String str = ta.getText(); <BR>int pos = 
ta.getCaretPosition(); <BR>int lineStart = 0, lineEnd = 0; <BR>lineStart = 
str.substring(0, pos).lastIndexOf(&acute;\12&acute;); <BR>lineEnd = str.indexOf(&acute;\15&acute;, pos); 
<BR>lineStart = (lineStart == -1) ? 0 : (lineStart - 1); <BR>ta.replaceRange("", 
lineStart, lineEnd); <BR>lineStart = (lineStart == 0) ? 0 : (lineStart + 2); 
<BR>ta.setCaretPosition(lineStart); <BR>} <BR>else if(ae.getSource() == 
menuEditDeleteWord) { <BR>String str = ta.getText(); <BR>int pos = 
ta.getCaretPosition(); <BR>int wordStart = 0, wordEnd = 0; <BR>wordEnd = 
wordLocation(str, pos, true); <BR>if(wordEnd == pos) { <BR>wordStart = pos; 
<BR>wordEnd = pos + 1; <BR>ta.replaceRange("", wordStart, wordEnd); <BR>} 
<BR>else { <BR>wordStart = wordLocation(str, pos, false); <BR>if(wordStart == 
-1) <BR>ta.replaceRange("", 0, wordEnd); <BR>else <BR>ta.replaceRange("", 
wordStart, wordEnd); <BR>} <BR>} <BR>else if(ae.getSource() == menuEditFind) { 
<BR>JDialog ds = new JDialog(this, "Find", true); 
<BR>ds.getContentPane().setLayout(new FlowLayout()); <BR>ds.setResizable(false); 
<BR>tfs = new TextField(15); <BR>ds.getContentPane().add(tfs); <BR>bs = new 
Button("Find"); <BR>bs.addActionListener(this); <BR>Button bsc = new 
Button("Cancel"); <BR>bsc.addActionListener(new ActionListener() { <BR>public 
void actionPerformed(ActionEvent bOke) { <BR>dispose(); <BR>foundCount = 0; 
<BR>} <BR>}); <BR>ds.getContentPane().add(bs); <BR>ds.getContentPane().add(bsc); 
<BR>JLabel dsMessage1 = new JLabel("Found counts: "); <BR>dsMessage2 = new 
JLabel(" 0"); <BR>dsLoop = new Checkbox("Loop"); 
<BR>dsLoop.setState(findingLoop); <BR>dsMatchCase = new Checkbox("Match Case"); 
<BR>ds.getContentPane().add(dsLoop); <BR>ds.getContentPane().add(dsMatchCase); 
<BR>ds.getContentPane().add(dsMessage1); 
<BR>ds.getContentPane().add(dsMessage2); <BR>ds.setLocation(120, 120); 
<BR>ds.addWindowListener(new WindowAdapter() { <BR>public void 
windowClosing(WindowEvent e) { <BR>dispose(); <BR>FindStartPos = 0; <BR>} 
<BR>}); <BR>ds.setSize(250,110); <BR>ds.setVisible(true); <BR>} <BR>else 
if(ae.getSource() == bs) { <BR>int a = 0, b = 0; <BR>String str1, str2, str3, 
str4, strA, strB; <BR>str1 = ta.getText(); <BR>str2 = str1.toLowerCase(); 
<BR>str3 = tfs.getText(); <BR>str4 = str3.toLowerCase(); 
<BR>if(dsMatchCase.getState()) { <BR>strA = str1; <BR>strB = str3; <BR>} 
<BR>else { <BR>strA = str2; <BR>strB = str4; <BR>} <BR>a = strA.indexOf(strB, 
FindStartPos); <BR>if(a &gt; -1) { <BR>ta.setCaretPosition(a); <BR>b = 
tfs.getText().length(); <BR>ta.select(a, a + b); <BR>FindStartPos = a + b; 
<BR>foundCount++; <BR>dsMessage2.setText(foundCount+""); <BR>} <BR>else { 
<BR>if(dsLoop.getState()) { <BR>JOptionPane.showMessageDialog(null, "End of 
file.", "Find result",1); <BR>FindStartPos = 0; <BR>} <BR>else { 
<BR>JOptionPane.showMessageDialog(null, "End of file.", "Find result",1); <BR>} 
<BR>foundCount = 0; <BR>} <BR>} <BR>else if(ae.getSource() == menuEditReplace) { 
<BR>JDialog dr = new JDialog(this, "Replace", true); 
<BR>dr.getContentPane().setLayout(new GridBagLayout()); <BR>GridBagConstraints 
gbc = new GridBagConstraints(); <BR>gbc.gridx = 0; <BR>gbc.gridy = 0; 
<BR>gbc.gridwidth = 2; <BR>gbc.gridheight =1; <BR>//gbc.fill = gbc.HORIZONTAL; 
gbc.anchor = gbc.CENTER; <BR>Panel p1 = new Panel(); 
<BR>dr.getContentPane().add(p1); <BR>//drMatchCase = new Checkbox("Match Case"); 
<BR>//gbc.gridx = 1; <BR>//gbc.gridy = 1; <BR>//gbc.gridwidth = 1; 
<BR>//gbc.gridheight =1; <BR>//dr.getContentPane().add(drMatchCase); 
<BR>gbc.gridx = 2; <BR>gbc.gridy = 0; <BR>gbc.gridwidth = 1; <BR>gbc.gridheight 
=1; <BR>//gbc.fill = gbc.NONE; gbc.anchor = gbc.EAST; <BR>Panel p2 = new 
Panel(); <BR>p1.setLayout(new GridLayout(5, 1)); <BR>p2.setLayout(new 
GridLayout(4, 1)); <BR>dr.getContentPane().add(p2); <BR>JLabel drMessage1 = new 
JLabel("Replace: "); <BR>JLabel drMessage2 = new JLabel("With: "); 
<BR>drMatchCase = new Checkbox("Match Case"); <BR>tfro = new TextField(15); 
<BR>tfrn = new TextField(15); <BR>p1.add(drMessage1); <BR>p1.add(tfro); 
<BR>p1.add(drMessage2); <BR>p1.add(tfrn); <BR>p1.add(drMatchCase); <BR>brf = new 
Button("Find"); <BR>//brf.addActionListener(this); <BR>brf.addActionListener(new 
ActionListener() { <BR>public void actionPerformed(ActionEvent brfe) { <BR>int a 
= 0, b = 0; <BR>String str1, str2, str3, str4, strA, strB; <BR>str1 = 
ta.getText(); <BR>str2 = str1.toLowerCase(); <BR>str3 = tfro.getText(); <BR>str4 
= str3.toLowerCase(); <BR>if(drMatchCase.getState()) { <BR>strA = str1; <BR>strB 
= str3; <BR>} <BR>else { <BR>strA = str2; <BR>strB = str4; <BR>} <BR>a = 
strA.indexOf(strB, FindStartPos); <BR>if(a &gt; -1) { 
<BR>ta.setCaretPosition(a); <BR>b = tfro.getText().length(); <BR>ta.select(a, a 
+ b); <BR>FindStartPos = a + b; <BR>foundCount++; <BR>} <BR>else { 
<BR>JOptionPane.showMessageDialog(null, "End of file.", "Result",1); 
<BR>foundCount = 0; <BR>} <BR>} <BR>}); <BR>brr = new Button("Replace"); 
<BR>brr.addActionListener(new ActionListener() { <BR>public void 
actionPerformed(ActionEvent brre) { <BR>if(tfrn.getText().length() == 0 
&amp;&amp; ta.getSelectedText() != null) <BR>ta.replaceSelection(""); 
<BR>if(tfrn.getText().length() &gt; 0 &amp;&amp; ta.getSelectedText() != null) 
<BR>ta.replaceSelection(tfrn.getText()); <BR>int a = 0, b = 0; <BR>String str1, 
str2, str3, str4, strA, strB; <BR>str1 = ta.getText(); <BR>str2 = 
str1.toLowerCase(); <BR>str3 = tfro.getText(); <BR>str4 = str3.toLowerCase(); 
<BR>if(drMatchCase.getState()) { <BR>strA = str1; <BR>strB = str3; <BR>} 
<BR>else { <BR>strA = str2; <BR>strB = str4; <BR>} <BR>a = strA.indexOf(strB, 
FindStartPos); <BR>if(a &gt; -1) { <BR>ta.setCaretPosition(a); <BR>b = 
tfro.getText().length(); <BR>ta.select(a, a + b); <BR>FindStartPos = a + b; 
<BR>foundCount++; <BR>} <BR>else { <BR>JOptionPane.showMessageDialog(null, "End 
of file.", "Result",1); <BR>foundCount = 0; <BR>} <BR>} <BR>}); <BR>brra = new 
Button("Replace All"); <BR>brra.addActionListener(new ActionListener() { 
<BR>public void actionPerformed(ActionEvent brrae) { <BR>int a = 0; <BR>while( a 
&gt; -1) { <BR>int b = 0; <BR>String str1, str2, str3, str4, strA, strB; 
<BR>str1 = ta.getText(); <BR>str2 = str1.toLowerCase(); <BR>str3 = 
tfro.getText(); <BR>str4 = str3.toLowerCase(); <BR>if(drMatchCase.getState()) { 
<BR>strA = str1; <BR>strB = str3; <BR>} <BR>else { <BR>strA = str2; <BR>strB = 
str4; <BR>} <BR>a = strA.indexOf(strB, FindStartPos); <BR>if(a &gt; -1) { 
<BR>ta.setCaretPosition(a); <BR>b = tfro.getText().length(); <BR>ta.select(a, a 
+ b); <BR>FindStartPos = a + b; <BR>foundCount++; <BR>} <BR>else { 
<BR>JOptionPane.showMessageDialog(null, "End of file.", "Result",1); 
<BR>foundCount = 0; <BR>} <BR>if(tfrn.getText().length() == 0 &amp;&amp; 
ta.getSelectedText() != null) <BR>ta.replaceSelection(""); 
<BR>if(tfrn.getText().length() &gt; 0 &amp;&amp; ta.getSelectedText() != null) 
<BR>ta.replaceSelection(tfrn.getText()); <BR>} <BR>} <BR>}); <BR>Button brc = 
new Button("Cancel"); <BR>brc.addActionListener(new ActionListener() { 
<BR>public void actionPerformed(ActionEvent brce) { <BR>dispose(); <BR>} <BR>}); 
<BR>p2.add(brf); <BR>p2.add(brr); <BR>p2.add(brra); <BR>p2.add(brc); 
<BR>dr.setResizable(false); <BR>dr.setLocation(120, 120); 
<BR>dr.addWindowListener(new WindowAdapter() { <BR>public void 

⌨️ 快捷键说明

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