Java课程设计

【题目】:使用已经学习的java知识编写一个简单的记事本

【实现功能】:

【1】文件: 新建 打开 保存 另存为 退出

【2】编辑: 剪切 复制 粘贴 删除 撤销

【3】格式: 自动换行 字体格式 字体颜色 查找||替换

【4】查看: 关于记事本

【5】帮助 :帮助选项

【6】显示当前时间

【使用的类】:

import.java.io;
import java.swt;
import javax.swing;
import javax.swing.undo.UndoManager

代码:

package Note;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.undo.UndoManager; // 对菜单活动事件撤销的实现public class NotePad extends JFrame /// extends继承JFrame类
{int start = 0, end = 0;private UndoManager manager = new UndoManager();// 添加布局管理器private JTextArea text = new JTextArea();private JFileChooser jfc = new JFileChooser();private String jsb = "新建记事本";private File file;private JMenuBar menu;// ---------------菜单栏private JMenu File_bar, Edit_bar, Format_bar, View_bar, Help_bar;// ---------------文件菜单private JMenuItem File_bar_creat, File_bar_open, File_bar_save, File_bar_othersave, File_bar_exit;// ---------------编辑菜单private JMenuItem Edit_bar_Revoke, Edit_bar_shear, Edit_bar_copy, Edit_bar_paste, Edit_bar_delete;// ---------------格式菜单private JMenuItem Format_bar_hl, m30, Format_bar_ztxz, Format_bar_ztsz, View_bar_about, Help_bar_help;// ---------------查找替换菜单private JMenuItem Format_bar_find_replace;private JLabel statusLabel1;private JToolBar statusBar;GregorianCalendar time = new GregorianCalendar();int hour = time.get(Calendar.HOUR_OF_DAY);int min = time.get(Calendar.MINUTE);int second = time.get(Calendar.SECOND);/* 文件格式过滤器 */public class filter extends javax.swing.filechooser.FileFilter{public boolean accept(File file){String name = file.getName();name.toString(); // 该字符串中的数字被转换为字符/* 文件后缀是.txt且是个目录 */if (name.endsWith(".txt") || file.isDirectory()){return true;} elsereturn false;}/* 将引用具体子类的子类对象的方法,不可以省略类中的getDescription(),原因是编译器只允许调用在类中声明的方法. */public String getDescription(){return ".txt";}}/* 将菜单项 JMenu添加菜单 JMenuBar */public JMenu AddBar(String name, JMenuBar menu){JMenu jmenu = new JMenu(name);menu.add(jmenu);return jmenu;}/* 将菜单项JMenuItem添加到菜单JMenu */public JMenuItem AddItem(String name, JMenu menu){JMenuItem jmenu = new JMenuItem(name);menu.add(jmenu);return jmenu;}class Clock extends Thread{ // 模拟时钟public void run(){while (true){GregorianCalendar time = new GregorianCalendar();int hour = time.get(Calendar.HOUR_OF_DAY);int min = time.get(Calendar.MINUTE);int second = time.get(Calendar.SECOND);statusLabel1.setText(" 当前时间:" + hour + ":" + min + ":" + second);try{Thread.sleep(950);} catch (InterruptedException exception){}}}}NotePad note;// public void NotePad(){Container container = getContentPane();setTitle(jsb); // 设置窗口标题setBounds(250, 250, 500, 500);// 设置边界JMenuBar menu = new JMenuBar(); // 添加菜单 JMenuBarthis.setJMenuBar(menu);// 调用this方法text.getDocument().addUndoableEditListener(manager);// 用于获得程序当前有效的文档/** Font是JAVA中的字体类,PLAIN是Font类中的静态常量( static final ) ,表示是:普通样式常量 BOLD* :粗体样式常量 ,ITALIC: 斜体样式常量,14:磅*/text.setFont(new Font("宋体", Font.PLAIN, 14));/* 光标颜色 */text.setCaretColor(Color.gray);/* 选中字体颜色 */text.setSelectedTextColor(Color.blue);/* 选中背景颜色 */text.setSelectionColor(Color.green);/* 是否换行 */text.setLineWrap(true);/* 是否单词边界换行(即有空白) */text.setWrapStyleWord(true);/* 文本区与边框的间距,四个参数分别为上、左、下、右 */text.setMargin(new Insets(3, 5, 3, 5));/* 创建一个 JScrollPane,它将视图组件显示在一个视口中,视图位置可使用一对滚动条控制 */add(new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));File_bar = this.AddBar("文件(F)", menu);// File_bar.setMnemonic('F');Edit_bar = this.AddBar("编辑(E)", menu);Format_bar = this.AddBar("格式(O)", menu);View_bar = this.AddBar("查看(V)", menu);Help_bar = this.AddBar("帮助(H)", menu);/* 文件选项 *//* 新建选项 */File_bar_creat = this.AddItem("新建(N)   Ctrl+N", File_bar);File_bar_creat.addActionListener(new ActionListener(){// @Overridepublic void actionPerformed(ActionEvent arg0){// 设置新文件内容text.setText("");}});/* 打开选项 */File_bar_open = this.AddItem("打开(O)   Ctrl+O", File_bar);File_bar_open.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){try{jfc.setCurrentDirectory(new File("."));// 设置当前目录jfc.setFileFilter(new filter()); // 过滤文件/** 确定是否将AcceptAll FileFilter用作可选择过滤器列表中一个可用选项。如果为假,* 则AcceptAll文件过滤器从可用的文件过滤列表中删除。* 如果为true,则AcceptAll文件过滤器将成为可用的文件过滤器。*/jfc.setAcceptAllFileFilterUsed(false); // 全选文件jfc.showOpenDialog(null); // 弹出一个 "Open File" 文件选择器对话框。file = jfc.getSelectedFile(); // 获取已经选择目录jsb = file.getName(); // 获取目录名setTitle(jsb); // 显示目录名int length = (int) (jfc.getSelectedFile()).length();char[] ch = new char[length];FileReader fr = new FileReader(file);fr.read(ch);jsb = new String(ch);text.setText(jsb.trim()); // 获得对象的字段的值,然后转成string类型,并且去掉前后空白~~ToString()是转化为字符串的方法// Trim()是去两边空格} catch (Exception e){JOptionPane.showMessageDialog(null, e);}}});/* 保存选项 = (1)如果文件为空,新建一个目录保存;(2)如果当前文件存在,直接保存 */File_bar_save = this.AddItem("保存(S)   Ctrl+O", File_bar);File_bar_save.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){if (file == null){try{jfc = new JFileChooser();jfc.setCurrentDirectory(null);jsb = JOptionPane.showInputDialog("请输入文件名:") + ".txt";/** setSelectedFile返回的是对话框中选中的文件但如果对话框类型是showSaveDialog的话,* 那么这里返回的值是你要保存的文件, 这个文件可能存在,可能不存在,就是你在对话框中输入的文件名了,* 既然知道了文件,如果不存在,就新建一个,然后向文件写入数据,这样就可以实现保存了*/jfc.setSelectedFile(new File(jsb));jfc.setFileFilter(new filter());int temp = jfc.showSaveDialog(null); // 获取当前对象if (temp == jfc.APPROVE_OPTION) // 获得选中的文件对象{if (file != null)file.delete();file = new File(jfc.getCurrentDirectory(), jsb);file.createNewFile();FileWriter fw = new FileWriter(file);fw.write(text.getText());fw.close();}} catch (Exception e){JOptionPane.showMessageDialog(null, e);}} else{try{FileWriter fw = new FileWriter(file);fw.write(text.getText());fw.close();} catch (Exception e){JOptionPane.showMessageDialog(null, e);}}}});/* 另存为选项 */File_bar_othersave = this.AddItem("另存为(A)...", File_bar);File_bar_othersave.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){// file fw = new file();jfc = new JFileChooser();jfc.setCurrentDirectory(new File("."));try{if (file == null){jsb = JOptionPane.showInputDialog("请输入文件名:") + ".txt";} elsejsb = file.getName();jfc.setSelectedFile(new File(jsb));jfc.setFileFilter(new filter());int temp = jfc.showSaveDialog(null);if (temp == jfc.APPROVE_OPTION) // 获得选中的文件对象{if (file != null)file.delete();file = new File(jfc.getCurrentDirectory(), jsb);file.createNewFile();FileWriter fw = new FileWriter(file);fw.write(text.getText());fw.close();}} catch (Exception e){JOptionPane.showMessageDialog(null, e);}}});/* 将默认大小的分隔符添加到工具栏的末尾。 */File_bar.addSeparator();/* 退出选项 + 退出提示 */File_bar_exit = this.AddItem("退出(X)", File_bar);File_bar_exit.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){int state = JOptionPane.showConfirmDialog(note, "您确定要退出?退出前请确定您的文件已保存");if (state == JOptionPane.OK_OPTION)System.exit(0);}});/* 编辑选项 *//* 撤消选项 */Edit_bar_Revoke = this.AddItem("撤销(U)   Ctrl+Z", Edit_bar);Edit_bar_Revoke.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){if (manager.canUndo())manager.undo();}});/* 剪切选项 */Edit_bar_shear = this.AddItem("剪切(T)   Ctrl+X", Edit_bar);Edit_bar_shear.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){text.cut();}});/* 复制选项 */Edit_bar_copy = this.AddItem("复制(C)   Ctrl+C", Edit_bar);Edit_bar_copy.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){text.copy();}});/* 粘贴选项 */Edit_bar_paste = this.AddItem("粘贴(P)   Ctrl+V", Edit_bar);Edit_bar_paste.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){text.paste();}});/* 删除选项=用空格替换从当前选取的开始到结束 */Edit_bar_delete = this.AddItem("删除(L)   Del", Edit_bar);Edit_bar_delete.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){text.replaceRange("", text.getSelectionStart(), text.getSelectionEnd());}});/* 自动换行选项 */// m26 = this.AddItem("自动换行(W)", m3);JCheckBoxMenuItem Format_bar_hl = new JCheckBoxMenuItem("自动换行", true);Format_bar.add(Format_bar_hl);Format_bar_hl.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){/* 根据文件名获取文件信息 */if (Format_bar_hl.getState())text.setLineWrap(true);elsetext.setLineWrap(false);}});/* 字体选项 *//** 字体格式设置选项 GraphicsEnvironment 类描述了 Java(tm) 应用程序在特定平台上可用* * 的 GraphicsDevice 对象和 Font 对象的集合*/Format_bar_ztxz = this.AddItem("字体选择(F)", Format_bar);Format_bar_ztxz.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){/* 获取本地图形环境 */GraphicsEnvironment gr = GraphicsEnvironment.getLocalGraphicsEnvironment();/* 字体名称列表框 */JList fontnames = new JList(gr.getAvailableFontFamilyNames());/* JScrollPane 管理视口、可选的垂直和水平滚动条以及可选的行和列标题视口 */int selection = JOptionPane.showConfirmDialog(null, new JScrollPane(fontnames), "请选择字体",JOptionPane.OK_CANCEL_OPTION);Object selectedFont = fontnames.getSelectedValue();if (selection == JOptionPane.OK_OPTION && selectedFont != null){text.setFont(new Font(fontnames.getSelectedValue().toString(), Font.PLAIN, 20));}}});/* 字体颜色设置选项 */Format_bar_ztsz = this.AddItem("颜色(C)", Format_bar);Format_bar_ztsz.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){Color color = JColorChooser.showDialog(null, "文字颜色选择", Color.BLACK);text.setForeground(color);}});Format_bar_find_replace = this.AddItem("替换(R)||查找(F)", Format_bar);Format_bar_find_replace.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){JDialog search = new JDialog();search.setSize(300, 100);search.setLocation(450, 350);JLabel label_1 = new JLabel("查找的内容");JLabel label_2 = new JLabel("替换的内容");JTextField textField_1 = new JTextField(5);JTextField textField_2 = new JTextField(5);JButton buttonFind = new JButton("查找下一个");JButton buttonChange = new JButton("替换");JPanel panel = new JPanel(new GridLayout(2, 3));panel.add(label_1);panel.add(textField_1);panel.add(buttonFind);panel.add(label_2);panel.add(textField_2);panel.add(buttonChange);search.add(panel);search.setVisible(true);// 为查找下一个 按钮绑定监听事件buttonFind.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String findText = textField_1.getText();// 查找的字符String textArea = text.getText();// 当前文本框的内容start = textArea.indexOf(findText, end);end = start + findText.length();if (start == -1) // 没有找到{JOptionPane.showMessageDialog(null, "没找到" + findText, "记事本", JOptionPane.WARNING_MESSAGE);text.select(start, end);} else{text.select(start, end);}}});// 为替换按钮绑定监听时间buttonChange.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String changeText = textField_2.getText();// 替换的字符串/* 如果选定文件为真 */if (text.getSelectionStart() != text.getSelectionEnd())text.replaceRange(changeText, text.getSelectionStart(), text.getSelectionEnd());}});}});View_bar_about = this.AddItem("关于记事本(About)", View_bar);View_bar_about.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){JOptionPane.showMessageDialog(null, "记事本\n开发语言:JAVA\n开发者:【herongwei&&limiao】\n联系方式:rongweihe1995@gmail.com", "关于",JOptionPane.PLAIN_MESSAGE);}});Help_bar_help = this.AddItem("帮助选项(H)", Help_bar);Help_bar_help.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){JOptionPane.showMessageDialog(null, "详细代码请移步\n博客:https://rongweihe.github.io/", "帮助", JOptionPane.PLAIN_MESSAGE);}});JPanel toolBar = new JPanel();toolBar.setLayout(new FlowLayout(FlowLayout.LEFT));// --------------------------------------向容器添加工具栏container.add(toolBar, BorderLayout.NORTH);// -----------------------------------创建和添加状态栏// toolBar.add(File_bar);// toolBar.add(Edit_bar);// toolBar.add(Format_bar);// toolBar.add(Help_bar);// toolBar.add(View_bar);statusBar = new JToolBar();statusBar.setLayout(new FlowLayout(FlowLayout.LEFT));statusLabel1 = new JLabel(" 当前时间:" + hour + ":" + min + ":" + second);statusBar.add(statusLabel1);statusBar.addSeparator();container.add(statusBar, BorderLayout.SOUTH);statusBar.setVisible(true);Clock clock = new Clock();clock.start();this.setResizable(true); // 窗体是否可变this.setVisible(true); // 窗体是否可见this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String args[]){NotePad example = new NotePad();}/** public class jjishiben{ public static void main(Strin args[]) { public* void Run() { NotePad note = new NotePad(); note.setTitle("记事本");* note.setVisible(true); note.setBounds(250, 250, 500, 500);* note.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } }*/
}

