GUI_记事本

来源:互联网 发布:python base64 json 编辑:程序博客网 时间:2024/06/05 16:15
需求:设计简单的记事本程序。其中已实现【打开】【保存】【退出】3个功能。
 


 代码如下:
  1. package com.cn.gui.event;
  2. import java.awt.BorderLayout;
  3. import java.awt.FileDialog;
  4. import java.awt.ScrollPane;
  5. import java.awt.TextArea;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.FileReader;
  12. import java.io.IOException;
  13. import javax.swing.JFrame;
  14. import javax.swing.JMenu;
  15. import javax.swing.JMenuBar;
  16. import javax.swing.JMenuItem;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JTextArea;
  19. import com.cn.gui.util.FrameUtil;
  20. /**
  21. * Author:Liu Zhiyong(QQ:1012421396)
  22. * Version:Version_1
  23. * Date:2016年8月13日19:44:16
  24. * Desc:
  25. 需求:设计简单的记事本程序。其中已实现【打开】【保存】【退出】3个功能。
  26. */
  27. public class NotePad {
  28. JFrame frame = new JFrame("记事本");
  29. //菜单条
  30. JMenuBar menuBar = new JMenuBar();
  31. //文件菜单
  32. JMenu fileMenu = new JMenu("文件");
  33. JMenu editMenu = new JMenu("编辑");
  34. JMenu aboutMenu = new JMenu("帮助");
  35. JMenu switchMenu = new JMenu("切换工作目录");
  36. //菜单项
  37. JMenuItem openItem = new JMenuItem("打开");
  38. JMenuItem saveItem = new JMenuItem("保存");
  39. JMenuItem exitItem = new JMenuItem("退出");
  40. JMenuItem aboutItem = new JMenuItem("关于");
  41. JMenuItem switchMenuItem1 = new JMenuItem("目录1");
  42. JMenuItem switchMenuItem2 = new JMenuItem("目录2");
  43. JMenuItem switchMenuItem3 = new JMenuItem("目录3");
  44. //文本域
  45. JTextArea textArea = new JTextArea(); //JTextArea()中输入只有"\n",所以后面需要替换改成"\r\n"
  46. //TextArea textArea = new TextArea();//TextArea()中输入带"\r\n", 没有JTextArea的毛病,但是不好看
  47. //滚动条
  48. ScrollPane scrollPane = new ScrollPane();
  49. public void initNotePad(){
  50. //菜单添加菜单项
  51. switchMenu.add(switchMenuItem1);
  52. switchMenu.add(switchMenuItem2);
  53. switchMenu.add(switchMenuItem3);
  54. fileMenu.add(openItem);
  55. fileMenu.add(saveItem);
  56. fileMenu.add(exitItem);
  57. aboutMenu.add(aboutItem);
  58. //菜单添加菜单
  59. editMenu.add(switchMenu);
  60. //菜单条添加菜单
  61. menuBar.add(fileMenu);
  62. menuBar.add(editMenu);
  63. menuBar.add(aboutMenu);
  64. //窗体添加菜单条
  65. frame.add(menuBar, BorderLayout.NORTH);
  66. //滚动条添加文本域
  67. scrollPane.add(textArea);
  68. //窗体添加滚动条
  69. frame.add(scrollPane);
  70. //自定义函数工具,初始化窗体
  71. FrameUtil.initFrame(frame, 500, 600);
  72. //给打开openItem添加事件
  73. openItem.addActionListener(new ActionListener() {
  74. FileReader fileReader = null;
  75. @Override
  76. public void actionPerformed(ActionEvent e) {
  77. //打开前先清空文本域
  78. clearTextArea(textArea);
  79. try {
  80. //创建文件对话框
  81. FileDialog fileDialog = new FileDialog(frame, "请选择打开的文件", FileDialog.LOAD);
  82. //设置对话框可见
  83. fileDialog.setVisible(true);
  84. //获取用户选择的路径与要打开文件的文件名
  85. String pathName = fileDialog.getDirectory();
  86. String fileName = fileDialog.getFile();
  87. //创建输入流对象
  88. fileReader = new FileReader(new File(pathName, fileName));
  89. //创建字符缓冲流,读取输入流中的数据
  90. char[] buf = new char[1024];
  91. int length = 0;
  92. while((length = fileReader.read(buf)) != -1){
  93. //将读取到的数据写入到textArea中
  94. textArea.setText(textArea.getText() + new String(buf, 0, length));
  95. }
  96. } catch (FileNotFoundException e1) {
  97. // TODO Auto-generated catch block
  98. e1.printStackTrace();
  99. } catch (IOException e1) {
  100. // TODO Auto-generated catch block
  101. e1.printStackTrace();
  102. }finally{
  103. //关闭资源
  104. try {
  105. fileReader.close();
  106. System.out.println("关闭输入流成功");
  107. } catch (IOException e1) {
  108. System.out.println("关闭输入流失败");
  109. e1.printStackTrace();
  110. }
  111. }
  112. }
  113. });
  114. //给保存saveItem添加事件
  115. saveItem.addActionListener(new ActionListener() {
  116. FileOutputStream fileOutputStream = null;
  117. @Override
  118. public void actionPerformed(ActionEvent e) {
  119. try {
  120. //创建文件对话框
  121. FileDialog fileDialog = new FileDialog(frame, "请选择保存的路径", FileDialog.SAVE);
  122. //设置对话框可见
  123. fileDialog.setVisible(true);
  124. //获取用户选择的路径与输入的文件名
  125. String pathName = fileDialog.getDirectory();
  126. String fileName = fileDialog.getFile();
  127. //创建一个输出流对象
  128. fileOutputStream = new FileOutputStream(new File(pathName, fileName));
  129. //获取文本域的内容,把内容写出
  130. String content = textArea.getText();
  131. content = content.replace("\n", "\r\n");//"\n"替换成"\r\n"
  132. fileOutputStream.write(content.getBytes());
  133. } catch (FileNotFoundException e1) {
  134. // TODO Auto-generated catch block
  135. e1.printStackTrace();
  136. } catch (IOException e1) {
  137. // TODO Auto-generated catch block
  138. e1.printStackTrace();
  139. }finally{
  140. //关闭资源
  141. try {
  142. fileOutputStream.close();
  143. System.out.println("关闭输出流成功");
  144. } catch (IOException e1) {
  145. System.out.println("关闭输出流失败");
  146. e1.printStackTrace();
  147. }
  148. }
  149. }
  150. });
  151. //给退出菜单项添加事件
  152. exitItem.addActionListener(new ActionListener() {
  153. @Override
  154. public void actionPerformed(ActionEvent e) {
  155. int opt = JOptionPane.showConfirmDialog(frame, "确定退出?");
  156. if(opt == 0){
  157. System.exit(1);//退出jvm 如果参数是0表示正常退出jvm,非0表示异常退出jvm
  158. }else if(opt == 1 || opt == 2){
  159. return;//结束当前函数
  160. }
  161. }
  162. });
  163. }
  164. //清空显示文本域
  165. public static void clearTextArea(JTextArea textArea){
  166. String text = textArea.getText();
  167. if(text != null){
  168. textArea.setText("");
  169. }
  170. }
  171. public static void main(String[] args) {
  172. new NotePad().initNotePad();
  173. }
  174. }

 
 

0 0
原创粉丝点击