《Java程序設(shè)計(jì)》課程綜合性實(shí)驗(yàn)報(bào)告簡易寫字板軟件設(shè)計(jì)
華北科技學(xué)院計(jì)算機(jī)系綜合性實(shí)驗(yàn)報(bào)告華北科技學(xué)院計(jì)算機(jī)系綜合性實(shí)驗(yàn)實(shí) 驗(yàn) 報(bào) 告 課程名稱 Java程序設(shè)計(jì) 實(shí)驗(yàn)學(xué)期 2011 至 2012 學(xué)年 第 1 學(xué)期學(xué)生所在系部 基礎(chǔ)部 年級 2009 專業(yè)班級 計(jì)算B092 學(xué)生姓名 學(xué)號 任課教師 實(shí)驗(yàn)成績 計(jì)算機(jī)系制Java程序設(shè)計(jì)課程綜合性實(shí)驗(yàn)報(bào)告開課實(shí)驗(yàn)室: 基礎(chǔ)實(shí)驗(yàn)室 2011 年 12 月 5 日實(shí)驗(yàn)題目簡易寫字板軟件設(shè)計(jì)一、 實(shí)驗(yàn)?zāi)康膇. 通過編寫Java的應(yīng)用系統(tǒng)綜合實(shí)例簡易寫字板軟件,總結(jié)、回顧和實(shí)踐面向?qū)ο蟮木幊趟枷胍约熬幊谭椒?,并通過編寫程序來掌握J(rèn)ava語言編程技巧,將學(xué)習(xí)到的知識融會貫通,同時提高調(diào)試程序的能力,養(yǎng)成良好的編程習(xí)慣,并增強(qiáng)對程序設(shè)計(jì)整體思路的把握。ii. 熟悉在集成開發(fā)環(huán)境下編寫Java程序。二、 設(shè)備與環(huán)境iii. 硬件:多媒體計(jì)算機(jī)iv. 軟件:Windows 2000以上的操作系統(tǒng)、JDK開發(fā)包、集成開發(fā)環(huán)境三、 實(shí)驗(yàn)內(nèi)容v. 總體要求:程序功能基本完成,用戶界面友好,代碼的可維護(hù)性和可擴(kuò)展性較好,最好包括必要的注釋和相應(yīng)的文檔。vi. 具體要求:基本的文本錄入,字符修改,刪除,換行,粘貼,復(fù)制,剪切等功能,主要包括:2. 菜單欄設(shè)計(jì)及功能實(shí)現(xiàn):菜單欄主要包括文件、編輯、格式、幫助等常見菜單。其中文件菜單有新建、打開、保存、另存為、退出等功能;編輯菜單有剪切、復(fù)制、粘貼、等功能;格式菜單含有對文字字體、字型、字號及顏色等的設(shè)置功能,根據(jù)情況可在編輯菜單添加查找和查找替換功能。3. 編輯區(qū)設(shè)計(jì):對文本的編輯處理區(qū)。4. 工具欄設(shè)計(jì):可根據(jù)需要增加該項(xiàng)功能。5. 其他功能設(shè)計(jì)。四、 實(shí)驗(yàn)結(jié)果及分析1 主界面的設(shè)計(jì)與實(shí)現(xiàn)a) 整個界面共包括四個部分,包括:標(biāo)題欄、菜單欄、編輯區(qū)、狀態(tài)欄以及滾動條,如圖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. 菜單的設(shè)計(jì)a) 本程序共設(shè)計(jì)四個菜單,分別是文件、編輯、格式、幫助四個菜單的功能, b) 針對記事本的需求,文件菜單欄實(shí)現(xiàn)了新建、打開、保存、另存為、退出的功能。如圖二所示。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("關(guān)閉(C)");menuItem04.setMnemonic(C);a) 編輯欄中實(shí)現(xiàn)了常見的剪切、復(fù)制、粘貼、查找與替換、全選以及時間/日期的功能。如圖三所示。b)9. 圖表 3編輯菜單欄menuItem10=new JMenuItem("剪切(X)");menuItem10.setMnemonic(X);menuItem11=new JMenuItem("復(fù)制(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) 格式欄中實(shí)現(xiàn)了自動換行以及字體設(shè)置功能。如圖四所示b)10. 圖表 4格式菜單欄menuItem20=new JCheckBoxMenuItem("自動換行(W)");menuItem20.setMnemonic(T);i. 在字體設(shè)置中實(shí)現(xiàn)了字體,字號、字形三個功能,如圖五所示。11.12.13. 圖表 5字體設(shè)置選項(xiàng)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="常規(guī)","粗體","斜體","粗斜體"Container ctn;SchInfo schInfo;GraphicsEnvironment ge;Font defaultFont=new Font("宋體", Font.PLAIN, 12), fonts;public zhaoliyang514()super("無標(biāo)題 - 記事本");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("常規(guī)");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. 幫助欄實(shí)現(xiàn)了關(guān)于記事本的介紹功能,如圖六所示。14.15. 圖表 6幫助欄選項(xiàng)if (ae.getSource()=menuItem40)JOptionPane.showMessageDialog(null,"基礎(chǔ)部,計(jì)算B092 趙禮陽","關(guān)于 記事本",JOptionPane.INFORMATION_MESSAGE);16. 編輯區(qū)設(shè)計(jì)i. 在編輯區(qū)中通過getContentPane().add(new JScrollPane(text)成功實(shí)現(xiàn)了將要輸入的文本成功輸入到編輯區(qū)中,并且這句還增加了文本的滾動條,增加了記事本的實(shí)用性。2 文件菜單中各項(xiàng)功能的實(shí)現(xiàn)ii. 在設(shè)計(jì)記事本中成功完成了文件欄中的新建、打開、保存、另存為、退出的功能。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、格式菜單中各項(xiàng)功能的實(shí)現(xiàn)i. 在設(shè)計(jì)記事本的格式欄中成功完成了自動換行以及字體的設(shè)置功能。其中在字體中包括了字體、字號、字形三個設(shè)置,達(dá)到了對字體設(shè)置的基本要求。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("區(qū)分大小寫", 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("查找內(nèi)容:");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("區(qū)分大小寫", 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("查找內(nèi)容:");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="常規(guī)","粗體","斜體","粗斜體"Container ctn;SchInfo schInfo;GraphicsEnvironment ge;Font defaultFont=new Font("宋體", Font.PLAIN, 12), fonts;public zhaoliyang514()super("無標(biāo)題 - 記事本");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("常規(guī)");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("在這里設(shè)置字體");cbSize.setToolTipText("在這里設(shè)置字體大小");cbStyle.setToolTipText("在這里設(shè)置字體風(fēng)格");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("行數(shù):"+ta.getLineCount();changed=true;public void insertUpdate(DocumentEvent de)lblStatus.setText("行數(shù):"+ta.getLineCount();changed=true;public void removeUpdate(DocumentEvent de)lblStatus.setText("行數(shù):"+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("關(guān)閉(C)");menuItem04.setMnemonic(C);menuItem10=new JMenuItem("剪切(X)");menuItem10.setMnemonic(X);menuItem11=new JMenuItem("復(fù)制(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("狀態(tài)欄(S)", true);menuItem30.setMnemonic(S);menuItem40=new JMenuItem("關(guān)于(A) .");menuItem40.setMnemonic(S);popupMenuItem=new JMenuItem4;popupMenuItem0=new JMenuItem("剪切(X)");popupMenuItem0.setMnemonic(X);popupMenuItem1=new JMenuItem("復(fù)制(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