




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持华北科技学院计算机系综合性实验实验报告课程名称 Java程序设计B实验学期2011 至 2012 学年第 1 学期学生所在系部计算机学院年级 2009专业班级网络学生姓名 学号任课教师李芙玲实验成绩计算机系制Java程序设计B课程综合性实验报告开课实验室: 基础实验室2011年11月16日实验题目简易写字板软件设计一、实验目的通过编写Java的应用系统综合实例一一简易写字板软件,总结、回顾和实践面向对象的编程思想以及编程方法,并通过编写程序来掌握Java语言编程技巧,将学习到的知识融会贯通,同时提高调试程序的能力,养成良好的编程习惯,
2、并增强对程序设计整体思路的把握。熟悉在集成开发环境下编写 Java程序。二、设备与环境硬件:多媒体计算机软件:Windows 2000以上的操作系统、JDK开发包、集成开发环境三、实验内容总体要求:程序功能基本完成,用户界面友好,代码的可维护性和可扩展性较好,最好包括 必要的注释和相应的文档。具体要求:基本的文本录入, 字符修改,删除,换行,粘贴,复制,剪切等功能,主要包括:.菜单栏设计及功能实现:菜单栏主要包括文件、编辑、格式、帮助等常见菜单。其中文件菜单有新建、打开、保存、另存为、退出等功能;编辑菜单有剪切、复制、粘贴、 等功能;格式菜单含有对文字字体、字型、字号及颜色等的设置功能,根据情
3、况可在编辑菜 单添加查找和查找替换功能。.编辑区设计:对文本的编辑处理区。.工具栏设计:可根据需要增加该项功能。.其他功能设计。四、实验结果及分析1.主界面的设计与实现整个界面包括菜单条,菜单项(文件,编辑,格式,查看,帮助)和菜单条,滚动条菜单的设计本程序共设计了五个菜单,分别是文件菜单(新建,打开,保存,另存为,退出);编辑菜单(撤销,剪切,复制,粘贴,删除,全选,时间)格式菜单(自动换行,字体)查看菜单(状态栏)帮助(查看帮助,关于记事本)文件菜单功能编辑菜单功能格式菜单的功能帮助菜单的功能编辑区设计编辑区添加了文本区,并在文本区内添加了滚动条,在文本区内添加了鼠标操作,即鼠标右键进行复
4、制,粘贴,剪切2.各项功能的实现(1)窗体设计:设计了一个大小可变,名为记事本的窗体,并添加了窗体关闭事件,即关闭窗体时会提示是否保存menu1=new JMenu(文件);menu1.addActionListener(menu2=new JMenu(编辑);menu3=new JMenu(格式);menu4=new JMenu(查看);menu5=new JMenu(帮助); menubar.add( menu1);menubar.add( menu?;menubar.add( menu3);menubar.add( menu4);menubar.add( menu5);this );添加
5、窗体上的菜单addWindowListener( public if变。想保存文件吗?new WindowAdapter()void windowClosing(WindowEvent event)(save_status =0)int check=JOptionPane. showConfirmDialog (null ,文件的文字已经改,警告,JOptionPane. YES_NO_CANCEL_OP)T IONif(check=0)saveFile( status ); if(check=1)System. exit (0); if(check=2) dispose。;);(2)文件菜单
6、各项功能.新建功能设计:item1 本区为空item1 .addActionListener(是新建菜单,在iteml上添加监听器,当执行新建操作时,自动设置文publicifnew ActionListener()void actionPerformed(ActionEvent e)(e.getSource(尸 text .setText( this .setTitle(item1 );无标题-记事本);.打开功能设计:item2上添加监听器,JfileChooser jfc1=new JfileChooser( FileReader fr = newFileReader(f1);当执行打开
7、操作时运用文件的输入流进行打开“ D:/ )BufferedReader br = new BufferedReader(fr);调用showOpenDialog()方法返回初始目录为本地系统的默认目录的对话框null )while (str = br.readLine()!=文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持 text .setText(str); fr.close(); br.close(); 调用readLine()顺序读取文件的一行 运行结果如图.保存功能,和打开功能相反,iten3添加监听器,当执行保存操作时运用文件输出流把文件保存到指定目录中JfileCh
8、ooser jfc1=new JfileChooser() FileWriter fw =new FileWriter(f2);BufferedWriter bw =new BufferedWriter(fw);将BufferedReader 和FileReader连接在一起,然后使用BufferedReader流将数据写入到目的地。.编辑中的复制,粘贴,剪切功能item* .addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) text .*(); );添力口 ActionList
9、ener 监听器,调用 API 中的 copy(),cut(),prase() 方法复制粘贴剪切可从鼠标右键使用,构造一个弹出式菜单menu= new JpopupMenu()为text添加鼠标监听器text .addMouseListener( new MouseAdapter() / 鼠标右键实现复制,粘贴,剪切功能 public void mousePressed(MouseEvent e) if (e.getModifiers()=InputEvent. BUTTON3_MASK menushow( text ,e.getX(),e.getY(); );.显示时间,为日期添加日历 te
10、m27.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) if (e.getSource()= item27 ) Calendar c1 =Calendar. getInstance (); inty = c1.get(Calendar.YEARintm = c1.get(Calendar.MONTHintd = c1.get(Calendar.DATE;inth = c1.get(Calendar.HOURint m1 = c1.get(Calendar. MINUTE int
11、m2 = m+1;text .setText(y+ 年”+m2+月+d+日+h+: +m1); );6自动换行功能item31 .addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) if (item31 .getLabel().equals( 自动换行) item31 .setLabel(,自动换行”); text .setLineWrap( true ); else item31 .setLabel( 自动换行); text .setLineWrap( false ););7.字
12、体功能定义了另外一个类,用对话框功能实现一个能选择字体,字形,大小的对话框。myziti=new JtextField()定义文本框 显示所选择的字体,字形,大小JScrollPane scroll=new JScrollPane( zitiList );scroll.setBounds(10, 60,100 ,100);设置图中所示的滚动条public void valueChanged(ListSelectionEvent event)myzixing .setText( myxingzhuang zixingList.getSelectedIndex();ziti_check= myzi
13、ti .getText();style_check =myzixing .getText();daxiao_check = mydaxiao .getText();int zitidaxiao=Integer.parseInt(daxiao_check );int zitistyle=Font.PLAIN ;if( style_check.equals(常规)zitistyle=Font.PLAIN;if( style_check.equals(斜体)zitistyle=Font.ITALIC;if( style_check.equals(粗体)zitistyle=Font.BOLD;if(
14、style_check.equals(粗斜体)zitistyle=Font. -BOLD+Font. ITALIC;shili .setFont( new Font( ziti_check,zitistyle,zitidaxiao););添加监听器,调用 equal() 按钮添加监听器方法,选择所需的字形cancel .addActionListener(public voiddispose。;);ok .addActionListener( public voidnew ActionListener()actionPerformed(ActionEvent e) new ActionList
15、ener()actionPerformed(ActionEvent e)四、心得体会实验完成了基本功能,能进行基本的操作。做完实验的第一感受是乱,在实验 中按照系统提示添加了很多东西,如添加菜单条,菜单项,菜单,文本区等觉得都比 较熟练,在查阅了网上和书上别人代码后, 也根据自己的理解实现了打开,新建和保 存功能,但是对于字体的设计我确实没有自己完成,在运用别人的代码后因为定义, 结构等不匹配也很苦恼,自己修改了好久,找了很多同学帮忙才运行成功。这次实验让我对所学的知识运用更加熟练,也让我对JAVA有了更深的喜爱,虽然我能掌握的知识很少,但是在以后的时间我会更加认真的学习JAVA ,我觉得在所
16、学的专业课中,终于找到一个自己感兴趣的,虽然学的没有别人好,我相信我会学 好的。平教评定项目ABCD评定项目ABCD介师算法正确界囿美观,布局合理程序结构合理操作熟练语法、语义正确解析完整实验结果正确义字流畅报告规范题解正确其他:评价教师签名:2011年11月26日package me;import java.awt.*;import javax.swing.*;import ;import ;import ;import java.io.*;import ;public class FirstWindow public static void main(String args口儿new FW
17、indow(记事本);class FWindow extends JFrame implements ActionListener,WindowListener,ItemListener public static Frame frame;JMenuBar menubar;JPopupMenu menu;JMenu menu1,menu2,menu3,menu4,menu5;JMenuItemitem1,item2,item3,item4,item5,item21,item22,item23,item24,item25,item26,item27,item31,item32,ite m41,i
18、tem51,item52;JMenuItem itemCopy,itemCut,itemPaste;JTextArea text;JScrollPane scrollPane;JComboBox listFont,listFSize;int save_status = 0,status=0,huanhang_count=0;FWindow(String s)addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent event)if(save_status=0)int check=JOptionPane.
19、showConfirmDialog(null, 文件的文字已经改变。想保存文件吗? ,警告 ,JOptionPane.YES_NO_CANCEL_OPTION);if(check=0)saveFile(status);if(check=1)System.exit(0);if(check=2)dispose(););setTitle( 记事本 );setSize(700,400);setLocation(120,120);menubar=new JMenuBar();setJMenuBar(menubar);text=new JTextArea();scrollPane=new JScrollP
20、ane(text);/ 滚动条add(scrollPane);scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);menu=new JPopupMenu();menu1=new JMenu( 文件 );menu1.addActionListener(this);menu2=new JMenu( 编辑 );menu3=new JMenu
21、( 格式 );menu4=new JMenu( 查看 );menu5=new JMenu( 帮助 );menubar.add(menu1);menubar.add(menu2);menubar.add(menu3);menubar.add(menu4);menubar.add(menu5);item1=new JMenuItem( 新建 ); / 文件菜单下菜单项的创建item1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(e.getSource()=item1)text.
22、setText();this.setTitle( 无标题 - 记事本 );private void setTitle(String string) / TODO Auto-generated method stub);item2=new JMenuItem( 打开 .);item2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(e.getSource()=item2)File f1;JFileChooser jfc1 = new JFileChooser(D:/);int n
23、um1 = jfc1.showOpenDialog(null);if(num1=JFileChooser.APPROVE_OPTION)tryf1 = jfc1.getSelectedFile();this.setTitle(f1.getName();FileReader fr = new FileReader(f1);BufferedReader br = new BufferedReader(fr);String str;while(str = br.readLine()!=null)text.setText(str);fr.close();br.close();catch(FileNot
24、FoundException e1)e1.printStackTrace();catch(IOException e2)e2.printStackTrace();private void setTitle(String name) );item3=new JMenuItem( 保存 );item3.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(e.getSource()=item3)File f2 = null;JFileChooser jfc2 = new JFileCho
25、oser();int num2 = jfc2.showSaveDialog(null);if(num2=JFileChooser.APPROVE_OPTION)f2=jfc2.getSelectedFile();this.setTitle(f2.getName();tryFileWriter fw = new FileWriter(f2);BufferedWriter bw = new BufferedWriter(fw);bw.write(text.getText();bw.close();fw.close();catch(IOException e2)e2.printStackTrace(
26、);private void setTitle(String name) / TODO Auto-generated method stub);item4=new JMenuItem( 另存为 .);item5=new JMenuItem( 退出 );item5.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(e.getSource()=item4)int a = JOptionPane.showConfirmDialog(null, 文件已被改变, 是否要保存?,提示 ,JO
27、ptionPane.YES_NO_CANCEL_OPTION);if(a=1)this.dispose();else if(a=0)File f2 = null;JFileChooser jfc2 = new JFileChooser();int num2 = jfc2.showSaveDialog(null);if(num2=JFileChooser.APPROVE_OPTION)f2=jfc2.getSelectedFile();this.setTitle(f2.getName();tryFileWriter fw = new FileWriter(f2);BufferedWriter b
28、w = new BufferedWriter(fw);bw.write(text.getText();bw.close();fw.close();catch(IOException e2)e2.printStackTrace();this.dispose();private void setTitle(String name) / TODO Auto-generated method stubprivate void dispose() / TODO Auto-generated method stub);item1.setAccelerator(KeyStroke.getKeyStroke(
29、KeyEvent.VK_N,InputEvent.CTRL_MASK);item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK);item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK);menu1.add(item1);menu1.add(item2);menu1.add(item3);menu1.add(item4);menu1.add(item5);item21=new JMenuIte
30、m( 撤销 );/编辑菜单下菜单项的创建item22=new JMenuItem( 剪切 );item23=new JMenuItem( 复制 );item24=new JMenuItem( 粘贴 );item25=new JMenuItem( 删除 );item26=new JMenuItem( 全选 );item27=new JMenuItem( 时间 / 日期 );item27.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(e.getSource()=item27)Ca
31、lendar c1 =Calendar.getInstance();int y = c1.get(Calendar.YEAR);int m = c1.get(Calendar.MONTH);int d = c1.get(Calendar.DATE);int h = c1.get(Calendar.HOUR);int m1 = c1.get(Calendar.MINUTE);int m2 = m+1;text.setText(y+年+m2+月+d+日+h+:+m1););item21.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,Inpu
32、tEvent.CTRL_MASK);item22.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK);item23.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK);item24.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V ,InputEvent.CTRL_MASK);/item1.setAccelerator(KeyStroke.getKey
33、Stroke(KeyEvent.VK_Del);item26.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK);/item1.setAccelerator(new MenuShortcut(KeyEvent.VK_F5);menu2.add(item21);menu2.add(item22);menu2.add(item23);menu2.add(item24);menu2.add(item25);menu2.add(item26);menu2.add(item27);item31=new JMe
34、nuItem(,自动换行力item32=new JMenuItem( 字体 );item32.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) StyleDialog aa=new StyleDialog(text););item31.setAccelerator(KeyStroke.getKeyStroke(W);item32.setAccelerator(KeyStroke.getKeyStroke(F);menu3.add(item31);menu3.add(item32);i
35、tem31.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) if(item31.getLabel().equals( 自动换行 )item31.setLabel(,自动换行”);text.setLineWrap(true);else item31.setLabel( 自动换行 );text.setLineWrap(false););item41=new JMenuItem( 状态栏 );/查看菜单下菜单项的创建item41.setAccelerator(KeyStroke.getK
36、eyStroke(S);menu4.add(item41);item51=new JMenuItem( 查看帮助 );/帮助菜单下菜单项的创建item52=new JMenuItem( 关于记事本);item52.setAccelerator(KeyStroke.getKeyStroke(A);menu5.add(item51);menu5.add(item52);item22.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) text.cut(););item23.addActio
37、nListener(new ActionListener() public void actionPerformed(ActionEvent e) text.copy(););item24.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.paste(););text.addMouseListener(new MouseAdapter() /鼠标右键实现复制,粘贴,剪切功能public void mousePressed(MouseEvent e)if(e.getModi
38、fiers()=InputEvent.BUTTON3_MASK)menu.show(text,e.getX(),e.getY(););JMenuItem cut = new JMenuItem( 剪切 (T);/添加右键菜单JMenuItem copy = new JMenuItem( 复制 (C);JMenuItem paste = new JMenuItem( 粘贴 (P);menu.add(copy);menu.add(cut);menu.add(paste);/* 按钮事件 */cut.addActionListener(new ActionListener() public void
39、 actionPerformed(ActionEvent e) text.cut(););copy.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.copy(););paste.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.paste(););setVisible(true); / FWindow() 方法结束protected void sa
40、veFile(int status2) / TODO Auto-generated method stubclass StyleDialog extends JDialogJLabel ziti,zixing,daxiao;JPanel style,myshili;JTextArea shili;JButton ok,cancel;JList zitiList,zixingList,daxiaoList;JTextField myziti,myzixing,mydaxiao;String mystyle=Arial,Elephant,Forte, 宋体 ,华文行楷 ,黑体 ,华文彩云 ,华文
41、仿宋 , 楷书 _GB2312, 隶书 ,华文中宋 ;String myxingzhuang= 常规 ,斜体 , 粗体 , 粗斜体 ;String mypounds=9,10,11,12,13,14,15,16,17,18;String daxiao_check,style_check,ziti_check;StyleDialog(JTextArea text)super(frame, 字体 ,false);/setTitle( 字体 );this.setResizable(false);this.setBackground(Color.white);Container container=g
42、etContentPane();container.setLayout(null);ziti=new JLabel( 字体 );ziti.setFont(new Font(Monospaced,Font.PLAIN,12);ziti.setBounds(10,10,50,20);myziti=new JTextField( 宋体 );myziti.setEditable(false);myziti.setBackground(Color.white);myziti.setBounds(10, 35, 100, 20);zitiList=new JList(mystyle);zitiList.s
43、etSelectedIndex(3);zitiList.setFont(new Font(Monospaced,Font.PLAIN,12);zitiList.setVisibleRowCount(5);zitiList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);zitiList.addListSelectionListener(new ListSelectionListener()public void valueChanged(ListSelectionEvent event)myziti.setText(mystylezi
44、tiList.getSelectedIndex();ziti_check=myziti.getText();style_check=myzixing.getText();daxiao_check=mydaxiao.getText();int zitidaxiao=Integer.parseInt(daxiao_check);int zitistyle=Font.PLAIN;if(style_check.equals( 常规 )zitistyle=Font.PLAIN;if(style_check.equals( 斜体 )zitistyle=Font.ITALIC;if(style_check.
45、equals( 粗体 )zitistyle=Font.BOLD;if(style_check.equals( 粗斜体 )zitistyle=Font.BOLD+Font.ITALIC;shili.setFont(new Font(ziti_check,zitistyle,zitidaxiao););JScrollPane scroll=new JScrollPane(zitiList);scroll.setBounds(10, 60,100 ,100);zixing=new JLabel( 字形 );zixing.setFont(new Font(Monospaced,Font.PLAIN,1
46、2);zixing.setBounds(120, 10, 50, 20);myzixing=new JTextField( 常规 );myzixing.setEditable(false);myzixing.setBackground(Color.white);myzixing.setBounds(120, 35, 100, 20);zixingList=new JList(myxingzhuang);zixingList.setSelectedIndex(0);zixingList.setFont(new Font(Monospaced,Font.PLAIN,12);zixingList.s
47、etVisibleRowCount(5);zixingList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);zixingList.addListSelectionListener(new ListSelectionListener()public void valueChanged(ListSelectionEvent event)myzixing.setText(myxingzhuangzixingList.getSelectedIndex();ziti_check=myziti.getText();style_check=my
48、zixing.getText();daxiao_check=mydaxiao.getText();int zitidaxiao=Integer.parseInt(daxiao_check);int zitistyle=Font.PLAIN;if(style_check.equals( 常规 )zitistyle=Font.PLAIN;if(style_check.equals( 斜体 )zitistyle=Font.ITALIC;if(style_check.equals( 粗体 )zitistyle=Font.BOLD;if(style_check.equals( 粗斜体 )zitistyl
49、e=Font.BOLD+Font.ITALIC;shili.setFont(new Font(ziti_check,zitistyle,zitidaxiao););JScrollPane scroll2=new JScrollPane(zixingList);scroll2.setBounds(120, 60,100 ,80);daxiao=new JLabel( 大小 );daxiao.setFont(new Font(Monospaced,Font.PLAIN,12);daxiao.setBounds(230, 10, 50, 20);mydaxiao=new JTextField(12)
50、;mydaxiao.setEditable(false);mydaxiao.setBackground(Color.white);mydaxiao.setBounds(230, 35, 50, 20);daxiaoList=new JList(mypounds);daxiaoList.setSelectedIndex(3);daxiaoList.setFont(new Font(Monospaced,Font.PLAIN,12);daxiaoList.setVisibleRowCount(5);daxiaoList.setSelectionMode(ListSelectionModel.SIN
51、GLE_SELECTION);daxiaoList.addListSelectionListener(new ListSelectionListener()public void valueChanged(ListSelectionEvent event)mydaxiao.setText(mypoundsdaxiaoList.getSelectedIndex();ziti_check=myziti.getText();style_check=myzixing.getText();daxiao_check=mydaxiao.getText();int zitidaxiao=Integer.parseInt(daxiao_check);int zitistyle=Font.PLAIN;if(style_check.equals( 常规 )zitistyle=Font.PLAIN;if(style_check.equals( 斜体 )zitistyle=Font.ITALIC;if(style_ch
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 磷肥生产过程中的质量监控与数据分析考核试卷
- 清代‘公务员手册’:汪辉祖官场智慧解析
- 2025型钢租赁合同范本
- 2025屋顶维修工程补充合同详情解析
- 2025大连市房屋租赁合同范本2
- 二零二五版抵押担保借款合同书
- 大学生职业规划大赛《大学数据科学与大数据技术专业》生涯发展展示
- 2025空调购销合同范文
- 2025年床上用品产品质量监督抽查实施细则
- 2025屋顶涂料施工合同模板
- 分集水器选型计算
- GB/T 8314-2013茶游离氨基酸总量的测定
- GB/T 1410-2006固体绝缘材料体积电阻率和表面电阻率试验方法
- 工业厂房土方回填施工方案1215
- 鲜肉切片机设计说明书
- 2018年USB数据线检验规范资料
- 沥青混凝土拌合站吊装计算书
- 第4章单回路控制系统设计-zhm
- 视觉形象设计VIS清单
- LLC谐振半桥的主电路设计指导
- 工具钳工技能操作鉴定要素细目表09版
评论
0/150
提交评论