




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、鍵入文字實(shí)驗(yàn)報告(2013 / 2014 學(xué)年 第 二 學(xué)期)課 程 名 稱Java語言程序設(shè)計實(shí) 驗(yàn) 名 稱綜合圖形界面程序設(shè)計實(shí) 驗(yàn) 時 間2014年5月5日指 導(dǎo) 單 位計算機(jī)學(xué)院 軟件教學(xué)中心指 導(dǎo) 教 師薛景學(xué) 生 姓 名 周媛班 級 學(xué) 號12001108學(xué) 院(系)計算機(jī)學(xué)院專 業(yè)計算機(jī)科學(xué)與技術(shù)(信息安全)3實(shí)驗(yàn)名稱綜合圖形界面程序設(shè)計指導(dǎo)教師薛景實(shí)驗(yàn)類型上機(jī)實(shí)驗(yàn)學(xué)時2實(shí)驗(yàn)時間2014-5-5一、實(shí)驗(yàn)?zāi)康?.學(xué)習(xí)使用Java Swing設(shè)計GUI界面2.學(xué)習(xí)Java的事件監(jiān)聽機(jī)制的基本原理3.學(xué)習(xí)監(jiān)聽器處理Java中的事件二、實(shí)驗(yàn)環(huán)境1.每位同學(xué)配備實(shí)驗(yàn)計算機(jī)一臺2.安裝JDK
2、和Eclipse三、實(shí)驗(yàn)內(nèi)容1、編寫一個算數(shù)測試小軟件,用來訓(xùn)練小學(xué)生的算數(shù)能力。程序有3個類組成,其中Teacher對象充當(dāng)監(jiān)視器,負(fù)責(zé)給出算術(shù)題目,并判斷回答者的答案是否正確;ComputerFrame對象負(fù)責(zé)為算數(shù)題目提供視圖,比如用戶可以通過ComputerFrame對象提供的GUI界面看到題目,并通過該GUI界面給出題目的答案;MainClass是軟件的主類。(請?jiān)谙路娇瞻滋幪顚懕境绦虻娜砍绦虼a及軟件界面截圖)public class Mainclass public static void main(String args) ComputerFrame frame; frame
3、=new ComputerFrame(); frame.setTitle("算術(shù)測試"); frame.setBounds(100,100,650,180); import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ComputerFrame extends JFrame JMenuBar menubar; JMenu choiceGrade; JMenuItem grade1,grade2; JTextField textOne,textTwo,textResult; JBut
4、ton getProblem,giveAnswer; JLabel operatorLabel,message; Teacher teacherZhang; ComputerFrame() teacherZhang=new Teacher(); teacherZhang.setMaxInteger(20); setLayout(new FlowLayout(); menubar = new JMenuBar(); choiceGrade=new JMenu("選擇級別"); grade1=new JMenuItem("幼兒級別"); grade2=new
5、 JMenuItem("兒童級別"); grade1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) teacherZhang.setMaxInteger(10); ); grade2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) teacherZhang.setMaxInteger(50); ); choiceGrade.add(grade1
6、); choiceGrade.add(grade2); menubar.add(choiceGrade); setJMenuBar(menubar); textOne=new JTextField(5);/創(chuàng)建textOne,其可見字符長是5 textTwo=new JTextField(5); textResult=new JTextField(5); operatorLabel=new JLabel("+"); operatorLabel.setFont(new Font("Arial",Font.BOLD,20); message=new JLab
7、el("你還沒有回答呢"); getProblem=new JButton("獲取題目"); giveAnswer=new JButton("確認(rèn)答案"); add(getProblem); add(textOne); add(operatorLabel); add(textTwo); add(new JLabel("="); add(textResult); add(giveAnswer); add(message); textResult.requestFocus(); textOne.setEditable(
8、false); textTwo.setEditable(false); getProblem.setActionCommand("getProblem"); textResult.setActionCommand("answer"); giveAnswer.setActionCommand("answer"); teacherZhang.setJTextField(textOne,textTwo,textResult); teacherZhang.setJLabel(operatorLabel,message); getProblem
9、.addActionListener(teacherZhang); giveAnswer.addActionListener(teacherZhang); textResult.addActionListener(teacherZhang); setVisible(true); validate(); setDefaultCloseOperation(DISPOSE_ON_CLOSE); import java.util.Random;import java.awt.event.*;import javax.swing.*;public class Teacher implements Act
10、ionListener int numberOne,numberTwo; String operator="" boolean isRight; Random random; int maxInteger; JTextField textOne,textTwo,textResult; JLabel operatorLabel,message; Teacher() random=new Random(); public void setMaxInteger(int n) maxInteger=n; public void actionPerformed(ActionEvent
11、 e) String str=e.getActionCommand(); if(str.equals("getProblem") numberOne=random.nextInt(maxInteger)+1; numberTwo=random.nextInt(maxInteger)+1; double d=Math.random(); if(d>=0.5) operator="+" else operator="-" textOne.setText(""+numberOne); textTwo.setText
12、(""+numberTwo); operatorLabel.setText(operator); message.setText("請回答"); textResult.setText(null); else if(str.equals("answer") String answer=textResul.getText(); try int result=Integer.parseInt(answer); if(operator.equals("+") if(result=numberOne+numberTwo) m
13、essage.setText("你回答正確"); else message.setText("你回答錯誤"); else if(operator.equals("-") if(result=numberOne-numberTwo) message.setText("你回答正確"); else message.setText("你回答錯誤"); catch(NumberFormatException ex) message.setText("請輸入數(shù)字字符"); public
14、void setJTextField(JTextField.t) textOne=t0; textTwo=t1; textResult=t2; public void setJLabel(JLabel .label) operatorLabel=label0; message=label1; 2、編寫一個簡單的計算器軟件,實(shí)現(xiàn)簡單的四則運(yùn)算。(請?jiān)谙路娇瞻滋幪顚懕境绦虻娜砍绦虼a及軟件界面截圖)import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JF
15、rame private Container container; private GridBagLayout layout; private GridBagConstraints constraints; private JTextField displayField;/ 計算結(jié)果顯示區(qū) private String lastCommand; private double result;/ 保存計算結(jié)果 private boolean start;/ 判斷是否為數(shù)字的開始 public Calculator() super("Calculator"); container
16、 = getContentPane(); layout = new GridBagLayout(); container.setLayout(layout); constraints = new GridBagConstraints(); start = true; result = 0; lastCommand = "=" displayField = new JTextField(20); displayField.setHorizontalAlignment(JTextField.RIGHT); constraints.gridx = 0; constraints.g
17、ridy = 0; constraints.gridwidth = 4; constraints.gridheight = 1; constraints.fill = GridBagConstraints.BOTH; constraints.weightx = 100; constraints.weighty = 100; layout.setConstraints(displayField, constraints); container.add(displayField); ActionListener insert = new InsertAction(); ActionListener
18、 command = new CommandAction(); addButton("7", 0, 2, 1, 1, insert); addButton("8", 1, 2, 1, 1, insert); addButton("9", 2, 2, 1, 1, insert); addButton("/", 3, 2, 1, 1, command); addButton("4", 0, 3, 1, 1, insert); addButton("5", 1, 3, 1, 1,
19、insert); addButton("6", 2, 3, 1, 1, insert); addButton("*", 3, 3, 1, 1, command); addButton("1", 0, 4, 1, 1, insert); addButton("2", 1, 4, 1, 1, insert); addButton("3", 2, 4, 1, 1, insert); addButton("-", 3, 4, 1, 1, command); addButton(&qu
20、ot;0", 0, 5, 1, 1, insert); addButton(".", 2, 5, 1, 1, insert); addButton("+", 3, 5, 1, 1, command); addButton("=", 0, 6, 4, 1, command); this.setResizable(false); setSize(180, 200); setVisible(true); private void addButton(String label, int row, int column, int wi
21、th, int height, ActionListener listener) JButton button = new JButton(label); constraints.gridx = row; constraints.gridy = column; constraints.gridwidth = with; constraints.gridheight = height; constraints.fill = GridBagConstraints.BOTH; button.addActionListener(listener); layout.setConstraints(butt
22、on, constraints); container.add(button); private class InsertAction implements ActionListener public void actionPerformed(ActionEvent event) String input = event.getActionCommand(); if (start) displayField.setText(""); start = false; if (input.equals("+/-") displayField.setText(d
23、isplayField.getText() + "-"); if (!input.equals("+/-") if (input.equals("Backspace") String str = displayField.getText(); if (str.length() > 0) displayField .setText(str.substring(0, str.length() - 1); else if (input.equals("CE") | input.equals("C"
24、;) displayField.setText("0"); start = true; else displayField.setText(displayField.getText() + input); private class CommandAction implements ActionListener public void actionPerformed(ActionEvent evt) String command = evt.getActionCommand(); if (start) lastCommand = command; else calculat
25、e(Double.parseDouble(displayField.getText(); lastCommand = command; start = true; public void calculate(double x) if (lastCommand.equals("+") result += x; else if (lastCommand.equals("-") result -= x; else if (lastCommand.equals("*") result *= x; else if (lastCommand.eq
26、uals("/") result /= x; else if (lastCommand.equals("=") result = x; displayField.setText("" + result); public static void main(String args) Calculator calculator = new Calculator(); calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 3、使用Java Swing中的各個組件,編寫一個自定義的軟件界面,例如:學(xué)生成績管理系統(tǒng)、圖書館管理系統(tǒng)、人事管理系統(tǒng)等等(請?jiān)谙路娇瞻滋幪顚懕境绦虻娜砍绦虼a及軟件界面截圖)import javax.swing.*;public class Program public static void main(String args) WindowBoxLayou
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 會議贊助協(xié)議合同范本
- 農(nóng)村魚塘轉(zhuǎn)讓合同范本
- 加盟合同范本烤鴨
- 勞務(wù)合同范本拼音寫
- 上海理財合同范本
- 包子店員工合同范本
- 勞務(wù)補(bǔ)助合同范本
- 修補(bǔ)圍網(wǎng)合同范本
- 公積金擔(dān)保合同范本
- 出租醫(yī)療服務(wù)合同范本
- 普通地質(zhì)學(xué)教材
- 多重耐藥菌相關(guān)知識
- 2021年云南省中考地理試卷(附答案詳解)
- 教師資格證幼兒教育真題及答案近五年合集
- 物業(yè)管理工作流程圖全套2
- 防蠅防鼠防蟲害情況記錄表
- 化學(xué)檢驗(yàn)工高級工理論知識試題題及答案
- 收養(yǎng)協(xié)議書真實(shí)范本模板
- 教程教科書i2analysts notebook8培訓(xùn)中文版
- 國家電網(wǎng)公司電力安全工作規(guī)程 配電部分 試行
- 杭州市住宅房屋裝修備案表
評論
0/150
提交評論