




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
import java awt import java awt event import java text import java util import java io import javax swing import javax swing event import java util List public class TextFileEditorJFrame extends JFrame implements ActionListener ItemListener MouseListener private File file 当前文件 文件菜单 定义的添加项 新建 打开 保存 另存为 退出 private JMenuItem menuitem create menuitem open menuitem save menuitem saveas menuitem exit 编辑菜单 定义的添加项 剪切 复制 粘贴 删除 private JMenuItem menuitem cut menuitem copy menuitem paste menuitem delete 右键菜单项 剪切 复制 粘贴 删除 private JMenuItem final cut final copy final paste final delete private JTextArea textarea 文本编辑区 private JButton button color 设置颜色 private JScrollPane scroll 为文本编辑区提供滚动条 private JDialog dialog1 dialog2 对话框 private JLabel label dialog label dialog2 private Boolean via false private JPopupMenu popupmenu 右键弹出菜单 private JCheckBoxMenuItem checkbox cuti checkbox xieti 复选框表示粗体 斜体 private String fileName null 文件名 private int k 0 k 用来存放字号大小 private String size 宋体 private JComboBox jco1 jco2 工具栏处表示字体 字号组合框 File currentFile saveFileName null fileName1 null 文件类 public TextFileEditorJFrame 空文件的构造方法 初始化 super 文本编辑器 框架的标题 this setSize 700 500 this setLocation 140 140 相对界面的位置 this setDefaultCloseOperation JFrame EXIT ON CLOSE textarea new JTextArea textarea addMouseListener this this add textarea 添加文本区 this addMenu 调用自定义的 addMenu 方法 添加菜单栏 this addToolBar 调用自定义的 addToolBar 方法 添加工具栏 textarea setFont new Font 宋体 1 16 设置文本区初始字体 this setVisible true textarea requestFocus 设置文本区焦聚 this file null 空文件对象 public TextFileEditorJFrame String filename 指定文件名的构造方法 即打开文件后显示 文件内容 this if filename null this file new File filename this setTitle filename 将文件名添加在窗口标题 栏上 this textarea setText this readFromFile 读取指定文件中的字符串 并显 示在文本区中 public TextFileEditorJFrame File file 指定文件对象的构造方法 通过文件对象调用函数得到文件 名 从而读取文件内容 this if file null this file file this setTitle this file getName 把标题设置成得到的文件名 通过文件对 象调用函数得到文件名 this textarea setText this readFromFile 调用 readFromFile 方法 把读取的 内容在文本区显示 private void addMenu 添加主菜单 JMenuBar menubar new JMenuBar 创建菜单栏 this setJMenuBar menubar 将菜单条设置为当前窗口的菜单条 文件菜单 JMenu menu file new JMenu 文件 menubar add menu file 创建文件菜单 并添加到菜单栏 menuitem create new JMenuItem 新建 N 创建新建菜单项 menuitem create setAccelerator KeyStroke getKeyStroke KeyEvent VK N ActionEvent CTRL MASK 为新建设置快 捷键 Ctrl N menu file add menuitem create 添加到文件菜单 menuitem create addActionListener this menuitem open new JMenuItem 打开 O 创建打开菜单项 menuitem open setAccelerator KeyStroke getKeyStroke KeyEvent VK O ActionEvent CTRL MASK 为打开设置快 捷键 Ctrl O menu file add menuitem open 添加到文件菜单 menuitem open addActionListener this menuitem save new JMenuItem 保存 S 创建保存菜单项 menuitem save setAccelerator KeyStroke getKeyStroke KeyEvent VK S ActionEvent CTRL MASK 为保存设置快 捷键 Ctrl S menu file add menuitem save 添加到文件菜单 menuitem save addActionListener this menuitem saveas new JMenuItem 另存为 F12 创建另存为菜单项 menuitem saveas setAccelerator KeyStroke getKeyStroke KeyEvent VK F12 ActionEvent CTRL MASK 为另存为设置 快捷键 Ctrl F12 menu file add menuitem saveas 添加到文件菜单 menuitem saveas addActionListener this menu file addSeparator 在菜单中添加分隔线 将菜单项进行分组 menuitem exit new JMenuItem 退出 E 创建退出菜单项 menuitem exit setAccelerator KeyStroke getKeyStroke KeyEvent VK E ActionEvent ALT MASK 为退出设置快捷 键 Ctrl E menu file add menuitem exit 添加到文件菜单 menuitem exit addActionListener this 编辑菜单 JMenu menu edit new JMenu 编辑 创建编辑菜单 添加到菜单栏 menubar add menu edit menuitem cut new JMenuItem 剪切 X 创建剪切菜单项 并添加到编辑菜单 中 menuitem cut setAccelerator KeyStroke getKeyStroke KeyEvent VK X ActionEvent CTRL MASK 设置快捷键 Ctrl X menu edit add menuitem cut menuitem cut addActionListener this menuitem copy new JMenuItem 复制 C 在编辑菜单中添加复制菜单项 menuitem copy setAccelerator KeyStroke getKeyStroke KeyEvent VK C ActionEvent CTRL M ASK 设置快捷键 Ctrl C menu edit add menuitem copy menuitem copy addActionListener this menuitem paste new JMenuItem 粘贴 V 创建粘贴 menuitem paste setAccelerator KeyStroke getKeyStroke KeyEvent VK V ActionEvent CTRL MASK 设置快捷键 Ctrl V menu edit add menuitem paste menuitem paste addActionListener this menuitem delete new JMenuItem 删除 D 创建删除 menuitem delete setAccelerator KeyStroke getKeyStroke KeyEvent VK D ActionEvent CTRL MASK 设置快捷键 Ctrl D menu edit add menuitem delete menuitem delete addActionListener this menu edit addActionListener new ActionListener 注册事件监听 public void actionPerformed ActionEvent e checkMenuItemEnabled 设置剪切 复制 粘贴 删除等功 能的可用性 menu edit addSeparator 设置分隔线 格式菜单 JMenu menu style new JMenu 格式 在菜单栏中创建格式菜单 menubar add menu style JMenu menu ziti new JMenu 字体 添加字体项 checkbox cuti new JCheckBoxMenuItem 粗体 checkbox xieti new JCheckBoxMenuItem 斜体 checkbox cuti addItemListener this checkbox xieti addItemListener this menu ziti add checkbox cuti 粗体 斜体复选菜单添加到字体项 中 menu ziti add checkbox xieti menu style add menu ziti JMenuItem menu color new JMenuItem 颜色 添加颜色项 menu style add menu color menu color addActionListener this 帮助菜单 JMenu menu help new JMenu 帮助 创建帮助菜单 menubar add menu help JMenuItem menu look new JMenuItem 查看帮助 L 帮助菜单中添加帮助查 看项 menu help add menu look menu look addActionListener this JMenuItem menu about new JMenuItem 关于文本 A 添加关于文本项 menu help add menu about menu about addActionListener this dialog1 new JDialog this 关于文本 单击关于文本 弹出对话框 dialog1 setSize 480 150 label dialog new JLabel 作者 郑健健 制作时间 2013 1 8 JLabel CENTER dialog1 add label dialog dialog1 setDefaultCloseOperation HIDE ON CLOSE dialog2 new JDialog this 帮助文档 单击查看帮助 弹出对话框 dialog2 setSize 500 220 label dialog2 new JLabel 欢迎使用记事本 JLabel CENTER dialog2 add label dialog2 dialog2 setDefaultCloseOperation HIDE ON CLOSE 右键弹出菜单对象 popupmenu new JPopupMenu final cut new JMenuItem 剪切 X final cut setAccelerator KeyStroke getKeyStroke KeyEvent VK X InputEvent CTRL MASK 设置快捷键 Ctrl X popupmenu add final cut 添加剪切 final cut addActionListener this final copy new JMenuItem 复制 C final copy setAccelerator KeyStroke getKeyStroke KeyEvent VK C InputEvent CTRL MASK 设置快捷键 Ctrl C popupmenu add final copy 添加复制 final copy addActionListener this final paste new JMenuItem 粘贴 V final paste setAccelerator KeyStroke getKeyStroke KeyEvent VK V InputEvent CTRL MASK 设置快捷键 Ctrl V popupmenu add final paste 添加粘贴 final paste addActionListener this final delete new JMenuItem 删除 D final delete setAccelerator KeyStroke getKeyStroke KeyEvent VK D InputEvent CTRL MASK 设置快捷键 Ctrl D popupmenu add final delete 添加删除 final delete addActionListener this textarea add popupmenu 文本编辑区注册右键菜单事件 textarea addMouseListener new MouseAdapter public void mousePressed MouseEvent e checkForTriggerEvent e public void mouseReleased MouseEvent e checkForTriggerEvent e private void checkForTriggerEvent MouseEvent e if e isPopupTrigger popupmenu show e getComponent e getX e getY X Y 显示弹出菜单 在组件调用者的坐标空间中的位置 checkMenuItemEnabled 设置剪切 复制 粘贴 删除等功能的可用 性 textarea requestFocus 编辑区获取焦点 Container container getContentPane 创建滚动条 container setLayout new BorderLayout scroll new JScrollPane textarea JScrollPane VERTICAL SCROLLBAR AS NEEDED JScrollPane HORIZONTAL SCROLLBAR ALWAYS container add scroll BorderLayout CENTER private void addToolBar 设置工具栏 JToolBar jto new JToolBar 创建工具栏 jto setBackground Color black 设置工具栏背景颜色 JButton btziti new JButton 字体 jto add btziti 添加显示字体的按钮 String ob1 GraphicsEnvironment getLocalGraphicsEnvironment getAvailableFontFamilyNames 字体组合框数据项 String ob2 10 12 14 16 18 20 24 28 32 36 48 72 字号组 合框数据项 jco1 new JComboBox ob1 两个组合框 字体 字号大小 jco2 new JComboBox ob2 jco1 addActionListener this jco2 addActionListener this jto add jco1 工具栏里面添加 字体 字号组合框 jto add jco2 this add jto BorderLayout NORTH 添加工具栏 public void checkMenuItemEnabled String selectText textarea getSelectedText if selectText null 如果没有选择文本 剪切 删除 复制功能不可用 menuitem cut setEnabled false final cut setEnabled false menuitem copy setEnabled false final copy setEnabled false menuitem delete setEnabled false final delete setEnabled false else 选择了文本 剪切 删除 复制功能可用 menuitem cut setEnabled true final cut setEnabled true menuitem copy setEnabled true final copy setEnabled true menuitem delete setEnabled true final delete setEnabled true public String readFromFile 使用流从指定文本文件中读取字符 串 try FileReader fin new FileReader this file 以文件对象作为文件名来读取 文件 BufferedReader bin new BufferedReader fin 设置缓冲区 将读到的字符串 暂存在缓冲区 String aline lines do aline bin readLine 读取一行字符串 抵达文件结尾 时返回 null if aline null lines aline r n while aline null 文件不为空一直读取 bin close 读取后关闭文件 释放缓冲区 fin close return lines catch IOException ioex return null 出现异常 返回空 public boolean openDialog 执行 打开文件对话框 在打开文件对话框中 单击 打开 按钮时返回 true 单击 取消 按钮时返回 false FileDialog filedialog new FileDialog this 打开 FileDialog LOAD 创建打开文 件对话框 filedialog setVisible true 显示打 开文件对话框 if filedialog getDirectory null return true else return false 单 击 取消 按钮时 public void fileSaveAs JFileChooser jFileChooser new JFileChooser if JFileChooser APPROVE OPTION jFileChooser showSaveDialog this 打开保存 对话框 选择 yes or no fileName jFileChooser getSelectedFile txt 文件名称赋给 fileName fileSave fileName 不为空 调用保存函数 对文件进行另存 public void fileSave if fileName null fileSaveAs else if via if fileName length 0 try File saveFile new File fileName FileWriter fw new FileWriter saveFile fw write textarea getText 文本内容输出 fw close via true this setTitle fileName substring fileName lastIndexOf 1 this repaint catch Exception e JOptionPane showMessageDialog this 保存文件时出错 错 误 JOptionPane ERROR MESSAGE 提示错误 else fileSaveAs public void chooseColor Color bcolor textarea getForeground JColorChooser jColor new JColorChooser jColor setColor bcolor textarea setForeground JColorChooser showDialog textarea 选择颜色 bcolor public void actionPerformed ActionEvent e 单击事件处理程序 if e getActionCommand 新建 N 单击新建时 弹出提示框 文件是否 保存 textarea replaceRange 0 textarea getText length this setTitle 无标题 记事本 if e getActionCommand 打开 O 单击打开时 弹出对话框 if this openDialog 打开文件对话框并单击 打开 按钮时 执行 单击 取消 按钮时不执行 this setTitle this file getName 取得文件名 作为标题 this textarea setText this readFromFile 调用读函数 把文件读出 显示 到文本区 if e getActionCommand 保存 S 非第 1 次保存时 只保 存不需要打开保存文件对话框 this fileSave if e getActionCommand 保存 S if e getSource menuitem exit int exitChoose JOptionPane showConfirmDialog this 确定要退出吗 退出 提示 JOptionPane OK CANCEL OPTION if exitChoose JOptionPane OK OPTION System exit 0 else return if e getActionCommand 剪切 T textarea cut if e getActionCommand 复制 C textarea copy if e getActionCommand 粘贴 V textarea paste if e getActionCommand 删除 D textarea replaceRange textarea getSelectionStart textarea getSelect
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高等教育改革与产教融合发展的面临的问题、机遇与挑战
- 绿色储备技术在粮食产业中的应用
- 文化体制改革的现状与发展趋势
- 构建出版业融合发展的背景意义及必要性
- 抽水蓄能产业高质量发展风险管理评估
- 应用型卓越医学人才培养的面临的问题、机遇与挑战
- 地理世界洞察
- 创新融合生日派对
- 学术成就与职业规划
- 文化遗产的旅游革新
- 甘肃省陇南市多校2024-2025学年上学期期中检测七年级数学试卷(无答案)
- 中华人民共和国突发事件应对法培训课件
- 状元帽美术课件
- 小学数学课程体系介绍
- 脱硫检修工个人工作总结
- 内部工程项目承包合同范本
- 山西省2022年中考语文真题试卷(含答案)
- 甘肃省2024年中考生物试卷四套合卷【附答案】
- 民用无人机操控员执照(CAAC)备考复习题库-下部分(600题)
- 骨筋膜室综合征讲课
- 沪教牛津版三年级英语下册-Unit-3-Sounds-教学课件
评论
0/150
提交评论