(完整版)JAVA记事本源代码毕业设计
JAVA简单记事本程序(源代码)课程设计
辽宁工业大学JA V A程序设计课程设计(论文)题目: JAVA简单记事本程序院(系):软件学院专业班级:软件工程班学号:学生姓名:指导教师:教师职称:起止时间:程序设计专题(报告)任务及评语目录第1章课程设计的目的与要求 (4)1.1 课程设计目的 (4)1.2 课程设计的实验环境 (4)1.3 课程设计的预备知识 (4)1.4 课程设计要求 (4)第2章课程设计内容 (5)2.1课题描述 (5)2.2 系统设计 (5)2.2.1功能分析 (5)2.2.2面向对象设计 (5)2.2.3详细设计 (7)2.3程序实现 (10)2.3.1源码分析 (10)2.3.1运行结果 (14)第3章总结 (18)参考文献19第1章课程设计的目的与要求1.1 课程设计目的《JA V A程序设计》是计算机相关专业的必修专业基础课程,其实践性、应用性很强。
实践教学环节是必不可少的一个重要环节。
本课程的程序设计专题实际是计算机相关专业学生学习完《JAVA程序设计》课程后,进行的一次全面的综合训练,JA V A程序设计的设计目的是加深对理论教学内容的理解和掌握,使学生较系统地掌握程序设计及其在网络开发中的广泛应用,基本方法及技巧,为学生综合运用所学知识,利用软件工程为基础进行软件开发、并在实践应用方面打下一定基础。
1.2 课程设计的实验环境硬件要求能运行Windows 9.X操作系统的微机系统。
JAVA程序设计语言及相应的集成开发环境,J2SDK和ECLIPSE开发工具。
1.3 课程设计的预备知识熟悉JAVA语言及ECLIPSE开发工具。
1.4 课程设计要求按课程设计指导书提供的课题,要求学生在自行完成各个操作环节,并能实现且达到举一反三的目的,完成一个项目解决一类问题。
要求学生能够全面、深入理解和熟练掌握所学内容,并能够用其分析、设计和解答类似问题;对此能够较好地理解和掌握,能够进行简单分析和判断;能编写出具有良好风格的程序;掌握JA V A程序设计的基本技能和面向对象的概念和方法;了解多线程、安全和网络等编程技术。
java记事本课程设计论文附源码
java记事本课程设计论文附源码设计题目:记事本课程设计专业:电子信息科学及技术班级:电子1202班学号:20121210606姓名:王鹏2015 年7月XXX学院本科学生课程设计任务书摘要目前,很多新的技术领域都涉及到了Java语言,Java语言是面向对象编程,并涉及到网络、多线程等重要的基础知识,因此Java语言也是学习面向对象编程和网络编程的首选语言。
此次课程设计主要通过编写一个简单的记事本程序使我们深入理解并实践在本课程中所学的有关JAVA程序设计方面的语言基础、异常处理、事件处理、小应用程序以及图形用户界面设计等内容。
此简易记事本程序,使用Java程序编写,能够进行输入文字操作,并具有新建文件,打开文件,保存文件,退出,复制,粘贴,剪切等功能。
该程序主要用到了Java swing和事件监听等。
关键词记事本程序,java语言,Java swing目录摘要 (1)1 需求分析 (4)1.1需求分析 (4)1.2功能设计 (4)2 概要分析 (5)2.1概要设计思想 (5)2.2程序运行界面 (5)2.3程序模块说明 (5)2.4流程图 (6)2.5程序相关说明 (7)3 程序详细设计及分析 (8)3.1构建菜单栏及其下拉菜单 (8)3.2“文件”菜单操作的事件监听 (9)3.3“编辑”、“格式”等菜单的事件监听 (11)3.4异常处理 (13)4 测试分析 (15)5 源程序清单 (18)结论 (30)参考文献 (31)1 需求分析1.1需求分析现在网络上各种文档编辑器数不胜数,有EmEditor ,UEStudio ,GridinSoft Notepad ,Notepad++,win32pad ,SkimEdit,UniRed, xint 。
功能也是应有尽有,有能改变字体的,有能改变文字颜色的。
但是,这些软件又存在各种各样的瑕疵或问题:有的文件体积相对于一般文字编辑来说太大;有的功能太繁杂,使初级使用者一时难以掌握。
JAVA课程设计记事本(含代码)
Java程序课程设计任务书一、主要任务与目标1.掌握java语言,能够学会使用JCreator开发具体的软件。
2.使用所学知识开发一个基于Java的记事本。
二、主要内容与基本要求本程序在执行后应能处理文本编辑的一些基本功能。
根据记事本的特点,程序主要功能设计有:打开文件,读取文件中的文本信息:对原有文件可实行打开操作,并能读取文件中的内容,以方便对文件的查看,修改等任务。
显示、编辑修改文本信息:记事本对文字信息的主要处理功能。
可对相关信息进行复制,剪切,粘贴等操作。
实现新建、保存文本文件:新建文本可以使用户写入新的信息,文件保存后再次打开仍可获得原有信息。
三、计划进度1.12月28日-- 12月28日:搜集资料、查阅文献,确定选题2.12月29日-- 12月30日:需求分析,系统设计,系统功能模块完善3.1月1日-- 1月3日:编程实现系统、系统测试与修改4.1月4日-- 1月5日:课程设计报告撰写5.1月6日-- 1月7日:课程设计报告答辩四、主要参考文献(1)洪维恩何嘉 java 2面向对象程序设计. 中国铁道出版社,2004.2(2)耿祥义张跃平 Java大学实用教程(第2版).电子工业出版社,2008.5(3)孙全党王吴迪赵枫朝 Java程序设计应用教程,电子工业出版社,2006.2摘要随时着科学技术的快速发展,计算机程序设计早已成为了趋势,JAV A是计算机专业的核心课程,是计算机科学的算法理论基础和软件设计的技术基础。
JA VA是实践性很强的课程。
课程设计是加强实践能力的一个强有力手段。
要求掌握JA V A的应用、编写、上机调试的基本方法。
本课程设计就是主要利用java 语言编写的一个模仿windows 记事本的较简单的程序,它能实现打开文件,读取文件中的文本信息;显示、编辑修改文本信息;实现新建、打开、保存文本文件以及控制保存文本信息到指定的文件的一些基本功能。
该记事本是一个基于JCreator的应用程序。
《Java》课程设计》记事本
《Java课程设计》记事本课程设计报告书目录一、设计课题二、设计目的三、操作环境四、设计场所(机房号、机器号或自己机器)五、设计过程(设计内容及主要程序模块)六、本次设计中用到的课程知识点(列出主要知识点)七、设计过程中遇到的问题及解决办法八、程序清单五、设计过程(设计内容及主要模块,不少于3000字)1.设计要求1)界面设计2)功能实现(1)文件菜单:新建、打开、保存、另存为、退出等。
(2)其中新建菜单项可以新建一个记事本程序;打开菜单项可以打开一个用户指定的文本文件,并将其内容显示在记事本的文本区域;保存和另存为菜单项可分别实现文件的保存和另存为3)编辑菜单:复制、剪切和粘贴等4)帮助菜单:软件版本信5)右键弹出快捷菜单2.总体设计1)功能模块图:图一功能模块图2)功能描述1、打开记事本。
首先是标准型的记事本,拥有文件、编辑。
格式和帮助。
如图1所示:图1标准型记事本界面2、在标准型的记事本界面中,进行的新建一个本件名字叫新记事本。
如图2记事本文件帮助新建打开保存另存为退出复制剪切粘贴编辑关于记事本右键快捷格式字体颜色图2新建记事本功能3、用打开文件打开刚刚新建的新记事本。
如图三所示。
图3—打开文件4、点击退出即可退出,如图4所示:图4—退出记事本5、点击帮助可以看到有关记事本的相关信息,其中有作者名、版本、许可条款、隐私声明等必要信息。
如图5所示:图5—帮助相关信息6、右键可实现复制、粘贴、剪切、清除等常用功能,方便用户可以快捷方便的使用记事本。
如图6所示:图6—右键功能7、编辑也可实现制、粘贴、剪切、清除等常用功能,方便用户选择自己适合的方式,自由选择方便的快捷方式使用。
如图7:图7—编辑八、程序清单package test;import java.io.File;import java.io.*;import java.awt.event.*;import java.awt.Toolkit;import java.awt.*;import javax.swing.*;import javax.swing.filechooser.*;public class Notebook extends JFrame implements ActionListener,ItemListener{ //组件创建JButton b_save,b_close; //按钮JTextArea textArea; //文本框File tempFile; //文件JPanel jp; //面板JMenu file,edit,style,help; //菜单JMenuItemf_new,f_open,f_save,f_close,f_saveas,e_copy, e_paste,e_cut,e_clear,e_selectAll,e_find,e_rep lace,s_font,s_color,h_editor,h_help; //菜单条JMenuBar jmb;JScrollPane jsp; //滚动面板JPopupMenu popUpMenu = new JPopupMenu(); //右键弹出式菜单JLabel stateBar;//标签JLabel jl,jj;JFileChooser jfc = new JFileChooser(); //文件选择JMenuItemje_copy,je_paste,je_cut,je_clear,je_selectAll,je _find,je_replace; //弹出式菜单条public Notebook(){jfc.addChoosableFileFilter(new FileNameExtensionFilter("文本文件(*.txt)","txt"));jmb = new JMenuBar();textArea = new JTextArea();jsp = new JScrollPane(textArea);file = new JMenu("文件");edit = new JMenu("编辑");style = new JMenu("格式");help = new JMenu("帮助");je_copy = new JMenuItem("复制(C) ");je_paste = new JMenuItem("粘贴(P) ");je_cut = new JMenuItem("剪切(X) ");je_clear = new JMenuItem("清除(D) ");je_selectAll = new JMenuItem("全选(A) ");je_find = new JMenuItem("查找(F) ");je_replace = new JMenuItem("替换(R) ");je_copy.addActionListener(this); //给弹窗式的各组件添加监听器je_paste.addActionListener(this);je_cut.addActionListener(this);je_clear.addActionListener(this);je_selectAll.addActionListener(this);je_find.addActionListener(this);je_replace.addActionListener(this);//给界面上方的菜单条添加监听器f_new = new JMenuItem("新建(N)");f_new.setAccelerator(KeyStroke.getKeyS troke('N',InputEvent.CTRL_MASK,false)); //设置带修饰符快捷键f_new.addActionListener(this);f_open = new JMenuItem("打开(O)");f_open.setAccelerator(KeyStroke.getKey Stroke('O',InputEvent.CTRL_MASK,false));f_open.addActionListener(this);f_save = new JMenuItem("保存(S)");f_save.setAccelerator(KeyStroke.getKey Stroke('S',InputEvent.CTRL_MASK,false));f_save.addActionListener(this);f_saveas = new JMenuItem("另存为");f_saveas.setAccelerator(KeyStroke.getKe yStroke(KeyEvent.VK_S,InputEvent.CTRL_ MASK|InputEvent.SHIFT_MASK));f_saveas.addActionListener(this);f_close = new JMenuItem("退出(W)");f_close.setAccelerator(KeyStroke.getKey Stroke('W',InputEvent.CTRL_MASK,false));f_close.addActionListener(this);e_copy = new JMenuItem("复制(C)");e_copy.setAccelerator(KeyStroke.getKey Stroke('C',InputEvent.CTRL_MASK,false));e_copy.addActionListener(this);e_paste = new JMenuItem("粘贴(V)");e_paste.setAccelerator(KeyStroke.getKeyStro ke('V',InputEvent.CTRL_MASK,false));e_paste.addActionListener(this);e_cut = new JMenuItem("剪切(X)"); e_cut.setAccelerator(KeyStroke.getKeyStroke ('X',InputEvent.CTRL_MASK,false));e_cut.addActionListener(this);e_clear = new JMenuItem("清除(D)");e_clear.setAccelerator(KeyStroke.getKeyStrok e('D',InputEvent.CTRL_MASK,false));e_clear.addActionListener(this);e_selectAll = new JMenuItem("全选(A)");e_selectAll.setAccelerator(KeyStroke.getKey Stroke('A',InputEvent.CTRL_MASK,false));e_selectAll.addActionListener(this);e_find = new JMenuItem("查找(F)");e_find.setAccelerator(KeyStroke.getKeyStrok e('F',InputEvent.CTRL_MASK,false));e_copy.addActionListener(this);e_replace = new JMenuItem("替换(R)");e_replace.setAccelerator(KeyStroke.getK eyStroke('R',InputEvent.CTRL_MASK,false)) ;e_replace.addActionListener(this);s_font = new JMenuItem("字体(T)");s_font.setAccelerator(KeyStroke.getKeyStrok e('T',InputEvent.CTRL_MASK,false));s_font.addActionListener(this);s_color = new JMenuItem("颜色(C)...");s_color.setAccelerator(KeyStroke.getKeyStro ke(KeyEvent.VK_C,InputEvent.CTRL_MAS K | InputEvent.SHIFT_MASK));s_color.addActionListener(this);h_editor = new JMenuItem("关于记事本");h_editor.setAccelerator(KeyStroke.getKeyStro ke(KeyEvent.VK_E,InputEvent.CTRL_MAS K));h_editor.addActionListener(this);h_help = new JMenuItem("帮助信息(H)");h_help.setAccelerator(KeyStroke.getKeyStrok e(KeyEvent.VK_I,InputEvent.CTRL_MASK) );h_help.addActionListener(this);//添加右键弹出式菜单popUpMenu.add(je_copy);popUpMenu.add(je_paste);popUpMenu.add(je_cut);popUpMenu.add(je_clear);popUpMenu.addSeparator();popUpMenu.add(je_selectAll);popUpMenu.add(je_find);popUpMenu.add(je_replace);//编辑区鼠标事件,点击右键弹出"编辑"菜单textArea.addMouseListener(new MouseAdapter(){public void mouseReleased(MouseEvent e) {if(e.getButton() == MouseEvent.BUTTON3)popUpMenu.show(e.getComponent(), e.getX(), e.getY());} //e.getComponent()和textArea具有同等效果public void mouseClicked(MouseEvent e){if(e.getButton() == MouseEvent.BUTTON1)popUpMenu.setVisible(false);}});this.setJMenuBar(jmb);this.setTitle("记事本程序");file.add(f_new); //添加文件菜单组件file.add(f_open);file.addSeparator(); //加分隔线file.add(f_save);file.add(f_saveas);file.addSeparator();file.add(f_close);edit.add(e_copy); //添加编辑菜单组件edit.add(e_paste);edit.add(e_cut);edit.add(e_clear);edit.addSeparator();edit.add(e_selectAll);edit.add(e_find);edit.add(e_replace);style.addSeparator();style.add(s_font);style.add(s_color);jmb.add(file); //添加格式菜单组件jmb.add(edit);jmb.add(style);jmb.add(help);help.add(h_editor); //添加帮助菜单组件help.add(h_help);//textArea.setWrapStyleWord(true); //设置在单词过长的时候是否要把长单词移到下一行。
简单记事本java源码实例
简单记事本java源码实例本⽂实例讲述了简单记事本java实现代码。
分享给⼤家供⼤家参考。
具体如下:完整代码如下:复制代码代码如下:import java.awt.*;import java.io.*;import java.awt.datatransfer.*;import java.awt.event.*;public class Main extends Frame implements ActionListener {private static final long serialVersionUID = 1L;TextArea textArea = new TextArea();MenuBar menuBar = new MenuBar();Menu fileMenu = new Menu("File");MenuItem newItem = new MenuItem("New");MenuItem openItem = new MenuItem("Open");MenuItem saveItem = new MenuItem("Save");MenuItem saveAsItem = new MenuItem("Save As");MenuItem exitItem = new MenuItem("Exit");Menu editMenu = new Menu("Edit");MenuItem selectItem = new MenuItem("Select All");MenuItem copyItem = new MenuItem("Copy");MenuItem cutItem = new MenuItem("Cut");MenuItem pasteItem = new MenuItem("Paste");String fileName = null;Toolkit toolKit=Toolkit.getDefaultToolkit();Clipboard clipBoard=toolKit.getSystemClipboard();private FileDialog openFileDialog = new FileDialog(this,"Open File",FileDialog.LOAD);private FileDialog saveAsFileDialog = new FileDialog(this,"Save File As",FileDialog.SAVE); public Main(){setTitle("记事本程序-by Jackbase");setFont(new Font("Times New Roman",Font.PLAIN,12));setBackground(Color.white);setSize(400,300);fileMenu.add(newItem);fileMenu.add(openItem);fileMenu.addSeparator();fileMenu.add(saveItem);fileMenu.add(saveAsItem);fileMenu.addSeparator();fileMenu.add(exitItem);editMenu.add(selectItem);editMenu.addSeparator();editMenu.add(copyItem);editMenu.add(cutItem);editMenu.add(pasteItem);menuBar.add(fileMenu);menuBar.add(editMenu);setMenuBar(menuBar);add(textArea);addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});newItem.addActionListener(this);openItem.addActionListener(this);saveItem.addActionListener(this);saveAsItem.addActionListener(this);exitItem.addActionListener(this);selectItem.addActionListener(this);copyItem.addActionListener(this);cutItem.addActionListener(this);pasteItem.addActionListener(this);}public void actionPerformed(ActionEvent e) { //监听事件Object eventSource = e.getSource();if(eventSource == newItem){textArea.setText("");}else if(eventSource == openItem){openFileDialog.show();fileName = openFileDialog.getDirectory()+openFileDialog.getFile();if(fileName != null)readFile(fileName);}else if (eventSource == saveItem){if(fileName != null)writeFile(fileName);}else if(eventSource == saveAsItem){saveAsFileDialog.show();fileName = saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();if (fileName!= null)writeFile(fileName);}else if(eventSource == selectItem){textArea.selectAll();}else if(eventSource == copyItem){String text=textArea.getSelectedText();StringSelection selection=new StringSelection(text);clipBoard.setContents(selection,null);}else if(eventSource == cutItem){String text=textArea.getSelectedText();StringSelection selection=new StringSelection(text);clipBoard.setContents(selection,null);textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd()); }else if(eventSource == pasteItem){Transferable contents=clipBoard.getContents(this);if(contents==null) return;String text;text="";try{text=(String)contents.getTransferData(DataFlavor.stringFlavor);}catch(Exception exception){}textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd()); }else if(eventSource == exitItem){System.exit(0);}}public void readFile(String fileName){ //读取⽂件处理try{File file = new File(fileName);FileReader readIn = new FileReader(file);int size = (int)file.length();int charsRead = 0;char[] content = new char[size];while(readIn.ready())charsRead += readIn.read(content, charsRead, size - charsRead);readIn.close();textArea.setText(new String(content, 0, charsRead)); }catch(IOException e){System.out.println("Error opening file");}}public void writeFile(String fileName){ //写⼊⽂件处理 try{File file = new File (fileName);FileWriter writeOut = new FileWriter(file);writeOut.write(textArea.getText());writeOut.close();}catch(IOException e){System.out.println("Error writing file");}}@SuppressWarnings("deprecation")public static void main(String[] args){Frame frame = new Main(); //创建对象frame.show(); //是对象显⽰}}运⾏结果如下图所⽰:希望本⽂所述对⼤家的java程序设计有所帮助。
JAVA课程设计---设计一个记事本
课程设计(大作业)报告课程名称:面向对象程序设计设计题目:设计一个记事本院系:信息技术学院课程设计(大作业)任务书课程设计(大作业)报告一、需求分析如今有关记事本的软件种类繁多。
功能也是应有尽有,但是,软件并不是十全十美的:有的文件体积相对于一般文字编辑来说太大;有的功能太繁杂,使初级使用者一时难以掌握。
仅从日常应用方面来说,实现输入、保存、编辑等简单实用的功能就够了。
本程序的设计就是依照这样一种需要设计了一个简单的记事本程序。
本程序要构建的记事本程序参照了Windows操作系统的记事本工具,具体其功能有以下几个方面:(1)菜单中有“文件”,“编辑”,“关于”三个主菜单。
(2)“文件”有“新建”“打开”“保存”“退出”四个子菜单:分别用于新建文件,打开文件,保存文件,退出记事本。
(3)“编辑”中有“剪切”“复制”“粘贴”“颜色”三个字菜单:用于剪切文字,复制文字,粘贴文字。
增加了“颜色”子菜单来对文字的颜色进行设置。
(4)“关于”菜单中有关于记事本程序的制作日期,作者等信息。
二、系统总体设计在本程序中,设计了一个菜单栏和文本内容区。
设计过程中用到了JScrollPane,JMenu,JTextPane,JFileChooser等。
JMenu用来制作菜单,菜单包括“文件”“编辑”“关于”。
JTextPane主要用来输入文本。
为了加入颜色的设置,所以加了一个JColorChooser组件。
1.系统功能结构图设计2.系统文件结构图设计3.系统编辑结构图设计UML图:三、软件平台1、使用JDK配置环境在JAVA编辑器下进行编辑JAVA源程序2、EditPlus 3四、模块的分析与实现构建菜单以及下拉菜单:JMenu[] menus=new JMenu[]{new JMenu("文件"),new JMenu("编辑"),new JMenu("关于")};J MenuItem optionofmenu[][]=new JMenuItem[][]{{new JMenuItem("新建"),new JMenuItem("打开"),new JMenuItem("保存"),new JMenuItem("退出")},{new JMenuItem("复制"),new JMenuItem("剪切"),new JMenuItem("粘贴"),new JMenuItem("颜色")},{new JMenuItem("关于")}};文件菜单事件监听:ActionListener action=new ActionListener(){public void actionPerformed(ActionEvent e){String name = e.getActionCommand();JMenuItem MI=(JMenuItem)e.getSource();if("新建".equals(name)){content.setText("");file=null;}else if("打开".equals(name)){if(file !=null)openfile.setSelectedFile(file);int returnVal=openfile.showOpenDialog(mynotepad.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=openfile.getSelectedFile();unfold();}}else if("保存".equals(name)){if(file!=null) openfile.setSelectedFile(file);int returnVal=openfile.showSaveDialog(mynotepad.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=openfile.getSelectedFile();saving();}}else if("退出".equals(name)){mynotepad f=new mynotepad();int s=JOptionPane.showConfirmDialog(f,"退出?","退出",JOptionPane.YES_NO_OPTION);编辑菜单的事件监听:else if("剪切".equals(name)){content.cut();}else if("复制".equals(name)){content.copy();}else if("粘贴".equals(name)){content.paste();}else if("颜色".equals(name)){color=JColorChooser.showDialog(mynotepad.this,"",color);content.setForeground(color);异常处理:void saving(){try{FileWriter Writef=new FileWriter(file);Writef.write(content.getText());Writef.close();}Catch (Exception e){e.printStackTrace();}}关于部分:void initAboutDialog(){about.setLayout(new GridLayout(3,1));about.getContentPane().setBackground(Color.white);about.getContentPane().add(new JLabel("简单记事本程序设计"));about.getContentPane().add(new JLabel("11级计算机1班"));about.getContentPane().add(new JLabel("制作者:彭磊"));about.getContentPane().add(new JLabel("2012年12月27日"));about.setModal(true);about.setSize(200,100);about.setLocation(250,150);}五、调试分析各部分调试功能如图所示:六、总结在完成记事本合格程序中,基本上实现了我在需求分析时所预期的功能。
(最新版)Java毕业课程设计报告记事本程序
Java课程设计报告题目:简单记事本程序的设计年级专业:i ____ 学号: _________________________ 学生姓名: ______________________ 指导老师: ______________________完成时间:2010年6月8日目录摘要 (1)前言 (2)1需求分析 (2)1.1需求分析 (2)1.2功能设计 (3)2.概要设计 (3)2.1程序设计思路 (3)2.2程序运行界面 (3)2.3模块说明图 (4)2.4 程序流程图 (5)2.5程序相关说明 (6)3.程序详细设计与分析 (6)3.1.初始化组件 (6)3.2.构建菜单栏及其下拉菜单 (6)3.3.“文件”菜单的事件监听 (7)3.4.“编辑”菜单的事件监听 (8)3.5.异常处理 (9)4•测试分析 (10)5•源程序清单 (12)6•课程设计总结 (17)参考文献 (17)简单记事本程序的设计摘要该报告中的程序是使用Java 程序编写,能够进行输入文字操作,并具有新建文件,打开文件,保存文件,退出,复制,粘贴,剪切等功能,不同之处是本程序能够对文字的颜色进行设置。
该程序主要用到了Java swing 和事件监听等。
关键字:记事本程序;Java 语言;Jcreator.、八、一前言Java的前身是Oak,它一开始只是被应用于消费性电子产品中。
后来它的开发者们发现它还可以被用于更大范围的In ternet 上。
1995年,Java语言的名字从Oak编程了Java。
1997 年J2SE1.1 发布。
1998 年J2SE1.2 发布,标志Java2 的诞生。
十多年来, Java 编程语言及平台成功地运用在网络计算及移动等各个领域。
Java的体系结构由Java语言、Javaclass 、Java API 、Java 虚拟机组成。
它具有简单、面向对象、健壮、安全、结构中立、可移植和高效能等众多优点。
Java课程设计报告(记事本)
Java课程设计报告专业:班级:姓名:学号:系部:目录目录 (2)正文 (3)一、设计内容和要求 (3)二、设计思路 (4)三、具体实现 ............................................................ 错误!未定义书签。
四、运行调试与分析 .............................................. 错误!未定义书签。
3五、设计体会小结 (21)正文一、设计内容和要求Java文档编辑器是一个允许修改DefaultStyledDocument对象的应用程序。
这些对象用于管理Swing文本组件(例如JTextPane和JTextArea)的内容。
JTextPane用于显示DefaultStyledDocument的内容。
通过改变与DefaultStyledDocument相关联的Style对象,能够改变DefaultStyledDocument在JTextPane中的显示方式。
此应用程序也能够将DefaultStyledDocument存储到磁盘上和读入以前存储的DefaultStyledDocument。
读、写、操纵DefaultStyledDocument的所有选项都可以通过使用菜单项来实现。
当这些菜单项被选中时,它们生成事件。
使用ActionListener, ItemListener, UndoableEditListener和MenuDragMouseListener对象来处理这些事件。
要求通过课程设计,使学生将掌握的Java语言基本知识应用于实践中,在实际练习中加深对Java语言各方面的知识和概念的理解,掌Java程序设计中编程方法、编程思想,深入理解Java语言在实际中的应用。
要求学生动手上机编程,掌握Java程序设计思想和设计方法,设计实现一个简单Java文档编辑器,该编辑器由打开文件、新建文件、保存、复制、剪切、粘贴、撤销、删除等基本文本操作功能组件组成,有能力和时间的同学还可以设计实现如查找、时间、设置字体颜色、格式等功能,右键菜单响应,快捷键等功能,进一步加强自己的编程能力和编程思想。
Java记事本源代码(完整)
/*** 作品:记事本* 作者:**** 功能:简单的文字编辑*/import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;class NotePad extends JFrame{private JMenuBar menuBar;private JMenu fielMenu,editMenu,formMenu,aboutMenu;private JMenuItemnewMenuItem,openMenuItem,saveMenuItem,exitMenuItem;private JMenuItemcutMenuItem,copyMenuItem,pasteMenuItem,foundItem,replaceItem,s electAll;private JMenuItem font,about;private JTextArea textArea;private JFrame foundFrame,replaceFrame;private JCheckBoxMenuItem wrapline;private JTextField textField1=new JTextField(15);private JTextField textField2=new JTextField(15);private JButton startButton,replaceButton,reallButton;int start=0;String value;File file=null;JFileChooser fileChooser=new JFileChooser();boolean wrap=false;public NotePad(){//创建文本域textArea=new JTextArea();add(new JScrollPane(textArea),BorderLayout.CENTER);//创建文件菜单及文件菜单项fielMenu=new JMenu("文件");fielMenu.setFont(new Font("微软雅黑",0,15));newMenuItem=new JMenuItem("新建",newImageIcon("icons\\new24.gif"));newMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent. VK_N,InputEvent.CTRL_MASK));newMenuItem.addActionListener(listener);openMenuItem=new JMenuItem("打开",newImageIcon("icons\\open24.gif"));openMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_O,InputEvent.CTRL_MASK));openMenuItem.addActionListener(listener);saveMenuItem=new JMenuItem("保存",newImageIcon("icons\\save.gif"));saveMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_S,InputEvent.CTRL_MASK));saveMenuItem.addActionListener(listener);exitMenuItem=new JMenuItem("退出",newImageIcon("icons\\exit24.gif"));exitMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_E,InputEvent.CTRL_MASK));exitMenuItem.addActionListener(listener);//创建编辑菜单及菜单项editMenu=new JMenu("编辑");editMenu.setFont(new Font("微软雅黑",0,15));cutMenuItem=new JMenuItem("剪切",newImageIcon("icons\\cut24.gif"));cutMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent. VK_X,InputEvent.CTRL_MASK));cutMenuItem.addActionListener(listener);copyMenuItem=new JMenuItem("复制",newImageIcon("icons\\copy24.gif"));copyMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_C,InputEvent.CTRL_MASK));copyMenuItem.addActionListener(listener);pasteMenuItem=new JMenuItem("粘贴",newImageIcon("icons\\paste24.gif"));pasteMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEven t.VK_V,InputEvent.CTRL_MASK));pasteMenuItem.addActionListener(listener);foundItem=new JMenuItem("查找");foundItem.setFont(new Font("微软雅黑",Font.BOLD,13));foundItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK _F,InputEvent.CTRL_MASK));foundItem.addActionListener(listener);replaceItem=new JMenuItem("替换");replaceItem.setFont(new Font("微软雅黑",Font.BOLD,13));replaceItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent. VK_R,InputEvent.CTRL_MASK));replaceItem.addActionListener(listener);selectAll=new JMenuItem("全选");selectAll.setFont(new Font("微软雅黑",Font.BOLD,13));selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK _A,InputEvent.CTRL_MASK));selectAll.addActionListener(listener);//创建格式菜单及菜单项formMenu=new JMenu("格式");formMenu.setFont(new Font("微软雅黑",0,15));wrapline=new JCheckBoxMenuItem("自动换行");wrapline.setFont(new Font("微软雅黑",Font.BOLD,13));wrapline.addActionListener(listener);wrapline.addChangeListener(new ChangeListener() {public void stateChanged(ChangeEvent e) {if(wrapline.isSelected()){textArea.setLineWrap(true);}elsetextArea.setLineWrap(false);}});font=new JMenuItem("字体");font.setFont(new Font("微软雅黑",Font.BOLD,13)); font.addActionListener(listener);//创建关于菜单aboutMenu=new JMenu("关于");aboutMenu.setFont(new Font("微软雅黑",0,15)); about=new JMenuItem("记事本……");about.setFont(new Font("微软雅黑",Font.BOLD,13)); about.addActionListener(listener);//添加文件菜单项fielMenu.add(newMenuItem);fielMenu.add(openMenuItem);fielMenu.add(saveMenuItem);fielMenu.addSeparator();fielMenu.add(exitMenuItem);//添加编辑菜单项editMenu.add(cutMenuItem);editMenu.add(copyMenuItem);editMenu.add(pasteMenuItem);editMenu.add(foundItem);editMenu.add(replaceItem);editMenu.addSeparator();editMenu.add(selectAll);//添加格式菜单项formMenu.add(wrapline);formMenu.add(font);//添加关于菜单项aboutMenu.add(about);//添加菜单menuBar=new JMenuBar();menuBar.add(fielMenu);menuBar.add(editMenu);menuBar.add(formMenu);menuBar.add(aboutMenu);setJMenuBar(menuBar);//创建两个框架,用作查找和替换foundFrame=new JFrame();replaceFrame=new JFrame();//创建两个文本框textField1=new JTextField(15);textField2=new JTextField(15);startButton=new JButton("开始");startButton.addActionListener(listener);replaceButton=new JButton("替换为");replaceButton.addActionListener(listener);reallButton=new JButton("全部替换");reallButton.addActionListener(listener);}//创建菜单项事件监听器ActionListener listener=new ActionListener() {public void actionPerformed(ActionEvent e) {String name=e.getActionCommand();if(e.getSource() instanceof JMenuItem){if("新建".equals(name)){textArea.setText("");file=null;}if("打开".equals(name)){if(file!=null){fileChooser.setSelectedFile(file);}intreturnVal=fileChooser.showOpenDialog(NotePad.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=fileChooser.getSelectedFile();}try{FileReader reader=new FileReader(file);int len=(int)file.length();char[] array=new char[len];reader.read(array,0,len);reader.close();textArea.setText(new String(array));}catch(Exception e_open){e_open.printStackTrace();}}}if("保存".equals(name)){if(file!=null){fileChooser.setSelectedFile(file);}intreturnVal=fileChooser.showSaveDialog(NotePad.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=fileChooser.getSelectedFile();}try{FileWriter writer=new FileWriter(file);writer.write(textArea.getText());writer.close();}catch (Exception e_save) {e_save.getStackTrace();}}if("退出".equals(name)){System.exit(0);}if("剪切".equals(name)){textArea.cut();}if("复制".equals(name)){textArea.copy();}if("粘贴".equals(name)){textArea.paste();}if("查找".equals(name)){value=textArea.getText();foundFrame.add(textField1,BorderLayout.CENTER);foundFrame.add(startButton,BorderLayout.SOUTH);foundFrame.setLocation(300,300);foundFrame.setTitle("查找");foundFrame.pack();foundFrame.setVisible(true);foundFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );}if("替换".equals(name)){value=textArea.getText();JLabel label1=new JLabel("查找内容:");JLabel label2=new JLabel("替换为:");JPanel panel1=new JPanel();panel1.setLayout(new GridLayout(2,2));JPanel panel2=new JPanel();panel2.setLayout(new GridLayout(1,3));replaceFrame.add(panel1,BorderLayout.NORTH);replaceFrame.add(panel2,BorderLayout.CENTER);panel1.add(label1);panel1.add(textField1);panel1.add(label2);panel1.add(textField2);panel2.add(startButton);panel2.add(replaceButton);panel2.add(reallButton);replaceFrame.setTitle("替换");replaceFrame.setLocation(300,300);replaceFrame.pack();replaceFrame.setVisible(true);replaceFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLO SE);}if("开始".equals(name)||"下一个".equals(name)){String temp=textField1.getText();int s=value.indexOf(temp,start);if(value.indexOf(temp,start)!=-1){textArea.setSelectionStart(s);textArea.setSelectionEnd(s+temp.length());textArea.setSelectedTextColor(Color.GREEN);start=s+1;startButton.setText("下一个");}else {JOptionPane.showMessageDialog(foundFrame, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif"));foundFrame.dispose();}}if("替换为".equals(name)){String temp=textField1.getText();int s=value.indexOf(temp,start);if(value.indexOf(temp,start)!=-1){textArea.setSelectionStart(s);textArea.setSelectionEnd(s+temp.length());textArea.setSelectedTextColor(Color.GREEN);start=s+1;textArea.replaceSelection(textField2.getText());}else {JOptionPane.showMessageDialog(foundFrame, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif"));foundFrame.dispose();}}if("全部替换".equals(name)){String temp=textArea.getText();temp=temp.replaceAll(textField1.getText(),textField2.getTex t());textArea.setText(temp);}if("全选".equals(name)){textArea.selectAll();}if("字体".equals(name)){FontDialog fontDialog=newFontDialog(NotePad.this);fontDialog.setVisible(true);if(textArea.getFont()!=fontDialog.getFont()){textArea.setFont(fontDialog.getFont());}}if("记事本……".equals(name)){AboutDialog aboutDialog=newAboutDialog(NotePad.this);aboutDialog.setVisible(true);}}};//创建字体设置对话面板,并添加相应事件监听器class FontDialog extends JDialog implements ItemListener, ActionListener, WindowListener{public JCheckBox Bold=new JCheckBox("Bold",false);public JCheckBox Italic=new JCheckBox("Italic",false);public List Size,Name;public int FontName;public int FontStyle;public int FontSize;public JButton OK=new JButton("OK");public JButton Cancel=new JButton("Cancel");public JTextArea Text=new JTextArea("字体预览文本域\n0123456789\nAaBbCcXxYyZz");public FontDialog(JFrame owner) {super(owner,"字体设置",true);GraphicsEnvironmentg=GraphicsEnvironment.getLocalGraphicsEnvironment();String name[]=g.getAvailableFontFamilyNames();Name=new List();Size=new List();FontName=0;FontStyle=0;FontSize=8;int i=0;Name.add("Default Value");for(i=0;i<name.length;i++)Name.add(name[i]);for(i=8;i<257;i++)Size.add(String.valueOf(i));this.setLayout(null);this.setBounds(250,200,480, 306);this.setResizable(false);OK.setFocusable(false);Cancel.setFocusable(false);Bold.setFocusable(false);Italic.setFocusable(false);Name.setFocusable(false);Size.setFocusable(false);Name.setBounds(10, 10, 212, 259);this.add(Name);Bold.setBounds(314, 10, 64, 22);this.add(Bold);Italic.setBounds(388, 10, 64, 22);this.add(Italic);Size.setBounds(232, 10, 64, 259);this.add(Size);Text.setBounds(306, 40, 157, 157);this.add(Text);OK.setBounds(306, 243, 74, 26);this.add(OK);Cancel.setBounds(390, 243, 74, 26);this.add(Cancel);Name.select(FontName);Size.select(FontSize);Text.setFont(getFont());Name.addItemListener(this);Size.addItemListener(this);Bold.addItemListener(this);Italic.addItemListener(this);OK.addActionListener(this);Cancel.addActionListener(this);this.addWindowListener(this);}public void itemStateChanged(ItemEvent e) {Text.setFont(getFont());}public void actionPerformed(ActionEvent e) {if(e.getSource()==OK){FontName=Name.getSelectedIndex();FontStyle=getStyle();FontSize=Size.getSelectedIndex();this.setVisible(false);}else cancel();}public void windowClosing(WindowEvent e) {cancel();}public Font getFont(){if(Name.getSelectedIndex()==0) return new Font("新宋体",getStyle(),Size.getSelectedIndex()+8);else return newFont(Name.getSelectedItem(),getStyle(),Size.getSelectedIndex() +8);}public void cancel(){Name.select(FontName);Size.select(FontSize);setStyle();Text.setFont(getFont());this.setVisible(false);}public void setStyle(){if(FontStyle==0 || FontStyle==2)Bold.setSelected(false);else Bold.setSelected(true);if(FontStyle==0 || FontStyle==1)Italic.setSelected(false);else Italic.setSelected(true);}public int getStyle(){int bold=0,italic=0;if(Bold.isSelected()) bold=1;if(Italic.isSelected()) italic=1;return bold+italic*2;}public void windowActivated(WindowEvent arg0) {}public void windowClosed(WindowEvent arg0) {}public void windowDeactivated(WindowEvent arg0) {}public void windowDeiconified(WindowEvent arg0) {}public void windowIconified(WindowEvent arg0) {}public void windowOpened(WindowEvent arg0) {} }//创建关于对话框class AboutDialog extends JDialog implements ActionListener{ public JButton OK,Icon;public JLabel Name,Version,Author,Java;public JPanel Panel;AboutDialog(JFrame owner) {super(owner,"关于",true);OK=new JButton("OK");Icon=new JButton(new ImageIcon("icons\\edit.gif"));Name=new JLabel("Notepad");Version=new JLabel("Version 1.0");Java=new JLabel("JRE Version 6.0");Author=new JLabel("Copyright (c) 11-5-2012 By Jianmin Chen");Panel=new JPanel();Color c=new Color(0,95,191);Name.setForeground(c);Version.setForeground(c);Java.setForeground(c);Author.setForeground(c);Panel.setBackground(Color.white);OK.setFocusable(false);this.setBounds(250,200,280, 180);this.setResizable(false);this.setLayout(null);Panel.setLayout(null);OK.addActionListener(this);Icon.setFocusable(false);Icon.setBorderPainted(false);Author.setFont(new Font(null,Font.PLAIN,11));Panel.add(Icon);Panel.add(Name);Panel.add(Version);Panel.add(Author);Panel.add(Java);this.add(Panel);this.add(OK);Panel.setBounds(0, 0, 280, 100);OK.setBounds(180, 114, 72, 26);Name.setBounds(80, 10, 160, 20);Version.setBounds(80, 27, 160, 20);Author.setBounds(15, 70, 250, 20);Java.setBounds(80, 44, 160, 20);Icon.setBounds(16, 14, 48, 48);}public void actionPerformed(ActionEvent e) { this.setVisible(false);}}}//创建记事本public class ChenJianmin {public static void main(String[] args){EventQueue.invokeLater(new Runnable() {public void run() {NotePad notePad=new NotePad();notePad.setTitle("记事本");notePad.setVisible(true);notePad.setBounds(100,100,800,600);notePad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}});}}。
JAVA--简单记事本源代码
JAVA--简单记事本源代码import javax.swing.JFrame;import javax.swing.JTextArea;import java.awt.*;import java.awt.event.*;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import javax.swing.*;import javax.swing.plaf.FileChooserUI;public class notepad extends JFrame implements ActionListener{//定义所需要的组件JTextArea jta=null;JMenuBar jmb=null;JMenu jm=null;JMenuItem jmi1=null;JMenuItem jmi2=null;public static void main(String[]args){new notepad();}public notepad(){//把组件添加到窗体上jta=new JTextArea();jmb=new JMenuBar();this.add(jta);this.setJMenuBar(jmb);//设置窗体属性this.setTitle("简易记事本");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setSize(400, 300);this.setVisible(true);//把菜单添加进菜单条jm=new JMenu("⽂件");jm.setMnemonic('f');jmb.add(jm);//把⼦菜单加⼊⾄菜单jmi1=new JMenuItem("打开");jmi2=new JMenuItem("保存");jmi1.setActionCommand("open");jmi2.setActionCommand("save");jm.add(jmi1);jm.add(jmi2);//为两个字菜单注册事件监听jmi1.addActionListener(this);jmi2.addActionListener(this);}public void actionPerformed(ActionEvent e){//对点击了打开进⾏处理if(e.getActionCommand().equals("open")){//创建⼀个⽂件选择组件JFileChooser jfc1=new JFileChooser();//设置⽂件选择器的名字jfc1.setDialogTitle("请选择⽂件....");//设置⽂件选择组件的类型(打开类型)jfc1.showOpenDialog(null);//显⽰该组件jfc1.setVisible(true);//获取选择⽂件的绝对路径String s;s=jfc1.getSelectedFile().getAbsolutePath();//将该⽂件显⽰到记事本上BufferedReader br=null;FileReader fr=null;try{//创建⼀个带缓冲的⽂件读取对象fr=new FileReader(s);br=new BufferedReader(fr);String text="";String m=null;//循环读取⽂件while((m=br.readLine())!=null){text+=m+"\r\n";}//将读取的结果打印到记事本上⾯this.jta.setText(text);}catch(Exception e1){e1.printStackTrace();}finally{//关掉打开的⽂件try{br.close();fr.close();}catch(Exception e2){e2.printStackTrace();}}}else if(e.getActionCommand().equals("save")){//创建⼀个⽂件选择组件JFileChooser jfc=new JFileChooser();//设置⽂件选择的名称jfc.setDialogTitle("另存为");//设置⽂件选择组件的类型(保存类型)jfc.showSaveDialog(null);//显⽰该组件jfc.setVisible(true);//获取选择⽂件的绝对路径String filename;filename=jfc.getSelectedFile().getAbsolutePath(); //将记事本内的⽂本保存⾄该路径BufferedWriter bw=null;FileWriter fw=null;try{//创建⽂件输出⽂件fw=new FileWriter(filename);bw=new BufferedWriter(fw);//获取⽂本String outtext="";outtext=this.jta.getText();//输出⽂本fw.write(outtext);}catch(Exception e2){e2.printStackTrace();}finally{//关闭打开的输出⽂件try{bw.close();fw.close();}catch(Exception e3){e3.printStackTrace();}}}}}。
java简单记事本源代码
java简单记事本源代码Java简单记事本源代码一、引言记事本是我们日常生活中常用的工具之一,它可以帮助我们记录重要的事项和备忘录。
在本文中,我们将介绍一个简单的记事本的Java源代码,它具有基本的记事功能,包括新建、打开、保存和关闭等操作。
二、界面设计记事本的界面应该简洁明了,用户可以通过菜单栏或快捷键来完成各个功能。
我们可以使用Swing库来实现记事本的图形界面。
在界面上,我们可以添加一个菜单栏,包括文件、编辑、格式和帮助等几个选项。
其中,文件菜单下应该包括新建、打开、保存和关闭选项。
三、实现新建功能新建功能是记事本的基本功能之一,它可以创建一个新的文档供用户编辑。
在Java中,我们可以使用JTextArea组件来实现文本编辑区域。
当用户选择新建功能时,我们可以清空文本编辑区域,让用户可以开始编辑新的内容。
四、实现打开功能打开功能允许用户从计算机中选择一个已有的文档进行编辑。
在Java中,我们可以使用JFileChooser组件来实现文件选择对话框。
当用户选择打开功能时,我们可以弹出一个文件选择对话框,让用户选择一个文档进行打开。
然后,我们可以读取文档内容,并显示在文本编辑区域中供用户编辑。
五、实现保存功能保存功能可以将用户编辑完成的文档保存到计算机中。
在Java中,我们可以使用JFileChooser组件来实现文件保存对话框。
当用户选择保存功能时,我们可以弹出一个文件保存对话框,让用户选择保存的路径和文件名。
然后,我们可以将文本编辑区域中的内容保存到指定的文件中。
六、实现关闭功能关闭功能是记事本的重要功能之一,它可以关闭当前打开的文档并退出记事本程序。
在Java中,我们可以使用System.exit(0)语句来实现程序的退出。
当用户选择关闭功能时,我们可以先判断当前文档是否已保存,如果未保存,则弹出提示框询问用户是否保存。
如果用户选择保存,则执行保存功能;如果用户选择不保存,则直接退出程序。
Java完整简单记事本源代码
Java完整简单记事本源代码版权所有请勿抄袭AboutDialog.javapackage notepad;import java.awt.Color;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class AboutDialog implements ActionListener{public JDialog Dialog;public JButton OK,Icon;public JLabel Name,Version,Author,Java;public JPanel Panel;AboutDialog(JFrame notepad, int x, int y) {Dialog=new JDialog(notepad,"About Notepad...",true);OK=new JButton("OK");Icon=new JButton(new ImageIcon("Notepad.jpg"));Name=new JLabel("Notepad");Version=new JLabel("Version 1.0");Java=new JLabel("JRE Version 6.0");Author=new JLabel("Copyright (c) 2011 Sky. No rights reserved.");Panel=new JPanel();Color c=new Color(0,95,191);Name.setForeground(c);Version.setForeground(c);Java.setForeground(c);Panel.setBackground(Color.WHITE);OK.setFocusable(false);Dialog.setSize(280, 180);Dialog.setLocation(x, y);Dialog.setResizable(false);Dialog.setLayout(null);Panel.setLayout(null);OK.addActionListener(this);Icon.setFocusable(false);Icon.setBorderPainted(false);Author.setFont(new Font(null,Font.PLAIN,11)); Panel.add(Icon);Panel.add(Name);Panel.add(Version);Panel.add(Author);Panel.add(Java);Dialog.add(Panel);Dialog.add(OK);Panel.setBounds(0, 0, 280, 100);OK.setBounds(180, 114, 72, 26);Name.setBounds(80, 10, 160, 20); Version.setBounds(80, 27, 160, 20); Author.setBounds(15, 70, 250, 20);Java.setBounds(80, 44, 160, 20);Icon.setBounds(16, 14, 48, 48);}public void actionPerformed(ActionEvent e) { Dialog.setVisible(false);}}ColorDialog.javapackage notepad;import java.awt.Color;import ponent;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;public class ColorDialog implements ActionListener, WindowListener{ public JDialog Dialog;public JLabel NFL,NBL,SFL,SBL;public JTextArea Normal,Selected;public JButton NFB,NBB,SFB,SBB,OK,Cancel,Reset;public Color NFC,NBC,SFC,SBC;public ColorChooser Chooser;public ColorDialog(JFrame notepad, int x, int y){NFC=new Color(0,0,0);NBC=new Color(249,249,251);SFC=new Color(0,0,0);SBC=new Color(191,207,223);Dialog=new JDialog(notepad,"Color...",true);NFL=new JLabel("Normal Foreground:");NBL=new JLabel("Normal Background:");SFL=new JLabel("Selected Foreground:");SBL=new JLabel("Selected Background:");Normal=new JTextArea("\n Normal 正常");Selected=new JTextArea("\n Selected 选中 ");NFB=new JButton("");NBB=new JButton("");SFB=new JButton("");SBB=new JButton("");OK=new JButton("OK");Cancel=new JButton("Cancel");Reset=new JButton("Reset");Chooser=new ColorChooser(Dialog, x+65, y-15);Normal.setEditable(false);Normal.setFocusable(false);Normal.setFont(new Font("新宋体", 0, 16)); Normal.setForeground(NFC);Normal.setBackground(NBC); Selected.setEditable(false);Selected.setFocusable(false); Selected.setFont(Normal.getFont()); Selected.setForeground(SFC); Selected.setBackground(SBC);NFB.setBackground(NFC);NBB.setBackground(NBC);SFB.setBackground(SFC);SBB.setBackground(SBC);Dialog.setLayout(null);Dialog.setLocation(x, y);Dialog.setSize(410, 220);Dialog.setResizable(false);Reset.setFocusable(false);OK.setFocusable(false);Cancel.setFocusable(false);Dialog.add(Normal);Dialog.add(Selected);Dialog.add(NFL);Dialog.add(NBL);Dialog.add(SFL);Dialog.add(SBL);Dialog.add(SBB);Dialog.add(SFB);Dialog.add(NBB);Dialog.add(NFB);Dialog.add(OK);Dialog.add(Cancel);Dialog.add(Reset);SBB.setBounds(144, 100, 60, 22);SFB.setBounds(144, 70, 60, 22);NBB.setBounds(144, 40, 60, 22);NFB.setBounds(144, 10, 60, 22);NFL.setBounds(10, 10, 130, 22);NBL.setBounds(10, 40, 130, 22);SFL.setBounds(10, 70, 130, 22);SBL.setBounds(10, 100, 130, 22);Normal.setBounds(220, 10, 174, 56);Selected.setBounds(220, 66, 174, 56);Reset.setBounds(10, 160, 74, 24);OK.setBounds(236, 160, 74, 24);Cancel.setBounds(320, 160, 74, 24);Dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON _CLOSE);Dialog.addWindowListener(this);NFB.addActionListener(this);NBB.addActionListener(this);SFB.addActionListener(this);SBB.addActionListener(this);Reset.addActionListener(this);OK.addActionListener(this);Cancel.addActionListener(this);}public void setTextAreaColor(){Normal.setForeground(NFB.getBackground());Normal.setBackground(NBB.getBackground());Selected.setForeground(SFB.getBackground());Selected.setBackground(SBB.getBackground()); }public void cancel(){Normal.setForeground(NFC);Normal.setBackground(NBC);Selected.setForeground(SFC);Selected.setBackground(SBC);NFB.setBackground(NFC);NBB.setBackground(NBC);SFB.setBackground(SFC);SBB.setBackground(SBC);Dialog.setVisible(false);}public void actionPerformed(ActionEvent e) { Object obj=e.getSource();if(obj==Reset){NFB.setBackground(new Color(0,0,0)); NBB.setBackground(new Color(249,249,251)); SFB.setBackground(new Color(0,0,0));SBB.setBackground(new Color(191,207,223)); setTextAreaColor();}else if(obj==OK){NFC=NFB.getBackground();NBC=NBB.getBackground();SFC=SFB.getBackground();SBC=SBB.getBackground();Dialog.setVisible(false);}else if(obj==Cancel)cancel();else{Chooser.init(((Component) obj).getBackground());Chooser.Dialog.setVisible(true);((Component)obj).setBackground(Chooser.New.getBackground());setTextAreaColor();}}public void windowClosing(WindowEvent e) {cancel();}public void windowActivated(WindowEvent arg0) {}public void windowClosed(WindowEvent arg0) {}public void windowDeactivated(WindowEvent arg0) {}public void windowDeiconified(WindowEvent arg0) {}public void windowIconified(WindowEvent arg0) {}public void windowOpened(WindowEvent arg0) {}}class ColorChooser implements ActionListener,WindowListener,KeyListener{ JDialog Dialog;JButton Choice[][],Old,New,OK,Cancel;JPanel Panel;JTextField R,G,B;JLabel OldLabel,NewLabel,RL,GL,BL;ColorChooser(JDialog color,int x, int y){Dialog=new JDialog(color,true);Choice=new JButton[8][8];Panel=new JPanel();Old=new JButton("");New=new JButton("");OldLabel=new JLabel("Old:");NewLabel=new JLabel("New:");RL=new JLabel("R:");GL=new JLabel("G:");BL=new JLabel("B:");R=new JTextField("");G=new JTextField("");B=new JTextField("");OK=new JButton("OK");Cancel=new JButton("Cancel");Panel.setLayout(new GridLayout(8,8,0,0));int i=0,j=0;Color c;Choice[0][7]=newJButton("");Choice[0][7].setBackground(new Color(255,255,255));Choice[1][7]=newJButton("");Choice[1][7].setBackground(new Color(255,223,191));Choice[2][7]=newJButton("");Choice[2][7].setBackground(new Color(255,207,207));Choice[3][7]=newJButton("");Choice[3][7].setBackground(new Color(223,191,255));Choice[4][7]=newJButton("");Choice[4][7].setBackground(new Color(207,207,255));Choice[5][7]=newJButton("");Choice[5][7].setBackground(new Color(191,223,255));Choice[6][7]=newJButton("");Choice[6][7].setBackground(new Color(207,255,207));Choice[7][7]=newJButton("");Choice[7][7].setBackground(new Color(223,255,191));for(i=0;i<8;i++){c=Choice[i][7].getBackground();for(j=0;j<8;j++){if(j!=7){Choice[i][j]=new JButton("");Choice[i][j].setBackground(newColor(c.getRed()*(j+1)/8,c.getGreen()*(j+1)/8,c.getBlue()*(j +1)/8));}Choice[i][j].setFocusable(false);Choice[i][j].addActionListener(this);Panel.add(Choice[i][j]);}}Dialog.setSize(280,250);Dialog.setLayout(null);Dialog.setLocation(x, y);Dialog.setResizable(false);Dialog.add(Panel);Panel.setBounds(10, 10, 160, 160);Dialog.add(Old);Dialog.add(OldLabel);Old.setEnabled(false);Old.setBorderPainted(false);Old.setBounds(214, 10, 44, 22);OldLabel.setBounds(180, 10, 44, 22);Dialog.add(New);Dialog.add(NewLabel);New.setEnabled(false);New.setBorderPainted(false);New.setBounds(214, 32, 44, 22); NewLabel.setBounds(180, 32, 44, 22); Dialog.add(R);Dialog.add(G);Dialog.add(B);R.setBounds(214, 97, 44, 22);G.setBounds(214, 123, 44, 22);B.setBounds(214, 149, 44, 22); Dialog.add(RL);Dialog.add(GL);Dialog.add(BL);RL.setBounds(196, 97, 16, 22);GL.setBounds(196, 123, 16, 22);BL.setBounds(196, 149, 16, 22); Dialog.add(OK);Dialog.add(Cancel);OK.setFocusable(false);Cancel.setFocusable(false);OK.setBounds(106, 190, 74, 24); Cancel.setBounds(190, 190, 74, 24); OK.addActionListener(this); Cancel.addActionListener(this);G.addKeyListener(this);R.addKeyListener(this);B.addKeyListener(this);}public void setText(Color c){R.setText(String.valueOf(c.getRed()));G.setText(String.valueOf(c.getGreen()));B.setText(String.valueOf(c.getBlue()));}public void init(Color c){New.setBackground(c);Old.setBackground(c);setText(c);}public void actionPerformed(ActionEvent e) { Object obj=e.getSource();if(obj==OK) Dialog.setVisible(false);else if(obj==Cancel){New.setBackground(Old.getBackground());Dialog.setVisible(false);}else{New.setBackground(((Component) obj).getBackground());setText(New.getBackground());}}public void windowClosing(WindowEvent e) {New.setBackground(Old.getBackground());Dialog.setVisible(false);}public void keyReleased(KeyEvent e) {try{int r,g,b;if(R.getText().length()==0) r=0;else r=Integer.valueOf(R.getText());if(G.getText().length()==0) g=0;else g=Integer.valueOf(G.getT ext());if(B.getText().length()==0) b=0;else b=Integer.valueOf(B.getText());New.setBackground(new Color(r,g,b));}catch(NumberFormatExceptionnfe){setText(New.getBackground());}catch(IllegalArgumentExceptioniae){setText(New.getBackground());} }public void keyPressed(KeyEvent e) {}public void keyTyped(KeyEvent e) {}public void windowActivated(WindowEvent arg0) {}public void windowClosed(WindowEvent arg0) {}public void windowDeactivated(WindowEvent arg0) {} public void windowDeiconified(WindowEvent arg0) {} public void windowIconified(WindowEvent arg0) {}public void windowOpened(WindowEvent arg0) {}}EnsureDialog.javapackage notepad;import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class EnsureDialog implements WindowListener, ActionListener{ public int YES,NO,CANCEL,Status;public JDialog Ensure;public JButton Yes,No,Cancel;public JLabel Question;public JPanel ButtonPanel,TextPanel;EnsureDialog(JFrame notepad, int x, int y) {YES=0;NO=1;CANCEL=2;Status=CANCEL;Ensure=new JDialog(notepad,true);/** 这里的模式标志true的作用是使对话框处于notepad的上端,并且当对话框显示时进程处于停滞状态,* 直到对话框不再显示为止。
Java 记事本设计完整代码
String str = findtext.getText();
int end = text.length();
int len = str.length();
int start = content.getSelectionEnd();
if (start == end) {
boolean status=false;
mynotepad() {
initTextContent();
initMenu();
}
void initTextContent() {
JScrollPane scroll = new JScrollPane(content);
getContentPane().add(scroll);
public void actionPerformed(ActionEvent e) {
final JDialog dialog = new JDialog(about, "查找字符串...",
true);
dialog.setBounds(560, 250, 310, 130);
JLabel find = new JLabel("请输入字符串:");
// TODO Auto-generated method stub
JTextArea editArea = (JTextArea)e.getSource();
int linenum = 1;
int columnnum = 1;
try {
int caretpos = editArea.getCaretPosition();
JAVA课程设计 编写一个记事本程序
Java语言程序设计B报告班级:学号:姓名:成绩:2012年12月题目:编写一个记事本程序一.课程设计题目概要。
1.1课程设计目的1.2题目设计要求二.课程设计思路。
三.课程设计实现功能结构图。
3.1“文件”菜单。
3.2“编辑”菜单。
四.记事本实现功能代码实现。
4.1界面设计代码实现及实现截图。
4.2功能代码详细设计4.2.1新建文件4.2.2打开文件4.2.3复制文件4.2.4粘贴文件4.2.5退出记事本五.JAVA课程设计个人总结。
附:JAVA课程设计程序源代码一.课程设计题目概要。
1.1课程设计目的。
按照老师要求,编写一个记事本程序,并可以实现老师要求的相关功能。
在课程设计过程中,复习巩固Java语言的基础知识,进一步加深对Java语言的理解和掌握,将课本上的理论知识和实际有机的结合起来,锻炼自己的分析解决实际问题的能力,提高自己实践编程的能力。
1.2题目设计要求。
要求:用图形用户界面实现。
能实现编辑、保存、另存为、查找替换等功能。
提示:使用文件输入输出流。
二.课程设计思路。
设计两个类,一个记事本的类(JSB),具有程序的要求和功能再创建一个课设的主类(KESHE),在主类中创建一个JSB类的对象,就可以直接在编译器中运行程序。
在主界面中设计一个Frame容器,并在其中加入一个下拉菜单和一个文本域控件,把该容器设置为GridLayout(1,1)布局。
三.课程设计实现功能结构图。
3.1“文件”菜单。
3.2“编辑”菜单。
四.记事本实现功能代码实现。
4.1.界面设计。
主要代码:f=new Frame(s);p1=new TextArea("");f.setSize(400,600);f.setBackground(Color.white);f.setVisible(true);menubar=new MenuBar();menu1=new Menu("文件");menu2=new Menu("编辑");item1=new MenuItem("新建");item2=new MenuItem("打开");item3=new MenuItem("保存");item4=new MenuItem("另存为");item5=new MenuItem("退出");item6=new MenuItem("全选");item7=new MenuItem("复制");item8=new MenuItem("剪切");item9=new MenuItem("粘贴"); menu1.add(item1);menu1.add(item2);menu1.add(item4); menu1.add(item5); menu2.add(item6); menu2.add(item7); menu2.add(item8); menu2.add(item9); menubar.add(menu1); menubar.add(menu2);f.setMenuBar(menubar);程序代码实现截图:4.2功能代码详细设计。
Java记事本毕业论文设计
Java记事本摘要现在网上的各种记事本数不胜数,功能也应有尽有,有能改变字体的,改变字体颜色的,还要改变背景颜色,还有些可以给记事本加密保存,但是这些记事本总是有一些小瑕疵。
日常生活中,我们需要的是一个功能强大,而且操作简单的记事本,还需要记录一些比较私密的事情。
所以本程序就是综合了一些平时常用的功能,并且加入了加密保存功能,满足各种用户需求。
随着科学技术的快速发展,计算机程序设计早已成为了趋势,JAVA是计算机专业的核心课程,是计算机科学的算法理论基础和软件设计的技术基础。
JAVA是实践性很强的课程。
Java语言是当今流行的网络编程语言,它具有面向对象、跨平台、分布应用等特点。
面向对象的开发方法是当今世界最流行的开发方法,它不仅具有更贴近自然的语义,而且有利于软件的维护和继承。
该程序通过java语言,使用Eclipse编写而成,参考windows记事本的基本功能,实现了所要实现的功能。
点开文件,可以进行新建,打开,保存,另存为,载入加密文件,加密保存为和退出功能。
点开编辑有撤销,复制,粘贴,剪切,删除,搜索/替换,全选,时间/日期功能。
点开格式有自动换行,字体格式,字体颜色,背景颜色的功能。
关键词:JA V A;Eclipse;记事本;加密Java NotepadAbstractNow online notepad numerous various functions are everything, there can change the font, change the font color, but also to change the background color, and some can be saved to the encrypted notepad, but they always have some Notepad small flaws. Everyday life, what we need is a powerful and easy-to-notepad, also need to record some of the more intimate things. So the procedure is usually a combination of some of the commonly used functions, and joined the encryption save function, to meet the various needs of users.Any time the rapid development of science and technology, computer programming has become a trend, JA V A is a computer professional core courses, is the technical foundation of computer science algorithms and software design rationale. JA V A is a very practical course. The Java language is today's popular web programming language, it has object-oriented, cross-platform, distributed applications and so on. Object-oriented development approach is the development of the world's most popular method, which not only has more natural semantics, but also conducive to software maintenance and inheritance.The program written by java language made reference to the basic functions of windows notepad achieve a function to be achieved. Opening the file, you can be New, Open, Save, Save As, loading the encrypted file, save it as encryption and exit functions. Opening the editor has withdrawn, copy, paste, cut, delete, search / replace, select all, time / date function. Opening the wrap format, font formatting, font color, background color function. Keywords: JA V A; Eclipse; notebook; encryption目录摘要 (i)Abstract (ii)1 绪论 (1)1.1 记事本的背景 (1)1.2 记事本的编写目的及意义 (1)1.3 Java技术 (2)1.4 Java技术的优点 (4)1.5 Eclipse(集成开发环境) (7)2 需求分析 (8)2.1 系统可行性分析 (9)2.1.1 技术可行性 (10)2.1.2 经济可行性 (10)2.1.3 操作可行性 (11)2.1.4 运行及时间可行性 (11)2.2 需求分析 (11)2.2.1 系统功能概述 (12)2.2.2 系统功能需求 (12)2.2.3 系统性能需求 (12)3 记事本总体设计 (12)3.1 系统设计原则 (13)3.2 功能模块设计 (13)3.2.1 模块设计划分 (14)3.2.2 系统结构设计 (15)4 记事本详细设计 (15)4.1 详细设计的方法 (16)4.2 详细设计常用工具 (16)4.3 详细设计基本任务 (16)4.4 常用的加密方法 (17)4.5 加密功能的实现 (21)5 记事本编码与实现 (21)5.1 窗口设计 (21)5.2 菜单栏以及子模块的设计 (23)5.3 文件菜单中各个子功能的设计 (25)5.3.1 搜索/替换功能实现 (25)5.3.2 自动换行功能实现 (26)5.3.3 字体格式功能实现 (27)5.3.4 字体颜色功能实现 (28)5.3.5 字体背景功能实现 (28)5.3.6 加密等功能实现 (29)5.3.7 改动保存的功能实现 (32)5.4 帮助文字信息的设计 (32)6 记事本软件测试与运行 (34)6.1 软件运行环境要求 (34)6.2 测试的定义 (34)6.3 测试的原则 (34)6.4 测试的目的 (35)6.5 系统测试用例 (35)6.5.1 界面测试 (35)6.5.2 功能测试 (35)6.6 系统测试结果 (37)结论 (38)参考文献 (39)致谢 (40)外文原文 (41)中文翻译 (51)1绪论1.1记事本的背景记事本相当常见,其存储文件的扩展名为.txt,文件属性没有任何格式标签或者风格,所以相当适合在dos环境中编辑。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
以下文档格式全部为word格式,下载后您可以任意修改编辑。
JAVA记事本源代码import java.awt.*;import java.awt.event.*;import java.text.*;import java.util.*;import java.io.*;import javax.swing.undo.*;import javax.swing.*;import javax.swing.event.*;import java.awt.datatransfer.*;import java.util.List;public class Notepad4 extends JFrame implements ActionListener, DocumentListener {JMenu mFile, mEdit, mMode, mView, mHelp;---文件菜单JMenuItem mFile_New, mFile_Open, mFile_Save, mFile_ASave, mFile_Print, mFile_Exit;---编辑菜单JMenuItem mEdit_Undo, mEdit_Cut, mEdit_Copy, mEdit_Paste, mEdit_Del, mEdit_Search, mEdit_SearchNext, mEdit_Replace, mEdit_Turnto, mEdit_SelectAll, mEdit_TimeDate;---格式菜单JCheckBoxMenuItem formatMenu_LineWrap;JMenu formatMenu_Color;JMenuItem formatMenu_Font, formatMenu_Color_FgColor, formatMenu_Color_BgColor;---查看菜单JCheckBoxMenuItem viewMenu_Status;---帮助菜单JMenuItem mHelp_HelpTopics, mHelp_About;---弹出菜单级菜单项JPopupMenu popupMenu;JMenuItem popupMenu_Undo, popupMenu_Cut, popupMenu_Copy, popupMenu_Paste, popupMenu_Delete, popupMenu_SelectAll;---工具栏按钮JButton newButton, openButton, saveButton, saveAsButton, printButton, undoButton, redoButton, cutButton, copyButton, pasteButton, deleteButton, searchButton, timeButton, fontButton, boldButton,italicButton, fgcolorButton, bgcolorButton, ;文本编辑区域static JTextArea Text;状态栏标签JLabel statusLabel1, statusLabel2, statusLabel3;JToolBar statusBar;---系统剪贴板Toolkit toolKit = Toolkit.getDefaultToolkit();Clipboard clipBoard = toolKit.getSystemClipboard();---创建撤消操作管理器protected UndoManager undo = new UndoManager();protected UndoableEditListener undoHandler = new UndoHandler();----其它变量boolean isNewFile = true; 是否新文件(未保存过的)File currentFile; 当前文件名String oldValue; 存放编辑区原来的内容,用于比较文本是否有改动JButton fontOkButton; 字体设置里的"确定"按钮----设置编辑区默认字体protected Font defaultFont = new Font("宋体", Font.PLAIN, 12); GregorianCalendar time = new GregorianCalendar();int = time.get(Calendar.MINUTE);int second = time.get(Calendar.SECOND);File saveFileName = null, fileName = null;public Notepad4() {super("VXBB的记事本");setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);Container container = getContentPane();System.out.println(Text.getDragEnabled()); 支持自动拖放JScrollPane scroll = new JScrollPane(Text);scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALW AYS);scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) ;Text.setWrapStyleWord(true); 设置单词在一行不足容纳时换行Text.setLineWrap(true);Text.setFont(defaultFont); 设置编辑区默认字体Text.setBackground(Color.white); 设置编辑区默认背景色Text.setForeground(Color.black); 设置编辑区默认前景色oldValue = Text.getText(); 获取原文本编辑区的内容--编辑区注册事件监听Text.getDocument().addUndoableEditListener(undoHandler); 添加负责通知任何更改的撤消侦听器Text.getDocument().addDocumentListener(this); 添加负责通知任何更改的文档侦听器JMenuBar MenuBar = new JMenuBar();mFile = new JMenu("文件(F)", true); 创建菜单mEdit = new JMenu("编辑(E)", true);mMode = new JMenu("格式(O)", true);mView = new JMenu("查看(V)", true);mHelp = new JMenu("帮助(H)", true);mEdit.addActionListener(new ActionListener() 注册事件监听{public void actionPerformed(ActionEvent e) {checkMenuItemEnabled(); 设置剪切、复制、粘贴、删除等功能的可用性}});mFile.setMnemonic('F');mEdit.setMnemonic('E');mMode.setMnemonic('O');mView.setMnemonic('V');mHelp.setMnemonic('H');MenuBar.add(mFile);MenuBar.add(mEdit);MenuBar.add(mMode);MenuBar.add(mView);MenuBar.add(mHelp);--文件菜单mFile_New = new JMenuItem("新建(N)", 'N');mFile_Open = new JMenuItem("打开(O)", 'O');mFile_Save = new JMenuItem("保存(S)", 'S');mFile_ASave = new JMenuItem("另存为(A)", 'A');mFile_Print = new JMenuItem("打印(P)", 'P');mFile_Exit = new JMenuItem("退出(X)", 'X');mFile_New.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));mFile_Open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));mFile_Save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));mFile_Print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK));mFile_New.addActionListener(this); 注册事件监听mFile_Open.addActionListener(this);mFile_Save.addActionListener(this);mFile_ASave.addActionListener(this);mFile_Print.addActionListener(this);mFile_Exit.addActionListener(this);mFile.add(mFile_New); 添加菜单项mFile.add(mFile_Open);mFile.add(mFile_Save);mFile.add(mFile_ASave);mFile.addSeparator(); 添加分割线mFile.add(mFile_Print);mFile.addSeparator(); 添加分割线mFile.add(mFile_Exit);--编辑菜单mEdit_Undo = new JMenuItem("撤消(U)", 'U');mEdit_Cut = new JMenuItem("剪切(T)", 'T');mEdit_Copy = new JMenuItem("复制(C)", 'C');mEdit_Paste = new JMenuItem("粘贴(P)", 'P');mEdit_Del = new JMenuItem("删除(L)", 'L');mEdit_Search = new JMenuItem("查找(F)", 'F');mEdit_SearchNext = new JMenuItem("查找下一个(N)", 'N'); mEdit_Replace = new JMenuItem("替换(R)", 'R');mEdit_Turnto = new JMenuItem("转到(G)", 'G');mEdit_SelectAll = new JMenuItem("全选(A)", 'A');mEdit_TimeDate = new JMenuItem("时间日期(D)", 'D'); mEdit_Cut.setEnabled(false);mEdit_Undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK));mEdit_Cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));mEdit_Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));mEdit_Paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));mEdit_Del.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); mEdit_Search.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK));mEdit_SearchNext.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));mEdit_Replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK));mEdit_Turnto.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_MASK));mEdit_SelectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));mEdit_TimeDate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0)); mEdit_Undo.addActionListener(this); 注册事件监听mEdit_Cut.addActionListener(this);mEdit_Copy.addActionListener(this);mEdit_Paste.addActionListener(this);mEdit_Del.addActionListener(this);mEdit_Search.addActionListener(this);mEdit_SearchNext.addActionListener(this);mEdit_Replace.addActionListener(this);mEdit_Turnto.addActionListener(this);mEdit_SelectAll.addActionListener(this);mEdit_TimeDate.addActionListener(this);mEdit.add(mEdit_Undo); 添加菜单项mEdit.addSeparator(); 添加分割线mEdit.add(mEdit_Cut);mEdit.add(mEdit_Copy);mEdit.add(mEdit_Paste);mEdit.add(mEdit_Del);mEdit.addSeparator();mEdit.add(mEdit_Search);mEdit.add(mEdit_SearchNext);mEdit.add(mEdit_Replace);mEdit.add(mEdit_Turnto);mEdit.addSeparator();mEdit.add(mEdit_SelectAll);mEdit.add(mEdit_TimeDate);--格式菜单formatMenu_LineWrap = new JCheckBoxMenuItem("自动换行(W)"); formatMenu_LineWrap.setMnemonic('W');formatMenu_LineWrap.setState(true);formatMenu_Font = new JMenuItem("字体(F)", 'F');formatMenu_Color = new JMenu("颜色");formatMenu_Color_FgColor = new JMenuItem("字体颜色"); formatMenu_Color_BgColor = new JMenuItem("背景颜色"); formatMenu_LineWrap.addActionListener(this); 注册事件监听formatMenu_Font.addActionListener(this);formatMenu_Color_FgColor.addActionListener(this); formatMenu_Color_BgColor.addActionListener(this); mMode.add(formatMenu_LineWrap); 添加菜单项mMode.addSeparator();mMode.add(formatMenu_Font);mMode.add(formatMenu_Color);formatMenu_Color.add(formatMenu_Color_FgColor); formatMenu_Color.add(formatMenu_Color_BgColor);--查看菜单viewMenu_Status = new JCheckBoxMenuItem("状态栏(S)"); viewMenu_Status.setMnemonic('S');viewMenu_Status.setState(true);viewMenu_Status.addActionListener(this);mView.add(viewMenu_Status);--帮助菜单mHelp_HelpTopics = new JMenuItem("帮助(H)", 'H');mHelp_About = new JMenuItem("关于(A)", 'A');mHelp_HelpTopics.addActionListener(this);mHelp_About.addActionListener(this);mHelp.add(mHelp_HelpTopics);mHelp.addSeparator(); 添加分割线mHelp.add(mHelp_About);创建右键弹出菜单popupMenu = new JPopupMenu();popupMenu_Undo = new JMenuItem("撤消(U)", 'U'); popupMenu_Cut = new JMenuItem("剪切(T)", 'T'); popupMenu_Copy = new JMenuItem("复制(C)", 'C'); popupMenu_Paste = new JMenuItem("粘贴(P)", 'P'); popupMenu_Delete = new JMenuItem("删除(D)", 'D'); popupMenu_SelectAll = new JMenuItem("全选(A)", 'A'); popupMenu_Undo.setEnabled(false); 撤消选项初始设为不可用 ---向右键菜单添加菜单项和分隔符popupMenu.add(popupMenu_Undo);popupMenu.addSeparator();popupMenu.add(popupMenu_Cut);popupMenu.add(popupMenu_Copy);popupMenu.add(popupMenu_Paste);popupMenu.add(popupMenu_Delete);popupMenu.addSeparator();popupMenu.add(popupMenu_SelectAll);右键菜单注册事件popupMenu_Undo.addActionListener(this);popupMenu_Cut.addActionListener(this);popupMenu_Copy.addActionListener(this);popupMenu_Paste.addActionListener(this);popupMenu_Delete.addActionListener(this);popupMenu_SelectAll.addActionListener(this);文本编辑区注册右键菜单事件Text.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显示弹出菜单。