给大家分享下我之前的作品:

源文件下载:

  链接:https://pan.baidu.com/s/1N45VsS9aVgmvhvYjRLxBrA 
  提取码:b9fg 

源码:

  JF_Notpad.java实现主要面板,在二级菜单可以实现java模板建立,可以后期自己添加:

  

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import javax.swing.event.*; //导入菜单
  4. import javax.swing.filechooser.FileNameExtensionFilter;
  5. import java.util.Date; //获取系统的时间
  6. import java.text.SimpleDateFormat; //控制系统的时间格式
  7.  
  8. import java.awt.event.*;
  9. import java.io.*;
  10. import java.util.regex.*;
  11.  
  12. public class JF_Notpad extends JFrame implements ActionListener,WindowListener{
  13. //menuBar
  14. private JMenuBar mb;
  15. private JMenu file,edit,format,view,help;
  16.  
  17. //menuToolBar
  18. private JToolBar tool;
  19. private static int BUTTONWIDTH,BUTTONHEIGHT;
  20. private JButton create,unfold,copy,cut,paste,pasteShortcut,delete;
  21.  
  22. //File
  23. //JMenu下还可以有二级菜单
  24. private JMenu newNot;
  25. //JMenuItem不可以有其他菜单
  26. private JMenuItem notpad,model,open,save,saveAs,pageSet,print,exit;
  27.  
  28. //Edit
  29. private JMenuItem undoE,cutE,copyE,pasteE,deleteE,findE,findNextE,replaceE,goToE,selectAllE,timeDateE;
  30.  
  31. //Format
  32. private JMenuItem wordWrap,font;
  33.  
  34. //View
  35. private JMenuItem statusBar;
  36.  
  37. //Help
  38. private JMenuItem viewHelp,about;
  39.  
  40. //leftSpace
  41. private JPanel leftSpace;
  42.  
  43. //TextArea
  44. private JTextArea content;
  45. private JScrollPane scroll;
  46.  
  47. //openUrl,saveUrl
  48. private static String openUrl="E:/notpad.txt"; //保存之前打开文件的路径
  49. private static String saveUrl="E:/notpad.txt"; //保存之前保存文件的路径
  50. private boolean saved=false; //判断文件是否保存过
  51.  
  52. //contentFont 字体
  53. private Font contentFont;
  54.  
  55. //是否在打开保存面板后,还应该打开选择文件面板
  56. private boolean openFileBool=false;
  57.  
  58. public static void main(String[] args) {
  59. JF_Notpad not=new JF_Notpad();
  60. }
  61.  
  62. public static JButton changeIconSize(JButton button,String url,int width,int height,String tip){
  63. button.setBounds(0,0,width,height);
  64. ImageIcon buttonImg=new ImageIcon(url);
  65. //改变图片的大小
  66. Image temp=buttonImg.getImage().getScaledInstance(button.getWidth(), button.getHeight(), buttonImg.getImage().SCALE_DEFAULT);
  67. button=new JButton(new ImageIcon(temp));
  68. button.setToolTipText(tip); //提示
  69.  
  70. return button;
  71. }
  72.  
  73. public static Image changeImageSize(String url,int width,int height){
  74. ImageIcon Img=new ImageIcon(url);
  75. //改变图片的大小
  76. Image temp=Img.getImage().getScaledInstance(width, height, Img.getImage().SCALE_DEFAULT);
  77.  
  78. return temp;
  79. }
  80.  
  81. JF_Notpad(){
  82. //menubar
  83. mb=new JMenuBar();
  84. file=new JMenu("File(F)");
  85. file.setMnemonic('F');
  86. edit=new JMenu("Edit(E)");
  87. edit.setMnemonic('E');
  88. format=new JMenu("Format(O)");
  89. format.setMnemonic('O');
  90. view =new JMenu("View(V)");
  91. view.setMnemonic('V');
  92. help=new JMenu("Help(H)");
  93. help.setMnemonic('H');
  94.  
  95. //menuToolBar
  96. tool=new JToolBar();
  97. BUTTONWIDTH=22;
  98. BUTTONHEIGHT=22;
  99.  
  100. create=new JButton();
  101. create=JF_Notpad.changeIconSize(create, "img/notpad/news.png", BUTTONWIDTH, BUTTONWIDTH, "New");
  102. unfold=new JButton();
  103. unfold=JF_Notpad.changeIconSize(unfold, "img/notpad/open.png", BUTTONWIDTH, BUTTONWIDTH, "Open");
  104. copy=new JButton();
  105. copy=JF_Notpad.changeIconSize(copy, "img/notpad/copy.png", BUTTONWIDTH, BUTTONWIDTH, "Copy");
  106. cut=new JButton();
  107. cut=JF_Notpad.changeIconSize(cut, "img/notpad/cut.png", BUTTONWIDTH, BUTTONWIDTH, "Cut");
  108. paste=new JButton();
  109. paste=JF_Notpad.changeIconSize(unfold, "img/notpad/paste.png", BUTTONWIDTH, BUTTONWIDTH, "Paste");
  110. pasteShortcut=new JButton();
  111. pasteShortcut=JF_Notpad.changeIconSize(pasteShortcut, "img/notpad/pasteShortcut.png", BUTTONWIDTH, BUTTONWIDTH, "PasteShortcut");
  112. delete=new JButton();
  113. delete=JF_Notpad.changeIconSize(delete, "img/notpad/delete.png", BUTTONWIDTH, BUTTONWIDTH, "Delete");
  114.  
  115. //menuToolBarListener
  116. create.setActionCommand("notpad");
  117. create.addActionListener(this);
  118. unfold.setActionCommand("open");
  119. unfold.addActionListener(this);
  120. copy.setActionCommand("copyE");
  121. copy.addActionListener(this);
  122. cut.setActionCommand("cutE");
  123. cut.addActionListener(this);
  124. paste.setActionCommand("pasteE");
  125. paste.addActionListener(this);
  126. pasteShortcut.setActionCommand("pasteShortcut");
  127. pasteShortcut.addActionListener(this);
  128. delete.setActionCommand("deleteE");
  129. delete.addActionListener(this);
  130.  
  131. //file
  132. newNot=new JMenu("New(N)");
  133. newNot.setMnemonic('N');
  134. ImageIcon icon=new ImageIcon(this.changeImageSize("img/notpad/logo.png", BUTTONWIDTH-5,BUTTONHEIGHT-5 ));
  135. notpad=new JMenuItem("Notpad",icon);
  136. model=new JMenuItem("Model");
  137. icon=new ImageIcon(this.changeImageSize("img/notpad/open.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));
  138. open=new JMenuItem("Open",icon);
  139. icon=new ImageIcon(this.changeImageSize("img/notpad/save.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));
  140. save=new JMenuItem("Save",icon);
  141. icon=new ImageIcon(this.changeImageSize("img/notpad/saveAs.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));
  142. saveAs=new JMenuItem("Save As...",icon);
  143. pageSet=new JMenuItem("PageSet");
  144. print=new JMenuItem("Print");
  145. icon=new ImageIcon(this.changeImageSize("img/notpad/exit.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));
  146. exit=new JMenuItem("Exit",icon);
  147.  
  148. //FileListener
  149. notpad.setActionCommand("notpad");
  150. notpad.addActionListener(this);
  151. model.setActionCommand("model");
  152. model.addActionListener(this);
  153. open.setActionCommand("open");
  154. open.addActionListener(this);
  155. save.setActionCommand("save");
  156. save.addActionListener(this);
  157. saveAs.setActionCommand("saveAs");
  158. saveAs.addActionListener(this);
  159. pageSet.setActionCommand("pageSet");
  160. pageSet.addActionListener(this);
  161. print.setActionCommand("print");
  162. print.addActionListener(this);
  163. exit.setActionCommand("exit");
  164. exit.addActionListener(this);
  165.  
  166. //Edit
  167. undoE=new JMenuItem("Undo");
  168. icon=new ImageIcon(this.changeImageSize("img/notpad/cut.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));
  169. cutE=new JMenuItem("Cut",icon);
  170. icon=new ImageIcon(this.changeImageSize("img/notpad/copy.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));
  171. copyE=new JMenuItem("Copy",icon);
  172. icon=new ImageIcon(this.changeImageSize("img/notpad/paste.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));
  173. pasteE=new JMenuItem("Paste",icon);
  174. icon=new ImageIcon(this.changeImageSize("img/notpad/delete.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));
  175. deleteE=new JMenuItem("Delete",icon);
  176. findE=new JMenuItem("Find...");
  177. findNextE=new JMenuItem("Find Next");
  178. replaceE=new JMenuItem("Replace...");
  179. goToE=new JMenuItem("Go To..");
  180. selectAllE=new JMenuItem("Select All");
  181. timeDateE=new JMenuItem("Time/Data");
  182.  
  183. //EditListener
  184. undoE.setActionCommand("undoE");
  185. undoE.addActionListener(this);
  186. cutE.setActionCommand("cutE");
  187. cutE.addActionListener(this);
  188. copyE.setActionCommand("copyE");
  189. copyE.addActionListener(this);
  190. pasteE.setActionCommand("pasteE");
  191. pasteE.addActionListener(this);
  192. deleteE.setActionCommand("deleteE");
  193. deleteE.addActionListener(this);
  194. findE.setActionCommand("findE");
  195. findE.addActionListener(this);
  196. findNextE.setActionCommand("findNextE");
  197. findNextE.addActionListener(this);
  198. replaceE.setActionCommand("replaceE");
  199. replaceE.addActionListener(this);
  200. goToE.setActionCommand("goToE");
  201. goToE.addActionListener(this);
  202. selectAllE.setActionCommand("selectAllE");
  203. selectAllE.addActionListener(this);
  204. timeDateE.setActionCommand("timeDateE");
  205. timeDateE.addActionListener(this);
  206.  
  207. //Format
  208. icon=new ImageIcon(this.changeImageSize("img/notpad/wordWrap.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));
  209. wordWrap=new JMenuItem("Word Wrap",icon);
  210. icon=new ImageIcon(this.changeImageSize("img/notpad/font.png", BUTTONWIDTH-8, BUTTONHEIGHT-8));
  211. font=new JMenuItem("Font...",icon);
  212.  
  213. //FormatListener
  214. wordWrap.setActionCommand("wordWrap");
  215. wordWrap.addActionListener(this);
  216. font.setActionCommand("font");
  217. font.addActionListener(this);
  218.  
  219. //View
  220. statusBar=new JMenuItem("Status Bar");
  221.  
  222. //ViewListener
  223. statusBar.setActionCommand("statusBar");
  224. statusBar.addActionListener(this);
  225.  
  226. //Help
  227. viewHelp=new JMenuItem("View Help");
  228. icon=new ImageIcon(this.changeImageSize("img/notpad/about.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));
  229. about=new JMenuItem("About NotePad",icon);
  230.  
  231. //HelpListener
  232. viewHelp.setActionCommand("viewHelp");
  233. viewHelp.addActionListener(this);
  234. about.setActionCommand("about");
  235. about.addActionListener(this);
  236.  
  237. //leftSpace
  238. leftSpace=new JPanel();
  239.  
  240. //contentFont
  241. contentFont=new Font("KaiTi",Font.PLAIN,20);
  242.  
  243. //textArea
  244. content=new JTextArea();
  245. content.setFont(contentFont);
  246. scroll=new JScrollPane(content);
  247. //scroll.setHorizontalScrollBar(content);
  248.  
  249. //openUrl,saveUrl
  250. // openUrl="E:/notpad.txt";
  251. // saveUrl="E:/notpad.txt";
  252. // saved=false;
  253.  
  254. //saveFilePanel
  255. // Save saveF=new Save("Save",this);
  256.  
  257. //添加组件,从下一级开始装
  258. newNot.add(notpad); newNot.add(model);
  259. file.add(newNot); file.add(open); file.add(save); file.add(saveAs); file.addSeparator(); file.add(pageSet); file.add(print); file.addSeparator(); file.add(exit);
  260.  
  261. edit.add(undoE);
  262. edit.addSeparator();
  263. edit.add(cutE);
  264. edit.add(copyE);
  265. edit.add(pasteE);
  266. edit.add(deleteE);
  267. edit.addSeparator();
  268. edit.add(findE);
  269. edit.add(findNextE);
  270. edit.add(replaceE);
  271. edit.add(goToE);
  272. edit.addSeparator();
  273. edit.add(selectAllE);
  274. edit.add(timeDateE);
  275.  
  276. format.add(wordWrap);
  277. format.add(font);
  278.  
  279. view.add(statusBar);
  280.  
  281. help.add(viewHelp);
  282. help.addSeparator();
  283. help.add(about);
  284.  
  285. mb.add(file); mb.add(edit); mb.add(format); mb.add(view); mb.add(help);
  286.  
  287. tool.add(create);
  288. tool.add(unfold);
  289. tool.add(copy);
  290. tool.add(cut);
  291. tool.add(paste);
  292. tool.add(pasteShortcut);
  293. tool.add(delete);
  294.  
  295. this.setJMenuBar(mb);
  296. this.add(tool,BorderLayout.NORTH);
  297. this.add(leftSpace,BorderLayout.WEST);
  298. this.add(scroll);
  299.  
  300. this.setTitle("Notpad");
  301. this.setIconImage(new ImageIcon("img/notpad/logo.png").getImage());
  302. this.setLocation(300,300);
  303. this.setSize(400,400);
  304. this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
  305. this.setVisible(true);
  306.  
  307. }
  308.  
  309. private String origContent=null; //保存一开始的文件或者保存过的文件
  310. private String copyContent=null;
  311. @Override
  312. public void actionPerformed(ActionEvent e) {
  313. switch(e.getActionCommand()){
  314. case "notpad":{
  315. System.out.println("notpad");
  316.  
  317. this.isChangeFile("txt");
  318.  
  319. }break;
  320.  
  321. case "model":{
  322. System.out.println("model");
  323.  
  324. this.isChangeFile("java");
  325. }break;
  326.  
  327. case "open":{
  328. System.out.println("open");
  329. //防止第一次打开空文件时,还没保存
  330. if(this.origContent==null){
  331. this.saved=true;
  332. }
  333.  
  334. if(saved){
  335. this.openFilePanel();
  336. }else{
  337. Save saveF=new Save("Save",this);
  338. this.openFilePanel();
  339. }
  340.  
  341. }break;
  342.  
  343. case "save":{
  344. System.out.println("save");
  345. this.saveFile();
  346. //this.setSaved(true);
  347.  
  348. }break;
  349.  
  350. case "saveAs":{
  351. System.out.println("saveAs");
  352.  
  353. String str=content.getText();
  354. //选择保存文件的路径
  355. JFileChooser fileChoose=null;
  356. if(saveUrl==null){
  357. fileChoose=new JFileChooser();
  358. }else{
  359. fileChoose=new JFileChooser(saveUrl);
  360. }
  361. FileNameExtensionFilter filter=new FileNameExtensionFilter("txt & java","java","txt"); //设置可以识别的文件
  362. fileChoose.setFileFilter(filter);
  363. fileChoose.setDialogTitle("Save File");
  364. fileChoose.showSaveDialog(save);
  365. fileChoose.setVisible(true);
  366.  
  367. File outFile =fileChoose.getSelectedFile();
  368. saveUrl=fileChoose.getSelectedFile().getPath();
  369. PrintStream p=null;
  370. try{
  371. p=new PrintStream(outFile);
  372. System.setOut(p);
  373. System.out.print(str);
  374. }catch(Exception a){
  375. System.out.println("SaveAS File Error!!");
  376. }finally{
  377. p.close();
  378. }
  379. }break;
  380.  
  381. case "pageSet":{
  382. System.out.println("pageSet");
  383.  
  384. }break;
  385.  
  386. case "print":{
  387. System.out.println("print");
  388.  
  389. }break;
  390.  
  391. case "exit":{
  392. System.out.println("exit");
  393.  
  394. System.exit(-1);
  395. }break;
  396.  
  397. case "undoE":{
  398. System.out.println("undoE");
  399.  
  400. }break;
  401.  
  402. case "cutE":{
  403. System.out.println("cutE");
  404. if(this.content.getSelectedText()!=null){
  405. this.copyContent=this.content.getSelectedText();
  406. }
  407. this.content.replaceSelection("");
  408. }break;
  409.  
  410. case "copyE":{
  411. System.out.println("copyE");
  412. if(this.content.getSelectedText()!=null){
  413. this.copyContent=this.content.getSelectedText();
  414. }
  415.  
  416. }break;
  417.  
  418. case "pasteE":{
  419. System.out.println("pasteE");
  420. if(copyContent!=null){
  421. this.content.replaceRange("", content.getSelectionStart(), content.getSelectionEnd());
  422. }
  423. this.content.insert(copyContent, this.content.getSelectionStart());
  424. }break;
  425.  
  426. case "deleteE":{
  427. System.out.println("deleteE");
  428.  
  429. this.content.replaceSelection("");
  430. }break;
  431.  
  432. case "findE":{
  433. System.out.println("findE");
  434.  
  435. }break;
  436.  
  437. case "findNextE":{
  438. System.out.println("findNextE");
  439.  
  440. }break;
  441.  
  442. case "replaceE":{
  443. System.out.println("replaceE");
  444.  
  445. }break;
  446.  
  447. case "goToE":{
  448. System.out.println("goToE");
  449.  
  450. }break;
  451.  
  452. case "selectAllE":{
  453. System.out.println("selectAllE");
  454.  
  455. this.content.selectAll();
  456. }break;
  457.  
  458. case "timeDateE":{
  459. System.out.println("timeDateE");
  460.  
  461. SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  462. //System.out.println(df.format(new Date()));
  463. this.content.insert(df.format(new Date()), content.getSelectionStart());
  464. }break;
  465.  
  466. case "wordWrap":{
  467. System.out.println("wordWrap");
  468.  
  469. ImageIcon icon;
  470. if(this.content.getLineWrap()){
  471. this.content.setLineWrap(false);
  472. icon=new ImageIcon(this.changeImageSize("img/notpad/wordWrap.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));
  473. this.wordWrap.setIcon(icon);
  474. }else{
  475. this.content.setLineWrap(true);
  476. icon=new ImageIcon(this.changeImageSize("img/notpad/wordWrapNo.png", BUTTONWIDTH-5, BUTTONHEIGHT-5));
  477. this.wordWrap.setIcon(icon);
  478. }
  479.  
  480. }break;
  481.  
  482. case "font":{
  483. System.out.println("font");
  484.  
  485. FontPanel f=new FontPanel("Font",this);
  486.  
  487. // this.content.setFont(new Font(f.getFontN(),f.getFontSytleN(),f.getSizeN()));
  488.  
  489. }break;
  490.  
  491. case "statusBar":{
  492. System.out.println("statusBar");
  493.  
  494. }break;
  495.  
  496. case "viewHelp":{
  497. System.out.println("viewHelp");
  498.  
  499. }break;
  500.  
  501. case "about":{
  502. System.out.println("about");
  503.  
  504. About a=new About("About","1.0","feiquan","228332xxxxx@qq.com","xxx-xxxx-xxxx","https://www.baidu.com");
  505. }break;
  506.  
  507. case "pasteShortcut":{
  508. System.out.println("pasteShortcut");
  509.  
  510. }break;
  511.  
  512. }
  513.  
  514. }
  515.  
  516. @Override
  517. public void windowOpened(WindowEvent e) {
  518. // TODO Auto-generated method stub
  519.  
  520. }
  521.  
  522. @Override
  523. public void windowClosing(WindowEvent e) {
  524. // TODO Auto-generated method stub
  525. System.out .println("弹出是否选择保存的对话框");
  526. if(saved){
  527. Save saveF=new Save("Save",this);
  528. }
  529. }
  530.  
  531. @Override
  532. public void windowClosed(WindowEvent e) {
  533. // TODO Auto-generated method stub
  534.  
  535. }
  536.  
  537. @Override
  538. public void windowIconified(WindowEvent e) {
  539. // TODO Auto-generated method stub
  540.  
  541. }
  542.  
  543. @Override
  544. public void windowDeiconified(WindowEvent e) {
  545. // TODO Auto-generated method stub
  546.  
  547. }
  548.  
  549. @Override
  550. public void windowActivated(WindowEvent e) {
  551. // TODO Auto-generated method stub
  552.  
  553. }
  554.  
  555. @Override
  556. public void windowDeactivated(WindowEvent e) {
  557. // TODO Auto-generated method stub
  558.  
  559. }
  560.  
  561. public static String getOpenUrl() {
  562. return openUrl;
  563. }
  564.  
  565. public static void setOpenUrl(String openUrl) {
  566. JF_Notpad.openUrl = openUrl;
  567. }
  568.  
  569. public static String getSaveUrl() {
  570. return saveUrl;
  571. }
  572.  
  573. public static void setSaveUrl(String saveUrl) {
  574. JF_Notpad.saveUrl = saveUrl;
  575. }
  576.  
  577. //保存文件其中包含了,判断文件是否存在
  578. public void saveFile(){
  579. String str=this.content.getText();
  580. System.out.println("正在保存:\n"+str);
  581.  
  582. //判断文件是否存在,不存在则创建文件
  583. File file=new File(openUrl);
  584. if(!file.exists()){
  585. try{
  586. file.createNewFile();
  587. }catch(Exception a){
  588. System.out.println("创建文件 "+file.getAbsolutePath()+" 失败!!");
  589. }
  590. }
  591.  
  592. PrintStream p=null;
  593. try{
  594. p=new PrintStream(new File(openUrl));
  595. System.setOut(p);
  596. System.out.print(str);
  597. saved=true;
  598. }catch(Exception a){
  599. System.out.println("Save File Error!!");
  600. saved=false;
  601. }finally{
  602. p.close();
  603. }
  604.  
  605. }
  606.  
  607. //判断文件是否修改过,并根据传入的属性判断新建那个文件
  608. public void isChangeFile(String properties){
  609. System.out.println("是否保存文件:"+saved);
  610. //判断文件是否修改过
  611. if(!(this.content.getText().equals(origContent))){
  612. saved=false;
  613. }
  614.  
  615. //如果内容为空,也视为已经保存
  616. if(this.content.getText()==null||this.content.getText().equals(""))saved=true;
  617.  
  618. System.out.println("判断后是否保存文件:"+saved);
  619. if(saved){
  620. switch(properties){
  621. case "txt":{
  622. this.content.setText("");
  623. this.setTitle("notpad");
  624. origContent="";
  625. }break;
  626. case "java":{
  627. this.setTitle("notpad");
  628. String str="public class Notpad {\n\tpublic static void main (String[] ages) {\n\t\t\n\t}\n}";
  629. this.content.setText(str);
  630.  
  631. origContent=str;
  632. }break;
  633. }
  634.  
  635. }else{
  636. System.out .println("弹出是否选择保存的对话框");
  637. Save saveF=new Save("Save",this);
  638.  
  639. }
  640. System.out.println("经过保存面板后是否保存文件:"+saved);
  641. }
  642.  
  643. //打开选择打开文件面板,并选择文件
  644. public void openFilePanel(){
  645. origContent=this.content.getText();
  646. //显示选择文件面板
  647. JFileChooser fileChoose=null;
  648. if(openUrl==null){
  649. fileChoose=new JFileChooser();
  650. }else{
  651. fileChoose=new JFileChooser(openUrl);
  652. }
  653. FileNameExtensionFilter filter=new FileNameExtensionFilter("txt & java","java","txt");
  654. fileChoose.setFileFilter(filter);
  655. fileChoose.setDialogTitle("Open File");
  656. fileChoose.showOpenDialog(null);
  657. fileChoose.setVisible(true);
  658.  
  659. //得到文件
  660. String url=fileChoose.getSelectedFile().getAbsolutePath(); //得到选择文件的路径
  661.  
  662. //设置保存面板显示的路径
  663. JF_Notpad.setOpenUrl(url);
  664. Save saveF=new Save("Save",this);
  665. saveF.setVisible(false);
  666. saveF.setUrl(url);
  667. JLabel temp=saveF.getUrlL();
  668. temp.setText(url+" ?");
  669. saveF.setUrlL(temp);
  670.  
  671. String name=fileChoose.getSelectedFile().getName();
  672. this.setTitle(name+" - Notpad"); //设置主窗口标题
  673.  
  674. //文件流
  675. FileReader fRead=null; BufferedReader buffRead=null;
  676.  
  677. try{
  678. fRead=new FileReader(openUrl);
  679. buffRead=new BufferedReader(fRead);
  680.  
  681. String str=""; String readLine="";
  682. readLine=buffRead.readLine();
  683. while(readLine!=null){
  684. str=str+readLine+"\r\n";
  685. readLine=buffRead.readLine();
  686. }
  687.  
  688. content.setText(str);
  689. origContent=str;
  690. }catch(Exception a){
  691. System.out.println("Open file error!!");
  692. }finally{
  693. try{
  694. fRead.close();
  695. buffRead.close();
  696. }catch(Exception b){
  697. System.out.println("Open file stream error!!");
  698. }
  699. }
  700. }
  701.  
  702. public boolean isSaved() {
  703. return saved;
  704. }
  705.  
  706. public void setSaved(boolean saved) {
  707. this.saved = saved;
  708. }
  709.  
  710. public boolean getOpenFileBool() {
  711. return openFileBool;
  712. }
  713.  
  714. public void setOpenFileBool(boolean openFileBool) {
  715. this.openFileBool = openFileBool;
  716. }
  717.  
  718. public String getOrigContent() {
  719. return origContent;
  720. }
  721.  
  722. public void setOrigContent(String origContent) {
  723. this.origContent = origContent;
  724. }
  725.  
  726. public JTextArea getContent() {
  727. return content;
  728. }
  729.  
  730. public void setContent(JTextArea content) {
  731. this.content = content;
  732. }
  733. }

JF_Notpad.java

  FontPanel.java实现,字体面板:

  

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. public class FontPanel extends JFrame implements ActionListener{
  6. private JList list1,list2,list3;
  7. private JScrollPane scroll1,scroll2,scroll3;
  8. private JLabel l1,l2,l3,l4,l5,l6,simpleL;
  9. private JButton ok,cancel,use;
  10. private JPanel p,scrollP,p2,space,space2,simple,mainP,bottomP;
  11. private static String fontN="KaiTi";
  12. private static int fontSytleN=0,sizeN=20;
  13. private JF_Notpad notpad ;
  14.  
  15. // public static void main (String[] ages) {
  16. // FontPanel f=new FontPanel("Font");
  17. //
  18. // }
  19.  
  20. FontPanel(String title,JF_Notpad notpad){
  21.  
  22. this.notpad=notpad;
  23.  
  24. String[] s={"Font","FontStyle","Size"};
  25.  
  26. //获取系统字体
  27. String[] font=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
  28. String style=" PLAIN,BOLD, ITALIC ";
  29. String[] fontStyle=style.split(",");
  30. String sizeStr="";
  31. for(int i=8;i<73;i++){
  32. sizeStr=sizeStr+i+",";
  33. }
  34. String[] size=sizeStr.split(",");
  35. int count=5;
  36.  
  37. list1=new JList(font);
  38. list1.setVisibleRowCount(count);
  39. list2=new JList(fontStyle);
  40. list2.setVisibleRowCount(count);
  41. list3=new JList(size);
  42. list3.setVisibleRowCount(count);
  43.  
  44. list1.setSelectedIndex(153);
  45. list2.setSelectedIndex(0);
  46. list3.setSelectedIndex(12);
  47.  
  48. scroll1=new JScrollPane(list1);
  49. scroll2=new JScrollPane(list2);
  50. scroll3=new JScrollPane(list3);
  51.  
  52. l1=new JLabel(s[0],JLabel.CENTER);
  53. l2=new JLabel(s[1],JLabel.CENTER);
  54. l3=new JLabel(s[2],JLabel.CENTER);
  55. l4=new JLabel(this.fontN,JLabel.CENTER);
  56. l5=new JLabel(fontStyle[this.fontSytleN],JLabel.CENTER);
  57. l6=new JLabel(String.valueOf(this.sizeN),JLabel.CENTER);
  58.  
  59. ok=new JButton("OK"); cancel=new JButton("Cancel");
  60. ok.setActionCommand("OK"); ok.addActionListener(this);
  61. cancel.setActionCommand("Cancel"); cancel.addActionListener(this);
  62.  
  63. p=new JPanel();
  64. p.setLayout(new GridLayout(2,3,10,10));
  65.  
  66. p.add(l1); p.add(l2); p.add(l3);
  67. p.add(l4); p.add(l5); p.add(l6);
  68.  
  69. scrollP=new JPanel(new GridLayout(1,3,10,10));
  70.  
  71. scrollP.add(scroll1); scrollP.add(scroll2); scrollP.add(scroll3);
  72.  
  73. simple=new JPanel();
  74. simple.setLayout(new FlowLayout(FlowLayout.LEFT));
  75. simpleL=new JLabel("AaBbYyZz");
  76. simpleL.setFont(new Font(FontPanel.fontN,FontPanel.fontSytleN,FontPanel.sizeN));
  77.  
  78. use=new JButton("Simple");
  79. use.setActionCommand("Simple");
  80. use.addActionListener(this);
  81.  
  82. simple.add(use); simple.add(new JLabel(" ")); simple.add(simpleL);
  83.  
  84. p2=new JPanel();
  85. p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
  86.  
  87. p2.add(ok); p2.add(cancel);
  88.  
  89. space=new JPanel();
  90. space2=new JPanel();
  91.  
  92. bottomP=new JPanel(new GridLayout(2,1,10,10));
  93. bottomP.add(simple); bottomP.add(p2);
  94.  
  95. mainP =new JPanel(new GridLayout(3,1,10,10));
  96.  
  97. mainP.add(p); mainP.add(scrollP); mainP.add(bottomP);
  98.  
  99. this.add(mainP);
  100. this.add(p2,BorderLayout.SOUTH);
  101. this.add(space,BorderLayout.WEST);
  102. this.add(space2,BorderLayout.EAST);
  103.  
  104. this.setTitle(title);
  105. this.setIconImage(new ImageIcon("img/notpad/font.png").getImage());
  106. this.setLocation(300,300);
  107. this.setSize(500,640);
  108. this.setVisible(true);
  109. this.setResizable(false);
  110. // this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
  111.  
  112. }
  113.  
  114. @Override
  115. public void actionPerformed(ActionEvent e) {
  116. switch(e.getActionCommand()){
  117. case "OK":{
  118. this.setFontN(this.list1.getSelectedValue().toString());
  119. this.setFontSytleN(this.list2.getSelectedIndex());
  120. this.setSizeN(this.list3.getSelectedIndex()+8);
  121. // System.out.println(this.getFontN()+"\t"+this.getFontSytleN()+"\t"+this.getSizeN());
  122. this.setVisible(false);
  123.  
  124. Font f=new Font(FontPanel.fontN,FontPanel.fontSytleN,FontPanel.sizeN);
  125. JTextArea t=notpad.getContent();
  126. t.setFont(f);
  127. notpad.setContent(t);
  128. }break;
  129. case "Cancel":{
  130. this.setVisible(false);
  131. }break;
  132. case "Simple":{
  133. this.simpleL.setFont(new Font(this.list1.getSelectedValue().toString(),this.list2.getSelectedIndex(),this.list3.getSelectedIndex()+8));
  134. }break;
  135. }
  136. }
  137.  
  138. public static String getFontN() {
  139. return fontN;
  140. }
  141.  
  142. public static void setFontN(String fontN) {
  143. FontPanel.fontN = fontN;
  144. }
  145.  
  146. public static int getFontSytleN() {
  147. return fontSytleN;
  148. }
  149.  
  150. public static void setFontSytleN(int fontSytleN) {
  151. FontPanel.fontSytleN = fontSytleN;
  152. }
  153.  
  154. public static int getSizeN() {
  155. return sizeN;
  156. }
  157.  
  158. public static void setSizeN(int sizeN) {
  159. FontPanel.sizeN = sizeN;
  160. }
  161. }

FontPanel.java

  About.java实现关于面板: 

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. public class About extends JFrame implements ActionListener {
  6. private JLabel [] l={null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null};
  7. private JPanel p,p2;
  8. private JButton b;
  9.  
  10. About(String title,String version,String copyrigth,String qqEmil,String relative,String href){
  11. String [] s={"","","Version:",version," "," ","@CopyRight:",copyrigth,"QQ邮箱:",qqEmil,"联系方式:",relative,"","<html><a href='"+href+"'>更多</a></html>>","",""};
  12. JPanel p=new JPanel();
  13. p.setLayout(new GridLayout((int)l.length/2,2));
  14. for(int i=0;i<l.length;i++){
  15. if(i%2==0){
  16. l[i]=new JLabel(s[i],JLabel.CENTER);
  17. }else{
  18. l[i]=new JLabel(s[i],JLabel.LEFT) ;
  19. }
  20. //设置手型
  21. if(i==13){
  22. l[i].setCursor(Cursor.getDefaultCursor().getPredefinedCursor(Cursor.HAND_CURSOR));
  23. }
  24. p.add(l[i]);
  25. }
  26. System.out.println(l.length);
  27. b=new JButton("OK");
  28. b.setActionCommand("OK");
  29. b.addActionListener(this);
  30.  
  31. p2=new JPanel();
  32. p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
  33. p2.add(b);
  34. p2.add(new JLabel(" "));
  35.  
  36. this.add(p);
  37. this.add(p2,BorderLayout.SOUTH);
  38. this.setTitle(title);
  39. this.setIconImage(new ImageIcon("img/notpad/about.png").getImage());
  40. this.setLocation(300,300);
  41. this.setSize(350,300);
  42. this.setVisible(true);
  43. this.setResizable(false);
  44. //this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
  45. }
  46.  
  47. public void actionPerformed(ActionEvent e)
  48. {
  49. System.out.println(e);
  50. }
  51. }

About.java

    Save.java实现文件的保存读取:

  

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Save extends JFrame implements ActionListener {
  6. private JLabel l,urlL;
  7. private JButton save,noSave,cancel;
  8. private String url;
  9. private JPanel bottomP,contentP;
  10. private Font font;
  11. private JF_Notpad notpad;
  12.  
  13. // public static void main(String[] ages){
  14. // Save s=new Save("Save");
  15. // }
  16.  
  17. Save(String title,JF_Notpad notpad){
  18. this.notpad=notpad;
  19.  
  20. font=new Font("",Font.BOLD,14);
  21.  
  22. url=notpad.getOpenUrl()+" ?";
  23. urlL=new JLabel(url,JLabel.CENTER);
  24. urlL.setFont(font);
  25. l=new JLabel("Do you want to save changes to ",JLabel.CENTER);
  26. l.setFont(font);
  27.  
  28. contentP=new JPanel(new GridLayout(2,1));
  29. contentP.add(l); contentP.add(urlL);
  30.  
  31. save=new JButton("Save");
  32. noSave=new JButton("Don't Save");
  33. cancel=new JButton("Cancel");
  34.  
  35. save.setActionCommand("Save");
  36. noSave.setActionCommand("noSave");
  37. cancel.setActionCommand("Cancel");
  38.  
  39. save.addActionListener(this);
  40. noSave.addActionListener(this);
  41. cancel.addActionListener(this);
  42.  
  43. bottomP=new JPanel(new FlowLayout(FlowLayout.RIGHT));
  44.  
  45. bottomP.add(save); bottomP.add(noSave); bottomP.add(cancel);
  46.  
  47. this.add(contentP);
  48. this.add(bottomP,BorderLayout.SOUTH);
  49.  
  50. this.setTitle(title);
  51. this.setLocation(300,300);
  52. this.setSize(400,170);
  53. this.setResizable(false);
  54. // this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
  55. this.setVisible(true);
  56.  
  57. }
  58.  
  59. @Override
  60. public void actionPerformed(ActionEvent e) {
  61. switch(e.getActionCommand()){
  62. case "Save":{
  63. notpad.saveFile();
  64. notpad.setSaved(true);
  65. notpad.setOrigContent(notpad.getContent().getText());
  66. this.setVisible(false);
  67.  
  68. }break;
  69. case "noSave":{
  70. notpad.setOrigContent(notpad.getContent().getText());
  71. notpad.setSaved(true);
  72. this.setVisible(false);
  73. }break;
  74. case "Cancel":{
  75. notpad.setSaved(false);
  76. this.setVisible(false);
  77. }break;
  78. }
  79. }
  80.  
  81. public String getUrl() {
  82. return url;
  83. }
  84.  
  85. public void setUrl(String url) {
  86. this.url = url;
  87. }
  88.  
  89. public JLabel getUrlL() {
  90. return urlL;
  91. }
  92.  
  93. public void setUrlL(JLabel urlL) {
  94. this.urlL = urlL;
  95. }
  96. }

Save.java

  1. 版权
  2.  
  3. 作者:feiquan
  4.  
  5. 出处:http://www.cnblogs.com/feiquan/
  6.  
  7. 版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  8.  
  9. 大家写文都不容易,请尊重劳动成果~ 这里谢谢大家啦(*/ω\*)

  

java实现Windows记事本的更多相关文章

  1. 签名、BOM头、编码、Windows记事本编码、java编码解码的那些事

    对于Windows记事本: ANSI :GB2312 java中应使用GBK解码 Unicode :有签名的UTF-16LE java中应使用UTF-16解码 Unicode big endian : ...

  2. 编写运行最简单的java程序——使用记事本编写java程序

    第一个java程序--使用记事本编辑 经过上篇文章的java环境搭建成功的小伙伴们可以在自己的计算机上编写属于自己的java程序了yo~ 还没有搭建环境变量的小伙伴请转移到上一篇的随笔中去完成搭建. ...

  3. Windows 记事本的 ANSI、Unicode、UTF-8 这三种编码模式有什么区别?

    [梁海的回答(99票)]: 简答.一些细节暂无精力查证,如果说错了还请指出. 一句话建议:涉及兼容性考量时,不要用记事本,用专业的文本编辑器保存为不带 BOM 的UTF-8. * * * 如果是为了跨 ...

  4. java 调用windows bat脚本

    当我们需要在java程序中调用外部程序,我们可用通过Runtime.exec()调用来完成. The class java.lang.Runtime features a static method ...

  5. 使用Java修改Windows注册表

    使用Java修改Windows注册表,使用最基本的就是cmd命令. 事例和运行结果如下所示: package day01; import java.io.IOException; /* 1,reg a ...

  6. Java 修改Windows注册表,以实现开机自启动应用程序。

    使用Java修改Windows注册表,使用最基本的就是cmd命令. 事例和运行结果如下所示: package day01; import java.io.IOException; /* 1,reg a ...

  7. Selenium2学习-001-Selenium2 WebUI自动化Java开发 Windows 环境配置

    此文主要介绍 Selenium2 WebUI自动化Java开发 Windows 环境配置,供各位亲们参考,若有不足之处,敬请各位大神指正,非常感谢! 所需软件列表如下所示: 所属分类 具体名称 备注 ...

  8. Java读写Windows共享文件夹 .

    版权声明:本文为博主原创文章,未经博主允许不得转载. 项目常常需要有访问共享文件夹的需求,例如共享文件夹存储照片.文件等.那么如何使用Java读写Windows共享文件夹呢? Java可以使用JCIF ...

  9. Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例【附详细代码】

    http://blog.csdn.net/xiefu5hh/article/details/51707529 Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例[附 ...

随机推荐

  1. [Swift]LeetCode526. 优美的排列 | Beautiful Arrangement

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...

  2. [Swift]LeetCode636. 函数的独占时间 | Exclusive Time of Functions

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  3. [Swift]LeetCode640. 求解方程 | Solve the Equation

    Solve a given equation and return the value of x in the form of string "x=#value". The equ ...

  4. [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  5. [Swift]LeetCode849. 到最近的人的最大距离 | Maximize Distance to Closest Person

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  6. python高级-生成器(17)

    1. 什么是⽣成器 通过列表⽣成式,我们可以直接创建⼀个列表.但是,受到内存限制,列表容量肯定是有限的.⽽且,创建⼀个包含100万个元素的列表,不仅占⽤很⼤的存储空间,如果我们仅仅需要访问前⾯⼏个元素 ...

  7. JVM基础系列开篇:为什么要学虚拟机?

    跟许多人一样,我一开始接触 Java 虚拟机只是因为面试需要用到,所以硬着头皮看看.所以很多人对于为什么要学虚拟机这个问题,他们的答案都是:因为面试.但我经过了几年的学习和实战,我发现其实学习虚拟机并 ...

  8. Nginx学习系列二Linux下Nginx实现负载均衡

    关于在本地虚拟机(VMware 14)下安装Linux同时安装Nginx,请参考Nginx学习系列之搭建环境 1.启动Nginx 在Nginx安装成功的前提下,启动Nginx 已root模式登陆(权限 ...

  9. 使用ML.NET预测纽约出租车费

    有了上一篇<.NET Core玩转机器学习>打基础,这一次我们以纽约出租车费的预测做为新的场景案例,来体验一下回归模型. 场景概述 我们的目标是预测纽约的出租车费,乍一看似乎仅仅取决于行程 ...

  10. Owin学习笔记(二) 中间件开发

    Owin中也有类似于ASP.NET的管道,以前在做ASP.NET项目的时候,可以制作很多不同功能HttpHandler或者HttpModule并注册在Web.config中重复使用.在Owin的管道中 ...