2015/12/15   新增获取本机时间功能,显示在一个StausBar里面。

2015/12/15   新增查找和替换功能,当用户没有选择任何字符时,不会默认在最后面添加字符。

Java课程设计- 记事本代码相关推荐

  1. Java课程设计-日历记事本

    #Java课程设计-日历记事本 ##日历记事本 要求:带有日程提醒功能的日历(数据库可采用Access.SQL Server或者MySQL). (1)显示信息:用户可以向前翻页查询前一个月的日期,也可 ...

  2. java课程设计日历记事本_《Java程序设计》课程设计日历记事本.doc

    <Java程序设计>课程设计日历记事本 PAGE PAGE 2 本科生课程设计 课程名称 Java程序设计课程设计 课程编号 j1620011 题目 日历记事本 学号 2008116222 ...

  3. java课程设计日历_java课程设计日历记事本赵锐.doc

    java课程设计日历记事本赵锐.doc 2本科生课程设计课程名称JAVA程序设计课程设计题目日历记事本学号201440930252学生姓名赵锐所在专业2014计算机学院所在班级信工2班成绩课程设计时间 ...

  4. 打怪游戏Java课程设计_java 课程设计大作业 写的一个RPG游戏(代码+文档)

    [实例简介] java 课程设计大作业 写的一个RPG游戏(代码+文档) java 课程设计大作业 写的一个RPG游戏(代码+文档) [实例截图] [核心代码] Rebellion-master ├─ ...

  5. java课程设计指导记录表_JAVA课程设计指导书ofr2012级.doc

    JAVA课程设计指导书ofr2012级 <Java面向对象程序设计> 课程设计指导书 专 业: XXX软服XX班 指导教师: XXXX 计算机与信息工程学院 2013年12月26日 一.目 ...

  6. Java课程设计题目二:保存计算过程的计算器

    Java课程设计题目二:保存计算过程的计算器 1 设计要求 参考Windows 操作系统提供的计算器设计一个实用的计算器,要求除了具有普通的计算功能外,还具有保存计算过程的功能. ①单击计算器上的数字 ...

  7. java 课程设计文本编辑器,JAVA课程设计--文本编辑器

    JAVA课程设计--文本编辑器 Java 语言课程期末作业 1 Java 语言课程期末作业 题 目 第 8 题,文本编辑器 学 院 计算机学院 专 业 计算机科学与技术 班 别 学 号 姓 名 201 ...

  8. Java课程设计题目一:动物换位

    Java课程设计题目一:动物换位 1 设计要求 设计GUI界面的动物换位游戏,游戏结果是让左.右两组动物交换位置.具 体要求如下: ①在水平排列的7个位置上左.右各有3个类型相同的动物,中间的位置上没 ...

  9. JAVA课程设计——“小羊吃蓝莓”小游戏

    JAVA课程设计--"小羊吃蓝莓"小游戏 1. 团队课程设计博客链接 http://www.cnblogs.com/HXY071/p/7061216.html 2. 个人负责模块或 ...

  10. java中国象棋网络对弈,java课程设计---中国象棋对弈系统

    java课程设计---中国象棋对弈系统 1 目目 录录 摘要 1 关键字 1 正文 2 1.程序设计说明. 2 1.1 程序的设计及实现 2 1.1.1搜索引擎的实现(engine包) . 2 1.1 ...

