《Java程序设计》课程综合性实验报告-简易写字板软件设计.doc

上传人:西安人 文档编号:11033301 上传时间:2021-06-19 格式:DOC 页数:24 大小:668KB
返回 下载 相关 举报
《Java程序设计》课程综合性实验报告-简易写字板软件设计.doc_第1页
第1页 / 共24页
《Java程序设计》课程综合性实验报告-简易写字板软件设计.doc_第2页
第2页 / 共24页
《Java程序设计》课程综合性实验报告-简易写字板软件设计.doc_第3页
第3页 / 共24页
《Java程序设计》课程综合性实验报告-简易写字板软件设计.doc_第4页
第4页 / 共24页
《Java程序设计》课程综合性实验报告-简易写字板软件设计.doc_第5页
第5页 / 共24页
点击查看更多>>
资源描述

《《Java程序设计》课程综合性实验报告-简易写字板软件设计.doc》由会员分享,可在线阅读,更多相关《《Java程序设计》课程综合性实验报告-简易写字板软件设计.doc(24页珍藏版)》请在三一文库上搜索。

1、华北科技学院计算机系综合性实验报告华北科技学院计算机系综合性实验实 验 报 告 课程名称 Java程序设计 实验学期 2011 至 2012 学年 第 1 学期学生所在系部 基础部 年级 2009 专业班级 计算B092 学生姓名 学号 任课教师 实验成绩 计算机系制Java程序设计课程综合性实验报告开课实验室: 基础实验室 2011 年 12 月 5 日实验题目简易写字板软件设计一、 实验目的i. 通过编写Java的应用系统综合实例简易写字板软件,总结、回顾和实践面向对象的编程思想以及编程方法,并通过编写程序来掌握Java语言编程技巧,将学习到的知识融会贯通,同时提高调试程序的能力,养成良好

2、的编程习惯,并增强对程序设计整体思路的把握。ii. 熟悉在集成开发环境下编写Java程序。二、 设备与环境iii. 硬件:多媒体计算机iv. 软件:Windows 2000以上的操作系统、JDK开发包、集成开发环境三、 实验内容v. 总体要求:程序功能基本完成,用户界面友好,代码的可维护性和可扩展性较好,最好包括必要的注释和相应的文档。vi. 具体要求:基本的文本录入,字符修改,删除,换行,粘贴,复制,剪切等功能,主要包括:2. 菜单栏设计及功能实现:菜单栏主要包括文件、编辑、格式、帮助等常见菜单。其中文件菜单有新建、打开、保存、另存为、退出等功能;编辑菜单有剪切、复制、粘贴、等功能;格式菜单

