




免费预览已结束,剩余15页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
(武汉) Java课程设计 Java语言程序设计报告班级: 08计科3班 学号: 20081003258 姓名: 唐 翰 成绩: 2011年1月记事本程序一、实验要求编写一个记事本程序要求: 用图形用户界面实现。 能实现编辑、保存、另存为、查找替换等功能。 提示:使用文件输入输出流。二、实验背景“记事本”的功能虽然连“写字板”都比不上,但它还是有它自己的独门绝技的。下面我们就来看看记事本吧。相对于微软的 Word来说“记事本”的功能确实是太单薄了,只有:新建,保存,打印,查找,替换这几个功能。但是“记事本”却拥有一个Word不可能拥有的优点:打开 速度快,文件小。我相信对于这一点大家一定是深有感触地,一点就打开;同样的文本文件用Word保存和用记事本保存的文件大小就大不相同,所以对于大小在 64KB以下的纯文本的保存最好还是采用记事本。 记事本另一项不可取代的功能是:可以保存无格式文件。你可以把记事本编辑的文件保存为:“.html”, “.java”,“.asp”等等任意格式。这使得“记事本”又找到了一个新的用途:作为程序语言的编辑器。翻开任何一本介绍一门编程语言的入门教材,里面都会建议学生在记事本中编写源程序。在此我们利用Java的文件输入输入流来实现简单的记事本小程序。三、运行环境 系统:Microsoft XP SP3 软件:Eclipse 四、概要设计设计两个类,一个Notepad类继承JFrame类,然后在Notepad类中添加文件打开,新建,保存等方法,就可以直接在编译器中运行程序。1:界面设计在主界面中设计一个新建对象Notepad,由Notepad继续自JFrame,故得到Notepad容器,并在其中加入一个下拉菜单和一个文本域控件,把该容器设置为GridLayout(2,1)布局。 2:功能设计在下拉菜单中加入子菜单并进行监听,分别实现对文件的操作功能。点击不同菜单项时,显示不同界面。 3:代码设计编写每个控件的相关代码,并进行调试。五、详细代码1:界面设计创建一个下拉菜单和文本域,在文本域中显示编辑的文件信息。JMenu fileMenu=new JMenu(文件(F) ;JMenuItem newItem=new JMenuItem(新建);JMenuItem openItem=new JMenuItem(打开.);JMenuItem saveItem=new JMenuItem(另存为.);JMenuItem copyItem=new JMenuItem(复制);JMenuItem tieItem=new JMenuItem(粘贴);JMenuItem aboutItem=new JMenuItem(关于程序);JMenuItem exitItem= new JMenuItem(退出);JMenu formatMenu=new JMenu(设置(S);String colors=黑色,蓝色,红色,绿色,粉色,桔黄色;JMenu colorMenu=new JMenu(颜色);JMenu sizeMenu=new JMenu(大小);String size=10,16,24,36;String fontNames=幼圆,微软雅黑,隶书,楷体_GB2312,华文新魏;JMenu fontMenu=new JMenu(字体);String styleNames=加黑,斜体;JMenu FindMenu = new JMenu(查找(T);FindMenu.setMnemonic(T);JMenuItem searchMenu = new JMenuItem(查找);JMenuItem replaceMenu = new JMenuItem(替换);JMenu lookMenu=new JMenu(查看(L);/设置菜单项JMenuItem taiItem=new JMenuItem(状态栏);JMenu helpMenu=new JMenu(帮助(H); /设置菜单项JMenuItem helpItem=new JMenuItem(帮助主题);helpMenu.add(helpItem);helpMenu.addSeparator();/设置分割线JMenuItem about1Item=new JMenuItem(关于记事本.);界面如下: 2功能设计 打开窗口事件OpenHander:class OpenHandler implements ActionListener public void actionPerformed(ActionEvent e)JFileChooser jc=new JFileChooser();int rVal=jc.showOpenDialog(C3.this);/显示打开文件的对话框if(rVal=JFileChooser.APPROVE_OPTION)File dir=jc.getCurrentDirectory();File file=jc.getSelectedFile();filenameTf.setText(file.getName();dirTf.setText(dir.toString();/在文本域内显示文本文件内容contentTa.setText(read(new File(dir,file.getName();if(rVal=JFileChooser.CANCEL_OPTION) filenameTf.setText(你取消了当前选择!);dirTf.setText( ); 打开时读取文字事件:private String read(File file)/打开时的读取tryBufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(file),GBK);String data=null;StringBuffer buffer=new StringBuffer();while(data=reader.readLine()!=null)buffer.append(data+n);reader.close();return buffer.toString();catch(IOException e)throw new RuntimeException(e); 保存事件SaveHandler:class SaveHandler implements ActionListener /保存public void actionPerformed(ActionEvent e)JFileChooser jc=new JFileChooser();int rVal=jc.showSaveDialog(C3.this);/显示保存文件的对话框if(rVal=JFileChooser.APPROVE_OPTION)File dir=jc.getCurrentDirectory();File file=jc.getSelectedFile();filenameTf.setText(file.getName();dirTf.setText(dir.toString();write(new File(dir,file.getName(),contentTa.getText();if(rVal=JFileChooser.CANCEL_OPTION)filenameTf.setText(你取消了当前选择!);dirTf.setText( ); 粘贴文件: tieItem.addActionListener(new ActionListener() /响应粘贴事件public void actionPerformed(ActionEvent event)contentTa.setText(contentTa.getText()+str);); 查找替换功能: public void showFind() setTitle(查找); setSize(280,60); setVisible(true); public void showReplace() setTitle(查找替换); setSize(280,110); setVisible(true); private void find() String text=ta.getText(); String str=tFind.getText(); int end=text.length(); int len=str.length(); int start=ta.getSelectionEnd(); if(start=end) start=0; for(;start=end-len;start+) if(text.substring(start,start+len).equals(str) ta.setSelectionStart(start); ta.setSelectionEnd(start+len); return; /若找不到待查字符串,则将光标置于末尾 ta.setSelectionStart(end); ta.setSelectionEnd(end); public Button getBFind() return bFind; private void replace() String str=tReplace.getText(); if(ta.getSelectedText().equals(tFind.getText() ta.replaceRange(str,ta.getSelectionStart(),ta.getSelectionEnd(); else find(); 六、运行结果七、源码package p1;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;public class Notepad extends JFrame /* * 记事本程序 */private static final long serialVersionUID = 1L;/定义菜单项private final Color colorValues=Color.BLACK,Color.BLUE,Color.RED,Color.GREEN,Color.PINK,Color.ORANGE;private JRadioButtonMenuItem colorItems,fonts,sizeItems;private JCheckBoxMenuItem styleItems;private JLabel displayLabel;private ButtonGroup fontGroup,colorGroup,sizeGroup;/创建按钮组对象,实现JRadioButton多选一功能private int style;/定义字体大小int fonti=10; Font font; /JRadioButton r1,r2,r3; /声明按钮对象 ButtonGroup bg=new ButtonGroup( ); / private String str=new String();/用来存放用户当前选择的文本/private JTextField filenameTf=new JTextField(), dirTf=new JTextField();private JPanel optPane=new JPanel();private JPanel navigetePane=new JPanel();private JTextArea contentTa=new JTextArea(5,20);/public Notepad(String title)super(title);/设置字体大小/ r1=new JRadioButton(10); /r1.addActionListener(new sizeHandler(); / optPane.add(r1); /加载按钮到界面上 /r2=new JRadioButton(16); /r2.addActionListener(new sizeHandler(); /optPane.add(r2); / r3=new JRadioButton(24); /r3.addActionListener(new sizeHandler(); / optPane.add(r3); / bg.add(r1); /加载按钮到按钮组 / bg.add(r2); / bg.add(r3);/dirTf.setEditable(false); / 设置为不可编辑filenameTf.setEditable(false); /设置为不可编辑navigetePane.setLayout(new GridLayout(2,1);navigetePane.add(filenameTf); /添加navigetePane.add(dirTf);Container contentPane=getContentPane();contentPane.add(optPane,BorderLayout.SOUTH);contentPane.add(navigetePane,BorderLayout.NORTH);contentPane.add(new JScrollPane(contentTa),BorderLayout.CENTER);setSize(500,300);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /设置响应关闭按钮JMenu fileMenu=new JMenu(文件(F) ;fileMenu.setMnemonic(F);/设置快捷键JMenuItem newItem=new JMenuItem(新建);fileMenu.add(newItem); newItem.addActionListener( new ActionListener()/显示消息对话框public void actionPerformed(ActionEvent event) contentTa.setText( ); );/JMenuItem openItem=new JMenuItem(打开.);fileMenu.add(openItem);openItem.addActionListener(new OpenHandler();JMenuItem saveItem=new JMenuItem(另存为.);fileMenu.add(saveItem);fileMenu.addSeparator();/设置分割线saveItem.addActionListener(new SaveHandler();JMenuItem copyItem=new JMenuItem(复制);copyItem.setMnemonic(C);/设置快捷键fileMenu.add(copyItem);copyItem.addActionListener(new ActionListener() /响应复制事件public void actionPerformed(ActionEvent event)str=contentTa.getSelectedText(););/JMenuItem tieItem=new JMenuItem(粘贴);fileMenu.add(tieItem);tieItem.setMnemonic(V);/设置快捷键fileMenu.addSeparator();/设置分割线tieItem.addActionListener(new ActionListener() /响应粘贴事件public void actionPerformed(ActionEvent event)contentTa.setText(contentTa.getText()+str););/JMenuItem aboutItem=new JMenuItem(关于程序);fileMenu.add(aboutItem);aboutItem.addActionListener( new ActionListener() /显示消息对话框public void actionPerformed(ActionEvent event)JOptionPane.showMessageDialog(Notepad.this,这是(*_*)的记事本,About,JOptionPane.PLAIN_MESSAGE););JMenuItem exitItem= new JMenuItem(退出);fileMenu.add(exitItem);exitItem.addActionListener( /响应退出事件new ActionListener()public void actionPerformed(ActionEvent event) System.exit(0););JMenuBar bar=new JMenuBar();setJMenuBar(bar); /在frame中设置菜单条bar.add(fileMenu);JMenu formatMenu=new JMenu(设置(S);formatMenu.setMnemonic(S);String colors=黑色,蓝色,红色,绿色,粉色,桔黄色;JMenu colorMenu=new JMenu(颜色);JMenu sizeMenu=new JMenu(大小);String size=10,16,24,36;sizeItems=new JRadioButtonMenuItemsize.length;sizeGroup=new ButtonGroup();/colorItems=new JRadioButtonMenuItemcolors.length;colorGroup=new ButtonGroup();ItemHandler itemHandler=new ItemHandler();for(int count=0;countcolors.length;count+)colorItemscount=new JRadioButtonMenuItem(colorscount);colorMenu.add(colorItemscount);colorGroup.add(colorItemscount);colorItemscount.addActionListener(itemHandler);colorItems0.setSelected(true);formatMenu.add(colorMenu);formatMenu.addSeparator();/设置分割线sizeItems=new JRadioButtonMenuItemcolors.length;sizeGroup=new ButtonGroup();for(int count=0;countsize.length;count+)sizeItemscount=new JRadioButtonMenuItem(sizecount);sizeMenu.add(sizeItemscount);sizeGroup.add(sizeItemscount);sizeItemscount.addActionListener(itemHandler);formatMenu.add(sizeMenu);sizeItems0.setSelected(true);formatMenu.addSeparator();/设置分割线String fontNames=幼圆,微软雅黑,隶书,楷体_GB2312,华文新魏;JMenu fontMenu=new JMenu(字体);fonts=new JRadioButtonMenuItemfontNames.length;fontGroup=new ButtonGroup();for(int count=0;countfonts.length;count+)fontscount=new JRadioButtonMenuItem(fontNamescount);fontMenu.add(fontscount);fontGroup.add(fontscount);fontscount.addActionListener(itemHandler);fonts0.setSelected(true);fontMenu.addSeparator();/设置分割线String styleNames=加黑,斜体;styleItems=new JCheckBoxMenuItemstyleNames.length;StyleHandler styleHandler=new StyleHandler();for(int count=0;countstyleNames.length;count+)styleItemscount=new JCheckBoxMenuItem(styleNamescount);fontMenu.add(styleItemscount);styleItemscount.addItemListener(styleHandler);formatMenu.add(fontMenu);bar.add(formatMenu);/添加到菜单项 JMenu FindMenu = new JMenu(查找(T);FindMenu.setMnemonic(T);JMenuItem searchMenu = new JMenuItem(查找);JMenuItem replaceMenu = new JMenuItem(替换);FindMenu.add(searchMenu);FindMenu.add(replaceMenu);searchMenu.addActionListener( new ActionListener() /显示消息对话框 public void actionPerformed(ActionEvent event)JFrame ss = new JFrame(查找);JLabel tt = new JLabel(查找);JTextField txt = new JTextField();JButton but = new JButton(查找);ss.setLayout(null);ss.setLocation(150,150);Dimension dim = new Dimension();dim.setSize(300, 100);tt.setBounds(10,20, 40, 20);txt.setBounds(70,20,100,20);but.setBounds(190,20,80,20);but.addActionListener( new ActionListener() /显示消息对话框 public void actionPerformed(ActionEvent event)JOptionPane.showMessageDialog(Notepad.this,查找成功了,查找,JOptionPane.PLAIN_MESSAGE););ss.add(but);ss.add(txt);ss.add(tt);ss.setSize(dim);ss.setVisible(true););replaceMenu.addActionListener( new ActionListener() /显示消息对话框 public void actionPerformed(ActionEvent event)JFrame ss = new JFrame(替换);JLabel tt = new JLabel(替换);JTextField txt = new JTextField();JButton but = new JButton(替换);ss.setLayout(null);ss.setLocation(150,150);Dimension dim = new Dimension();dim.setSize(300, 100);tt.setBounds(10,20, 40, 20);txt.setBounds(70,20,100,20);but.setBounds(190,20,80,20);but.addActionListener( new ActionListener() /显示消息对话框 public void actionPerformed(ActionEvent event)JOptionPane.showMessageDialog(Notepad.this,替换成功了,替换,JOptionPane.PLAIN_MESSAGE););ss.add(but);ss.add(txt);ss.add(tt);ss.setSize(dim);ss.setVisible(true););bar.add(FindMenu);JMenu lookMenu=new JMenu(查看(L);/设置菜单项JMenuItem taiItem=new JMenuItem(状态栏);lookMenu.add(taiItem);taiItem.addActionListener( new ActionListener() /显示消息对话框 public void actionPerformed(ActionEvent event)JOptionPane.showMessageDialog(Notepad.this,欢迎使用唐翰的记事本程序!,状态栏,JOptionPane.PLAIN_MESSAGE););bar.add(lookMenu);JMenu helpMenu=new JMenu(帮助(H); /设置菜单项JMenuItem helpItem=new JMenuItem(帮助主题);helpMenu.add(helpItem);helpMenu.addSeparator();/设置分割线JMenuItem about1Item=new JMenuItem(关于记事本.);helpMenu.add(about1Item);bar.add(helpMenu);about1Item.addActionListener( new ActionListener()/显示消息对话框public void actionPerformed(ActionEvent event)JOptionPane.showMessageDialog(Notepad.this,19108320 唐翰!,关于记事本,JOptionPane.PLAIN_MESSAGE););getContentPane().setBackground(Color.WHITE);setSize(700,500);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public static void main (String args)new Notepad(唐翰的记事本);class OpenHandler implements ActionListener /打开窗口事件public void actionPerformed(ActionEvent e)JFileChooser jc=new JFileChooser();int rVal=jc.showOpenDialog(Notepad.this);/显示打开文件的对话框if(rVal=JFileChooser.APPROVE_OPTION)File dir=jc.getCurrentDirectory();File file=jc.getSelectedFile();filenameTf.setText(file.getName();dirTf.setText(dir.toString();/在文本域内显示文本文件内容contentTa.setText(read(new File(dir,file.getName();if(rVal=JFileChooser.CANCEL_OPTION) filenameTf.setText(你取消了当前选择!);dirTf.setText( );class SaveHandler implements ActionListener /保存public void actionPerformed(ActionEvent e)JFileChooser jc=new JFileChooser();int rVal=jc.showSaveDialog(Notepad.this);/显示保存文件的对话框if(rVal=JFileChooser.APPROVE_OPTION)File dir=jc.getCurrentDirectory();File file=jc.getSelectedFile();filenameTf.setText(file.getName();dirTf.setText(dir.toString();write(new File(dir,file.getName(),contentTa.getText();if(rVal=JFileChooser.CANCEL_OPTION)filenameTf.setText(你取消了当前选择!);dirTf.setText( );/private String read(File file)/打开时的读取tryBufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(file),GBK);String data=null;StringBuffer buffer=new StringBuffer();while(data=reader.readLine()!=null)buffer.append(data+n);reader.close();return buffer.toString();catch(IOException e)throw new RuntimeException(e);private void write(File file,String str)/输入文件tryPrintWriter writer=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file),GBK);writer.println(str);writer.close();catch(IOException e)throw new RuntimeException(e);/处理事件private class ItemHandler implements ActionListenerpublic void actionPerformed(ActionEvent event)for(int count=0;countcolorItems.length;count+)if(colorItemscount.isSelected()contentTa.setForegroun
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 支付系统业务知识培训课件
- (2025)口腔组织病理学考试题库及参考答案
- 撬装设备基础知识培训课件
- 2025年职业技术《机动车驾驶教练员》专业技能综合知识考试题与答案
- 2025年全民安全与健康科学教育知识考试题与答案
- 撇捺点汉字基本笔画课件
- 2025版《义务教育英语课程标准》测试题及答案(一)
- 2024年安徽省合肥市医疗三严三基理论考试模拟试题及答案
- 2024年乡村振兴战略全面提升小康家庭知识试题(附含答案)
- 摄影基础知识培训课件课程
- 电工仪表使用规范
- 地质灾害治理工程用表格(完整资料)
- 内镜室院感工作计划
- 第三章 监理规划与监理实施细则
- GB/T 25074-2010太阳能级多晶硅
- GB/T 21196.2-2007纺织品马丁代尔法织物耐磨性的测定第2部分:试样破损的测定
- 高原病的防治问题西京医院
- 三年级下册口算天天100题(A4打印版)
- 上海交通大学学生生存手册
- 船舶高级消防(新版PPT)
- 劳务分包工程量清单报价表
评论
0/150
提交评论