资源描述
华北科技学院计算机系综合性实验报告华北科技学院计算机系综合性实验实 验 报 告 课程名称 Java程序设计 实验学期 2011 至 2012 学年 第 1 学期学生所在系部 基础部 年级 2009 专业班级 计算B092 学生姓名 学号 任课教师 实验成绩 计算机系制Java程序设计课程综合性实验报告开课实验室: 基础实验室 2011 年 12 月 5 日实验题目简易写字板软件设计一、 实验目的i. 通过编写Java的应用系统综合实例简易写字板软件,总结、回顾和实践面向对象的编程思想以及编程方法,并通过编写程序来掌握Java语言编程技巧,将学习到的知识融会贯通,同时提高调试程序的能力,养成良好的编程习惯,并增强对程序设计整体思路的把握。ii. 熟悉在集成开发环境下编写Java程序。二、 设备与环境iii. 硬件:多媒体计算机iv. 软件:Windows 2000以上的操作系统、JDK开发包、集成开发环境三、 实验内容v. 总体要求:程序功能基本完成,用户界面友好,代码的可维护性和可扩展性较好,最好包括必要的注释和相应的文档。vi. 具体要求:基本的文本录入,字符修改,删除,换行,粘贴,复制,剪切等功能,主要包括:2. 菜单栏设计及功能实现:菜单栏主要包括文件、编辑、格式、帮助等常见菜单。其中文件菜单有新建、打开、保存、另存为、退出等功能;编辑菜单有剪切、复制、粘贴、等功能;格式菜单含有对文字字体、字型、字号及颜色等的设置功能,根据情况可在编辑菜单添加查找和查找替换功能。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.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=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=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 JMenuItem("粘贴(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) 格式栏中实现了自动换行以及字体设置功能。如图四所示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;boolean 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("无标题 - 记事本");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.setSelectedItem("宋体");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;fontName=(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,"基础部,计算B092 赵礼阳","关于 记事本",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(this)=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.getAbsolutePath()+"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().getAbsolutePath();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=_curFileName;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 Searcher 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 modal, 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("区分大小写", false);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(new 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.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.addActionListener(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 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;private 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 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(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(btnOK);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(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.addActionListener(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 (schInfo.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.substring(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(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 e)setVisible (false););class RplInfo extends SchInfopublic String 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;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("无标题 - 记事本");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.setSelectedItem("宋体");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;fontName=(String)cbFont.getSelectedItem();style=cbStyle.getSelectedIndex();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.getSelectedIndex();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.setToolTipText("在这里设置字体大小");cbStyle.setToolTipText("在这里设置字体风格");JPanel panel=new JPanel();panel.add(cbFont);panel.add(cbSize);panel.add(cbStyle);ctn.setLayout(new BorderLayout();ctn.add(panel, BorderLayout.NORTH);ctn.add(scr, BorderLayout.CENTER);ctn.add(lblStatus, BorderLayout.SOUTH);showMenu();schText0=new String("");schText1=new String("");lblStatus.setFont(defaultFont);ta.setFont(defaultFont);ta.getDocument().addDocumentListener(new DocumentListener()public void changedUpdate(DocumentEvent de)lblStatus.setText("行数:"+ta.getLineCount();changed=true;public void insertUpdate(DocumentEvent de)lblStatus.setText("行数:"+ta.getLineCount();changed=true;public void removeUpdate(DocumentEvent de)lblStatus.setText("行数:"+ta.getLineCount();changed=true;);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent we)closeAndSave();System.exit(0););ta.addMouseListener(new MouseAdapter()public void mouseReleased(MouseEvent me)if (me.isPopupTrigger()popupMenu.show(zhaoliyang514.this, me.getX()+10, me.getY()+80););public void showMenu()menuBar=new JMenuBar();setJMenuBar(menuBar);menu=new JMenu5;menuItem=new JMenuItemmenu.length;menu0=new JMenu("文件(F)");menu0.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');menuItem0=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=new JMenuItem("另存为(A) .");menuItem03.setMnemonic('A');menuItem04=new JMenuItem("关闭(C)");menuItem04.setMnemonic('C');menuItem10=new JMenuItem("剪切(X)");menuItem10.setMnemonic('X');menuItem11=new JMenuItem("复制(C)");menuItem11.setMnemonic('C');menuItem12=new JMenuItem("粘贴(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');menuItem20=new JCheckBoxMenuItem("自动换行(W)");menuItem20.setMnemonic('T');menuItem21=new JMenuItem("字体.");menuItem21.setEnabled(false);menuItem30=new JCheckBoxMenuItem("状态栏(S)", true);menuItem30.setMnemonic('S');menuItem40=new JMenuItem("关于(A) .");menuItem40.setMnemonic('S');popupMenuItem=new JMenuItem4;popupMenuItem0=new JMenuItem("剪切(X)");popupMenuItem0.setMnemonic('X');popupMenuItem1=new JMenuItem("复制(C)");popupMenuItem1.setMnemonic('C');popupMenuItem2=new JMenuItem("粘贴(P)");popupMenuItem2.setMnemonic('P');popupMenuItem3=new JMenuItem("全选(A)");popupMenuItem3.setMnemonic('A');popupMenu=new JPopupMenu();for(int i=0; i<menu.length; i+)menuBar.add(menui);menui.setFont(defaultFont);for (int j=0; j<menuItemi.length; j+)menui.add(menuItemij);menuItemij.addActionListener(this);menuItemij.setFont(defaultFont);if (i=0 && j=3 | i=1 && j=2 | i=1 && j=4)menui.addSeparator();for (int i=0; i<popupMenuItem.length; i+)popupMenu.add(popupMenuItemi);popupMenuItemi.addActionListener(this);popupMenuItemi.setFont(defaultFont);if (i=2)popupMenu.addSeparator();public void readFile(File file)ta.setText("");tryBufferedReader in=new BufferedReader(new FileReader(file);String read;char readBuf=new char20480;int n=0;while (true)n=in.read(readBuf, 0, 20480);if (n=-1)break;read=new String(readBuf, 0, n);ta.append(read);in.close();ta.setCaretPosition(0);catch (IOException ioe)JOptionPane.showMessageDialog(null,"I/O 错误:"+ioe+"。n","打开失败",JOptionPane.ERROR_MESSAGE);public void saveFile(File file)tryFileWriter fw_out=new FileWriter(file);PrintWriter out=new PrintWriter(new BufferedWriter(fw_out);String outData=ta.getText();String outLine;outLine=outData.spli
展开阅读全文