3、含有对文字字体、字型、字号及颜色等的设置功能,根据情况可在编辑菜单添加查找和查找替换功能。3. 编辑区设计:对文本的编辑处理区。4. 工具栏设计:可根据需要增加该项功能。5. 其他功能设计。四、 实验结果及分析1 主界面的设计与实现a) 整个界面共包括四个部分,包括:标题栏、菜单栏、编辑区、状态栏以及滚动条,如图1所示。b)6. 图表 1记事本的整体布局menuBar=new JMenuBar();setJMenuBar(menuBar);menu=new JMenu5;menuItem=new JMenuItemmenu.length;menu0=new JMenu(文件(F);menu0.

4、setMnemonic(F);menu1=new JMenu(编辑(E);menu1.setMnemonic(E);menu2=new JMenu(格式(O);menu2.setMnemonic(O);menu3=new JMenu(查看(V);menu3.setMnemonic(V);menu4=new JMenu(帮助(H);menu4.setMnemonic(H);7. 菜单的设计a) 本程序共设计四个菜单,分别是文件、编辑、格式、帮助四个菜单的功能, b) 针对记事本的需求,文件菜单栏实现了新建、打开、保存、另存为、退出的功能。如图二所示。c)8. 图表 2文件菜单栏menuItem0

5、=new JMenuItem5;menuItem1=new JMenuItem7;menuItem2=new JMenuItem2;menuItem3=new JMenuItem1;menuItem4=new JMenuItem1;menuItem00=new JMenuItem(新建(N);menuItem00.setMnemonic(N);menuItem01=new JMenuItem(打开(O) .);menuItem01.setMnemonic(O);menuItem02=new JMenuItem(保存(S);menuItem02.setMnemonic(S);menuItem03

6、=new JMenuItem(另存为(A) .);menuItem03.setMnemonic(A); menuItem04=new JMenuItem(关闭(C);menuItem04.setMnemonic(C);a) 编辑栏中实现了常见的剪切、复制、粘贴、查找与替换、全选以及时间/日期的功能。如图三所示。b)9. 图表 3编辑菜单栏menuItem10=new JMenuItem(剪切(X);menuItem10.setMnemonic(X);menuItem11=new JMenuItem(复制(C);menuItem11.setMnemonic(C);menuItem12=new J

7、MenuItem(粘贴(P);menuItem12.setMnemonic(P);menuItem13=new JMenuItem(查找(F);menuItem13.setMnemonic(F);menuItem14=new JMenuItem(替换(R);menuItem14.setMnemonic(R);menuItem15=new JMenuItem(全选(A);menuItem15.setMnemonic(A);menuItem16=new JMenuItem(时间/日期(T);menuItem16.setMnemonic(T);a) 格式栏中实现了自动换行以及字体设置功能。如图四所示

8、b)10. 图表 4格式菜单栏menuItem20=new JCheckBoxMenuItem(自动换行(W);menuItem20.setMnemonic(T);i. 在字体设置中实现了字体,字号、字形三个功能,如图五所示。11.12.13. 图表 5字体设置选项JTextArea ta;JScrollPane scr;JMenuBar menuBar;JMenu menu;JMenuItem menuItem, popupMenuItem;JPopupMenu popupMenu;JLabel lblStatus;JComboBox cbFont, cbSize, cbStyle;bool

9、ean changed;String curFileName;String schText=new String2;String strFonts;String strSize=8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72;String strStyle=常规,粗体,斜体,粗斜体;Container ctn;SchInfo schInfo;GraphicsEnvironment ge;Font defaultFont=new Font(宋体, Font.PLAIN, 12), fonts;public zhaoliyang514()super(无标

10、题 - 记事本);changed=false;curFileName=;lblStatus=new JLabel(就绪);ta=new JTextArea();scr=new JScrollPane(ta);ctn=getContentPane();ge=GraphicsEnvironment.getLocalGraphicsEnvironment();strFonts=ge.getAvailableFontFamilyNames();cbFont=new JComboBox(strFonts);cbFont.setFont(defaultFont);cbFont.setSelectedIte

11、m(宋体);cbSize=new JComboBox(strSize);cbSize.setFont(defaultFont);cbSize.setSelectedItem(12);cbStyle=new JComboBox(strStyle);cbFont.setSelectedItem(常规);cbStyle.setFont(defaultFont);cbFont.addItemListener(new ItemListener()public void itemStateChanged(ItemEvent ie)int size, style;String fontName;fontNa

12、me=(String)cbFont.getSelectedItem();style=cbStyle.getSelectedIndex();size=Integer.parseInt(String)cbSize.getSelectedItem();Font font=new Font(fontName, style, size);ta.setFont(font););i. 帮助栏实现了关于记事本的介绍功能,如图六所示。14.15. 图表 6帮助栏选项if (ae.getSource()=menuItem40)JOptionPane.showMessageDialog(null,基础部,计算B09

13、2 赵礼阳,关于 记事本,JOptionPane.INFORMATION_MESSAGE);16. 编辑区设计i. 在编辑区中通过getContentPane().add(new JScrollPane(text)成功实现了将要输入的文本成功输入到编辑区中,并且这句还增加了文本的滚动条,增加了记事本的实用性。2 文件菜单中各项功能的实现ii. 在设计记事本中成功完成了文件栏中的新建、打开、保存、另存为、退出的功能。17.18. 图表 7打开功能public void open()JFileChooser fc=new JFileChooser();if (fc.showOpenDialog(t

14、his)=JFileChooser.APPROVE_OPTION)String _curFileName=fc.getSelectedFile().getAbsolutePath();File file=new File(_curFileName);if (file.exists()curFileName=_curFileName;File f=new File(curFileName);setTitle(f.getName()+ - 记事本);changed=false;readFile(file);elseJOptionPane.showMessageDialog(null,file.ge

15、tAbsolutePath()+n找不到文件。n请检查所给的文件名是否正确。n,打开失败,JOptionPane.ERROR_MESSAGE);19.20. 图表 8保存及另存为功能public void saveAs()JFileChooser fc=new JFileChooser();fc.setDialogTitle(另存为);if (fc.showSaveDialog(this)=JFileChooser.APPROVE_OPTION)boolean overwrite=false;String _curFileName=fc.getSelectedFile().getAbsolut

16、ePath();File file=new File(_curFileName);if (file.exists()int exit;exit=JOptionPane.showConfirmDialog(this,file.getAbsolutePath()+已存在。n要覆盖它吗? ,另存为,JOptionPane.YES_NO_OPTION);if (exit=JOptionPane.YES_OPTION)overwrite=true;elseoverwrite=true;if (overwrite)changed=false;saveFile(file);curFileName=_curF

17、ileName;File f=new File(curFileName);setTitle(f.getName()+ - 记事本);21. 3、格式菜单中各项功能的实现i. 在设计记事本的格式栏中成功完成了自动换行以及字体的设置功能。其中在字体中包括了字体、字号、字形三个设置,达到了对字体设置的基本要求。22. 4、程序源代码import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.util.*;class Sear

18、cher extends JDialogprivate JLabel Sample = new JLabel();private int curRow, curCol;private JTextField tfS;private JCheckBox chkCaseSensible;private JButton btnOK;private JButton btnCancel;private Searcher dlg;public static SchInfo schInfo;private JTextArea ta;public Searcher(Frame parent, boolean m

19、odal, JTextArea _ta)super(parent,modal);schInfo=new SchInfo();ta=_ta;Container ctn=getContentPane();JPanel ctn1=new JPanel();JPanel ctn2=new JPanel();JPanel ctn3=new JPanel();JPanel ctn4=new JPanel();btnOK=new JButton(查找下一个);btnCancel=new JButton(取消);initAll();chkCaseSensible=new JCheckBox(区分大小写, fa

20、lse);tfS=new JTextField(10);ctn.setLayout(new FlowLayout();ctn.add(ctn1);ctn.add(ctn2);ctn1.setLayout(new GridLayout(2, 1);ctn1.add(ctn3);ctn1.add(ctn4);ctn3.setLayout(new FlowLayout();ctn4.setLayout(new FlowLayout();ctn3.add(new JLabel(查找内容:);ctn3.add(tfS);ctn4.add(chkCaseSensible);ctn2.setLayout(n

21、ew GridLayout(2, 1);ctn2.add(btnOK);ctn2.add(btnCancel);setTitle(查找);btnOK.addActionListener(new ActionListener()public void actionPerformed(ActionEvent ae)schInfo.str=tfS.getText();schInfo.caseSensible=chkCaseSensible.isSelected();String str=ta.getText();int index;if (schInfo.caseSensible)index=str

22、.indexOf(schInfo.str, ta.getCaretPosition();elseindex=str.toUpperCase().indexOf(schInfo.str.toUpperCase(), ta.getCaretPosition();if (index!=-1)ta.select(index, index+schInfo.str.length();elseJOptionPane.showMessageDialog(null,找不到+schInfo.str+,记事本,JOptionPane.INFORMATION_MESSAGE););btnCancel.addActio

23、nListener(new ActionListener()public void actionPerformed(ActionEvent ae)schInfo.str=;schInfo.caseSensible=false;setVisible(false););setVisible(true);private void initAll()setSize(400,140);setLocation(350,150);addWindowListener(new WindowAdapter()public void windowClosing(java.awt.event.WindowEvent

24、e)setVisible (false););class SchInfopublic String str;public boolean caseSensible;class Replacer extends JDialogprivate JLabel Sample = new JLabel();private int curRow, curCol;private JTextField tfS, tfR;private JCheckBox chkCaseSensible;private JButton btnOK, btnReplace;private JButton btnCancel;pr

25、ivate Replacer dlg;public static RplInfo schInfo;private JTextArea ta;public Replacer(Frame parent, boolean modal, JTextArea _ta)super(parent,modal);schInfo=new RplInfo();ta=_ta;Container ctn=getContentPane();JPanel ctn1=new JPanel();JPanel ctn2=new JPanel();JPanel ctn3=new JPanel();JPanel ctn4=new

26、JPanel();JPanel ctn5=new JPanel();btnOK=new JButton(查找下一个);btnReplace=new JButton(替换);btnCancel=new JButton(取消);initAll();chkCaseSensible=new JCheckBox(区分大小写, false);tfS=new JTextField(10);tfR=new JTextField(10);ctn.setLayout(new FlowLayout();ctn.add(ctn1);ctn.add(ctn2);ctn1.setLayout(new GridLayout

27、(3,1);ctn1.add(ctn3);ctn1.add(ctn4);ctn1.add(ctn5);ctn3.setLayout(new FlowLayout();ctn4.setLayout(new FlowLayout();ctn5.setLayout(new FlowLayout();ctn3.add(new JLabel(查找内容:);ctn3.add(tfS);ctn4.add(new JLabel(替换为:);ctn4.add(tfR);ctn5.add(chkCaseSensible);ctn2.setLayout(new GridLayout(3, 1);ctn2.add(b

28、tnOK);ctn2.add(btnReplace);ctn2.add(btnCancel);setTitle(替换);btnOK.addActionListener(new ActionListener()public void actionPerformed(ActionEvent ae)schInfo.str=tfS.getText();schInfo.caseSensible=chkCaseSensible.isSelected();String str=ta.getText();int index;if (schInfo.caseSensible)index=str.indexOf(

29、schInfo.str, ta.getCaretPosition();elseindex=str.toUpperCase().indexOf(schInfo.str.toUpperCase(), ta.getCaretPosition();if (index!=-1)ta.select(index, index+schInfo.str.length();elseJOptionPane.showMessageDialog(null,找不到+schInfo.str+,记事本,JOptionPane.INFORMATION_MESSAGE););btnReplace.addActionListene

30、r(new ActionListener()public void actionPerformed(ActionEvent ae)String str=ta.getSelectedText();int index;schInfo.str=tfS.getText();if (str!=null & str.equals(tfS.getText()index=ta.getCaretPosition()-tfS.getText().length();elseschInfo.caseSensible=chkCaseSensible.isSelected();str=ta.getText();if (s

31、chInfo.caseSensible)index=str.indexOf(schInfo.str, ta.getCaretPosition();elseindex=str.toUpperCase().indexOf(schInfo.str.toUpperCase(), ta.getCaretPosition();str=ta.getText();if (index!=-1)str.replaceFirst(tfS.getText(), tfR.getText();String strTemp=str.substring(0, index)+tfR.getText()+str.substrin

32、g(index+tfS.getText().length(), str.length();str=strTemp;ta.setText(str);ta.setCaretPosition(index+tfR.getText().length();elseJOptionPane.showMessageDialog(null,找不到+schInfo.str+,记事本,JOptionPane.INFORMATION_MESSAGE););btnCancel.addActionListener(new ActionListener()public void actionPerformed(ActionE

33、vent ae)schInfo.str=;schInfo.caseSensible=false;setVisible(false););setVisible(true);private void initAll()setSize(400,140);setLocation(350,150);addWindowListener(new WindowAdapter()public void windowClosing(java.awt.event.WindowEvent e)setVisible (false););class RplInfo extends SchInfopublic String

34、 strR;public class zhaoliyang514 extends JFrame implements ActionListenerJTextArea ta;JScrollPane scr;JMenuBar menuBar;JMenu menu;JMenuItem menuItem, popupMenuItem;JPopupMenu popupMenu;JLabel lblStatus;JComboBox cbFont, cbSize, cbStyle;boolean changed;String curFileName;String schText=new String2;St

35、ring strFonts;String strSize=8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72;String strStyle=常规,粗体,斜体,粗斜体;Container ctn;SchInfo schInfo;GraphicsEnvironment ge;Font defaultFont=new Font(宋体, Font.PLAIN, 12), fonts;public zhaoliyang514()super(无标题 - 记事本);changed=false;curFileName=;lblStatus=new JLabel(就绪)

36、;ta=new JTextArea();scr=new JScrollPane(ta);ctn=getContentPane();ge=GraphicsEnvironment.getLocalGraphicsEnvironment();strFonts=ge.getAvailableFontFamilyNames();cbFont=new JComboBox(strFonts);cbFont.setFont(defaultFont);cbFont.setSelectedItem(宋体);cbSize=new JComboBox(strSize);cbSize.setFont(defaultFo

37、nt);cbSize.setSelectedItem(12);cbStyle=new JComboBox(strStyle);cbFont.setSelectedItem(常规);cbStyle.setFont(defaultFont);cbFont.addItemListener(new ItemListener()public void itemStateChanged(ItemEvent ie)int size, style;String fontName;fontName=(String)cbFont.getSelectedItem();style=cbStyle.getSelecte

38、dIndex();size=Integer.parseInt(String)cbSize.getSelectedItem();Font font=new Font(fontName, style, size);ta.setFont(font););cbSize.addItemListener(new ItemListener()public void itemStateChanged(ItemEvent ie)int size, style;String fontName;fontName=(String)cbFont.getSelectedItem();style=cbStyle.getSe

39、lectedIndex();size=Integer.parseInt(String)cbSize.getSelectedItem();Font font=new Font(fontName, style, size);ta.setFont(font););cbStyle.addItemListener(new ItemListener()public void itemStateChanged(ItemEvent ie)int size, style;String fontName;fontName=(String)cbFont.getSelectedItem();style=cbStyle.getSelectedIndex();size=Integer.parseInt(String)cbSize.getSelectedItem();Font font=new Font(fontName, style, size);ta.setFont(font););cbFont.setToolTipText(在这里设置字体);cbSize.setToolTipTex

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 研究报告 > 商业贸易


经营许可证编号:宁ICP备18001539号-1