




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、鍵入文字實驗報告(2013 / 2014 學年 第 二 學期)課 程 名 稱Java語言程序設計實 驗 名 稱綜合圖形界面程序設計實 驗 時 間2014年5月5日指 導 單 位計算機學院 軟件教學中心指 導 教 師薛景學 生 姓 名 周媛班 級 學 號12001108學 院(系)計算機學院專 業(yè)計算機科學與技術(信息安全)3實驗名稱綜合圖形界面程序設計指導教師薛景實驗類型上機實驗學時2實驗時間2014-5-5一、實驗目的1.學習使用Java Swing設計GUI界面2.學習Java的事件監(jiān)聽機制的基本原理3.學習監(jiān)聽器處理Java中的事件二、實驗環(huán)境1.每位同學配備實驗計算機一臺2.安裝JDK
2、和Eclipse三、實驗內容1、編寫一個算數(shù)測試小軟件,用來訓練小學生的算數(shù)能力。程序有3個類組成,其中Teacher對象充當監(jiān)視器,負責給出算術題目,并判斷回答者的答案是否正確;ComputerFrame對象負責為算數(shù)題目提供視圖,比如用戶可以通過ComputerFrame對象提供的GUI界面看到題目,并通過該GUI界面給出題目的答案;MainClass是軟件的主類。(請在下方空白處填寫本程序的全部程序代碼及軟件界面截圖)public class Mainclass public static void main(String args) ComputerFrame frame; frame
3、=new ComputerFrame(); frame.setTitle("算術測試"); 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("確認答案"); 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、編寫一個簡單的計算器軟件,實現(xiàn)簡單的四則運算。(請在下方空白處填寫本程序的全部程序代碼及軟件界面截圖)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;/ 計算結果顯示區(qū) private String lastCommand; private double result;/ 保存計算結果 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中的各個組件,編寫一個自定義的軟件界面,例如:學生成績管理系統(tǒng)、圖書館管理系統(tǒng)、人事管理系統(tǒng)等等(請在下方空白處填寫本程序的全部程序代碼及軟件界面截圖)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)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 化學助劑項目投資管理方案
- 山東省泰安市肥城市2025屆高三下學期高考適應性測試(二)歷史試卷(含答案)
- 楚雄安全生產(chǎn)活動方案
- 民生十周年活動方案
- 汽水活動策劃方案
- 汽水創(chuàng)意活動方案
- 夢東方實踐活動方案
- 民間借款活動策劃方案
- 晚安配對活動策劃方案
- 春節(jié)綜合宣傳活動方案
- 中藥學學習要點
- 手機媒體概論(自考14237)復習題庫(含真題、典型題)
- 行車特種設備試題及答案
- 合同到期不續(xù)簽領失業(yè)金(2025年版)
- 智能包裝設計知到課后答案智慧樹章節(jié)測試答案2025年春湖南工業(yè)大學
- TAOPA 0067-2024 手持式無人機偵測反制設備技術要求
- 學校校長聘任合同
- 酒店安全生產(chǎn)責任制清單
- 電商平臺的供應鏈管理運營分析
- 廣東省茂名市2023-2024學年高一下學期7月期末考試 語文 含解析
- 2025年貴州貴陽市城市發(fā)展投資集團股份有限公司招聘筆試參考題庫附帶答案詳解
評論
0/150
提交評論