歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java 記事本代碼

Java 記事本代碼

日期:2017/3/1 11:08:22   编辑:Linux編程

源文件、文檔說明、可運行的jar文件下載地址

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /pub/2011/11/05/Java 記事本代碼/

  1. /**
  2. * NoteBook
  3. *
  4. * @versionliujunguang
  5. * @version 1.00 09/10/25
  6. */
  7. public class NoteBook {
  8. public static void main(String[] args) {
  9. // Create application frame.
  10. NoteBookFrame frame = new NoteBookFrame();
  11. // Show frame
  12. frame.setVisible(true);
  13. }
  14. }

  1. import java.awt.*;
  2. /**
  3. * NoteBook-OpenClass.
  4. *
  5. * @liujunguang
  6. * @version 1.00 09/10/28
  7. */
  8. class SeeClass extends Frame
  9. {
  10. NoteBookFrame notebookframe = null;
  11. SeeClass(NoteBookFrame p)
  12. {
  13. notebookframe = p;
  14. }
  15. public void Modo()
  16. {
  17. System.out.println("狀態欄");
  18. }
  19. }
  20. //編輯類中的查找選項的實現
  21. import javax.swing.*;
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import java.lang.*;//String 類
  25. import javax.swing.JOptionPane;
  26. class SearchFrame extends JFrame implements ActionListener,ItemListener//單選按鈕事件接口
  27. {
  28. JLabel label1,label2,label3;
  29. JButton but1,but2,but3,but4;
  30. JTextField textfield,text;
  31. JRadioButton radiobutton1,radiobutton2;
  32. ButtonGroup group1;
  33. JCheckBox box1;
  34. NoteBookFrame notebookframe = null;
  35. String sub1="",sub2="";
  36. int n=-1;
  37. boolean isup = false;//判斷是否是最後一個找到的內容時用到
  38. boolean direction = true ;//判斷查找方向 向下為 true
  39. SearchFrame(NoteBookFrame p)
  40. {
  41. notebookframe = p;
  42. label1 = new JLabel("查找內容(N):");
  43. label2 = new JLabel("方向");
  44. label3 = new JLabel("替換為(P)");
  45. but1 = new JButton("查找下一個(F)");
  46. but2 = new JButton(" 取 消 ");
  47. but3 = new JButton(" 替 換 ");
  48. but4 = new JButton(" 轉 到 ");
  49. textfield = new JTextField(15);
  50. text = new JTextField(15);
  51. radiobutton1 = new JRadioButton("向下(U)",true);
  52. radiobutton2 = new JRadioButton("向上(D)");
  53. group1 = new ButtonGroup();
  54. //創建組件
  55. box1 = new JCheckBox("區分大小寫(C)");
  56. Container con = getContentPane();
  57. con.setLayout(null);//設置布局為空
  58. group1.add(radiobutton1);
  59. group1.add(radiobutton2);//將單選按鈕加到按鈕組中
  60. con.add(label1);//查找
  61. label1.setBounds(10,10,85,25);
  62. con.add(label3);//替換為
  63. label3.setBounds(10,45,85,25);
  64. con.add(textfield);//文本
  65. textfield.setBounds(90,10,180,25);
  66. con.add(text);//文本
  67. text.setBounds(90,45,180,25);
  68. con.add(but1);//查找下一個
  69. but1.setBounds(280,10,120,25);
  70. con.add(label2);//方向
  71. label2.setBounds(135,80,50,25);
  72. con.add(box1);
  73. box1.setBounds(10,115,120,25);
  74. con.add(radiobutton1);//向下
  75. radiobutton1.setBounds(130,115,70,25);
  76. con.add(radiobutton2);//向上
  77. radiobutton2.setBounds(200,115,70,25);
  78. con.add(but2);//取消
  79. but2.setBounds(280,115,120,25);
  80. con.add(but3);//替換
  81. but3.setBounds(280,45,120,25);
  82. con.add(but4);//轉到
  83. but4.setBounds(280,80,120,25);
  84. but1.addActionListener(this);
  85. but2.addActionListener(this);
  86. but3.addActionListener(this);
  87. but4.addActionListener(this);
  88. box1.addItemListener(this);
  89. textfield.addActionListener(this);
  90. text.addActionListener(this);
  91. radiobutton1.addItemListener(this);
  92. radiobutton2.addItemListener(this);
  93. //注冊監聽
  94. setBounds(500,300,420,180);
  95. setVisible(false);
  96. setTitle("查找");
  97. validate();
  98. }
  99. public void actionPerformed(ActionEvent e)
  100. {
  101. sub1 = notebookframe.textarea.getText();//得到文本區中的文本
  102. sub2 = textfield.getText();//得到文本框中的文本
  103. if(!isup) //如果不區分大小寫(默認不區分大小寫)
  104. {
  105. sub1 = sub1.toLowerCase();//將sub1轉換成小寫
  106. sub2 = sub2.toLowerCase();//將sub2轉換成小寫
  107. }
  108. if(!direction) n = sub1.lastIndexOf(sub2);//在文本區中查找文本框中的內容
  109. else n = sub1.indexOf(sub2);//在文本區中查找文本框中的內容
  110. if(e.getSource() == but1||e.getSource() == textfield)//查找下一個
  111. {
  112. if(n!=-1)
  113. {
  114. notebookframe.toFront();//如果此窗口是可見的,則將此窗口置於前端,並可以將其設為焦點 Window
  115. notebookframe.textarea.select(n,n+sub2.length());//選中查找的內容
  116. this.setVisible(false);
  117. }
  118. else
  119. {
  120. JOptionPane.showMessageDialog(this,"所指定的文本沒有找到!","記事本",JOptionPane.WARNING_MESSAGE);
  121. }
  122. }
  123. if(e.getSource() == but2)//取消
  124. {
  125. this.setVisible(false);
  126. }
  127. if(e.getSource() == but4)//轉到
  128. {
  129. int i=0,j=1;
  130. char ch[];
  131. try{
  132. int raw = Integer.parseInt(textfield.getText());//得到文本框中的文本
  133. String s = notebookframe.textarea.getText();
  134. ch = new char[s.length()];
  135. s.getChars(0,s.length()-1,ch,0);
  136. while(j<raw)
  137. {
  138. i++;
  139. if(ch[i] == '/n')
  140. {j++;}
  141. if(i == s.length())break;
  142. }
  143. if(raw == 1) //轉到第一行
  144. notebookframe.textarea.setCaretPosition(0);
  145. else
  146. notebookframe.textarea.setCaretPosition(i+1);//轉到指定行
  147. this.setVisible(false);
  148. }
  149. catch(Exception a){
  150. JOptionPane.showMessageDialog(this,"你輸入的位置不對無法到達!","記事本",JOptionPane.WARNING_MESSAGE);
  151. }
  152. }
  153. if(e.getSource() == but3)//替換
  154. {
  155. if(n != -1)
  156. {
  157. String sub3 = text.getText();
  158. notebookframe.textarea.select(n,n+sub2.length());//選中查找的內容
  159. notebookframe.textarea.replaceRange(sub3,n,n+sub2.length());//替換選中位置的文本
  160. sub1 = notebookframe.textarea.getText();//得到文本區中的文本
  161. sub2 = textfield.getText();//得到文本框中的文本
  162. if(!isup) //如果不區分大小寫(默認不區分大小寫)
  163. {
  164. sub1 = sub1.toLowerCase();//將sub1轉換成小寫
  165. sub2 = sub2.toLowerCase();//將sub2轉換成小寫
  166. }
  167. if(!direction) n = sub1.lastIndexOf(sub2);//在文本區中查找最後出現的文本框中的內容
  168. else n = sub1.indexOf(sub2);//在文本區中查找最先出現的文本框中的內容
  169. notebookframe.toFront();//如果此窗口是可見的,則將此窗口置於前端,並可以將其設為焦點 Window
  170. if(n!=-1 )
  171. {
  172. notebookframe.textarea.select(n,n+sub2.length());//選中查找內容
  173. }
  174. else
  175. JOptionPane.showMessageDialog(this,"所指定的文本沒有找到!","記事本",JOptionPane.WARNING_MESSAGE);
  176. }
  177. else
  178. {
  179. JOptionPane.showMessageDialog(this,"所指定的文本沒有找到無法替換!","記事本",JOptionPane.WARNING_MESSAGE);
  180. }
  181. }
  182. }
  183. public void nextShear()//查找下一個 菜單項對應的
  184. {
  185. if(n!=-1)
  186. {
  187. sub1 = notebookframe.textarea.getText().substring(n+sub2.length());//得到文本區的子串
  188. if(!isup) //如果不區分大小寫(默認不區分大小寫)
  189. {
  190. sub1 = sub1.toLowerCase();//將sub1轉換成小寫
  191. sub2 = sub2.toLowerCase();//將sub2轉換成小寫
  192. }
  193. if(sub1.indexOf(sub2)!=-1)
  194. {
  195. n = n+sub2.length()+sub1.indexOf(sub2);//得到查找內容在文本區中的位置
  196. notebookframe.textarea.select(n,n+sub2.length());//選中查找內容
  197. }
  198. else
  199. JOptionPane.showMessageDialog(this,"所指定的文本沒有找到!","記事本",JOptionPane.WARNING_MESSAGE);
  200. }
  201. else
  202. JOptionPane.showMessageDialog(this,"所指定的文本沒有找到!","記事本",JOptionPane.WARNING_MESSAGE);
  203. }
  204. public void itemStateChanged(ItemEvent ee)
  205. {
  206. if(ee.getSource() == box1)
  207. {
  208. if(box1.isSelected())
  209. {
  210. isup = true ;
  211. }
  212. else
  213. isup = false ;
  214. }
  215. if(ee.getSource() == radiobutton1)
  216. {
  217. if(radiobutton1.isSelected())
  218. {
  219. direction = true ;
  220. }
  221. }
  222. if(ee.getSource() == radiobutton2)
  223. {
  224. if(radiobutton2.isSelected())
  225. {
  226. direction = false;
  227. }
  228. }
  229. }
  230. }
Copyright © Linux教程網 All Rights Reserved