最新文章

  1. golang中的os包
  2. 这是一个测试rss的内容哦
  3. 太简单!日常小动作让你摆脱程序员职业病
  4. 【GitHub】如何合并分支?
  5. 天梯赛2016-L2
  6. leetcode 236. 二叉树的最近公共祖先 思考分析
  7. Win7系统虚拟键盘打开的方法
  8. 中文乱码之springboot框架中两工程之间参数传递乱码
  9. Leetcode Trie Conclusion
  10. HyperLeger Composer 重启 | 进入play ground | 进入 couchdb
  11. Java 编程实例 - 查找数组中的重复元素
  12. qmh_confluence源码研究
  13. 两平面平行但不重合的条件是_____怎样证明平行
  14. 日语开发java自我介绍,用日语自我介绍,这些你一定会用到
  15. 华为、微软、戴尔等企业将参加“多哈智慧城市博览会”
  16. 梯田油菜花海距杭州仅120公里
  17. 你对Web3的迅速发展一无所知,逃离大厂去拥抱Web3
  18. LaTex排版一二三
  19. 纳米数据世界杯数据,世界杯分组,世界杯赛程,世界杯实时比分api接口
  20. 2012年5月后QQ空间农场Farmkey最新计算字符串

热门文章

  1. SSH工具连接谷歌云VPS实例
  2. 永洪BI悬停效果展示---第三集
  3. mysql导入数据dmp文件怎么打开_如何在oracle中导入dmp数据库文件
  4. 音频管理工具- Realtek 高清音频管理器
  5. ifix如何设画面大小_组态软件实用技术教程第3章iFIX画面设计.ppt
  6. 微信二维码扫码登录的原理
  7. 老男孩学python_在老男孩如何学习python?老男孩python课程好吗、
  8. GoldenDict和主流英语词典产品
  9. 大数据学习---HIVE入门SQL学习
  10. 利用BioEdit软件进行短序列序列比对之查看突变位点