




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、JAVA中用GUI編程實(shí)現(xiàn)計(jì)算器模擬摘 要:本文論述了用java對(duì)計(jì)算器程序進(jìn)行需求分析、概要設(shè)計(jì)、詳細(xì)設(shè)計(jì),最后使用Java編程實(shí)現(xiàn)的全過程。設(shè)計(jì)GUI界面的計(jì)算器,用戶可以通過鼠標(biāo)依次輸入?yún)⒓佑?jì)算的數(shù)值,進(jìn)行加、減、乘、除及求負(fù)、取余等,主要用到的組件有框架、面板、文本框、按鈕和菜單欄等。關(guān)鍵詞:GUI 計(jì)算器 一.編程思想定義計(jì)算器類calculator,該類繼承JFrame類并實(shí)現(xiàn)ActionListener接口。整體窗口采用邊界布局,通過另外建立若干面板組件。North位置的面板上放置一個(gè)文本框,用于顯示運(yùn)算中的數(shù)據(jù)及結(jié)果;center位置放置兩個(gè)按鈕,分別用來返回原界面和清零。再用
2、GridLayout(網(wǎng)格布局)對(duì)數(shù)字控件和操作按鈕進(jìn)行布局。此外,單擊任何一個(gè)按鈕都會(huì)觸發(fā)ActionEvent事件,要處理這些事件就必須實(shí)現(xiàn)ActionListener接口的actionPerformed方法。二程序代碼如下import javax.swing.*;import javax.swing.event.*;import java.awt.*;.*;/變量設(shè)置如下/繼承JFrame類并實(shí)現(xiàn)ActionListenter接口public class calculator extends JFrame implements ActionListenerJFrame frame;/數(shù)字
3、及操作按鈕JButton num0,num1,num2,num3,num4,num5,num6,num7,num8,num9,Back,c;/運(yùn)算按鈕JButton plusButton,minusButton,multiplyButton,divideButton,residueButton,equalButton,changeButton,dotButton,sqrtButton,reciprocalButton;JMenu fileM;JMenuItem exitM,helpM;JTextField text;/狀態(tài)變量boolean clicked=true;boolean clear
4、=true;int all=0;double previous;String fuhao;int first=1;/界面設(shè)計(jì)public calculator()setTitle("計(jì)算器");setSize(300,250);setLocation(400,400);text=new JTextField(25);text.setHorizontalAlignment(JTextField.LEFT);JPanel cp1=new JPanel();JPanel cp2=new JPanel();JPanel cp3=new JPanel();/設(shè)置整體布局getCont
5、entPane().add(cp1,"North");getContentPane().add(cp2,"Center");getContentPane().add(cp3,"South");cp1.setLayout(new GridLayout(1,1);cp2.setLayout(new GridLayout(1,2);cp3.setLayout(new GridLayout(5,4);plusButton=new JButton("+");/加minusButton=new JButton("-&
6、quot;);/減multiplyButton=new JButton("*");/乘divideButton=new JButton("/");/除residueButton=new JButton("%");/余數(shù)equalButton=new JButton("=");/等號(hào)changeButton=new JButton("+/-");/正負(fù)號(hào)切換dotButton=new JButton(".");/小數(shù)點(diǎn)sqrtButton=new JButton("s
7、qrt");/開方reciprocalButton=new JButton("1/x");/倒數(shù)/數(shù)字按鈕num0=new JButton("0");num1=new JButton("1");num2=new JButton("2");num3=new JButton("3");num4=new JButton("4");num5=new JButton("5");num6=new JButton("6");num7=new
8、 JButton("7");num8=new JButton("8");num9=new JButton("9");cp1.add(text);text.setEditable(false);text.setBackground(Color.white);/監(jiān)聽設(shè)置Back=new JButton("Back");Back.addActionListener(this);c=new JButton("C");c.addActionListener(this); cp2.add(Back);cp2
9、.add(c);cp3.add(num7);num7.addActionListener(this);cp3.add(num8);num8.addActionListener(this);cp3.add(num9);num9.addActionListener(this);cp3.add(divideButton);divideButton.addActionListener(this);cp3.add(num4);num4.addActionListener(this);cp3.add(num5);num5.addActionListener(this);cp3.add(num6);num6
10、.addActionListener(this);cp3.add(multiplyButton);multiplyButton.addActionListener(this);cp3.add(num1);num1.addActionListener(this); cp3.add(num2);num2.addActionListener(this);cp3.add(num3);num3.addActionListener(this);cp3.add(minusButton);minusButton.addActionListener(this); cp3.add(num0);num0.addAc
11、tionListener(this);cp3.add(changeButton);changeButton.addActionListener(this);cp3.add(dotButton);dotButton.addActionListener(this);cp3.add(plusButton);plusButton.addActionListener(this);cp3.add(reciprocalButton);reciprocalButton.addActionListener(this);cp3.add(residueButton);residueButton.addActionL
12、istener(this);cp3.add(sqrtButton);sqrtButton.addActionListener(this);cp3.add(equalButton);equalButton.addActionListener(this);/添加菜單組件JMenuBar mainMenu= new JMenuBar();setJMenuBar(mainMenu);JMenu editMenu=new JMenu("文件");JMenu helpMenu=new JMenu("幫助");helpM= new JMenuItem("關(guān)于
13、");mainMenu.add(editMenu);mainMenu.add(helpMenu);helpMenu.add(helpM);helpM.addActionListener(this);fileM=new JMenu(" 文件");exitM=new JMenuItem(" 退出");fileM.addActionListener(this);exitM.addActionListener(this);editMenu.add(fileM); editMenu.add(exitM);setVisible(true);addWindo
14、wListener(new WindowDestroyer();/結(jié)束窗口 /響應(yīng)動(dòng)作代碼 public void actionPerformed(ActionEvent e)if (first =1)text.setText("");first=0;Object temp=e.getSource();if (temp=exitM)System.exit(0);if (temp=helpM)JOptionPane.showMessageDialog(null,"請(qǐng)查看幫助文檔","幫助",JOptionPane.INFORMATION
15、_MESSAGE);/退格if (temp= Back)String s=text.getText();text.setText("");for (int i=0;i<s.length()-1;i+)char a=s.charAt(i);text.setText(text.getText()+a);/清零if (temp=c)text.setText("0.");clear=true;first=1;if (temp=num0)/判斷是否單擊了符號(hào)位if (clear=false)text.setText("");text.se
16、tText(text.getText()+"0"); if (temp=num1)if (clear=false)text.setText("");text.setText(text.getText()+"1");clear=true;if (temp=num2)if (clear=false)text.setText("");text.setText(text.getText()+"2"); clear=true; if (temp=num3)if (clear=false)text.setT
17、ext("");text.setText(text.getText()+"3"); clear=true; if (temp=num4)if (clear=false)text.setText("");text.setText(text.getText()+"4");clear=true; if (temp=num5)if (clear=false)text.setText("");text.setText(text.getText()+"5"); clear=true; i
18、f (temp=num6)if (clear=false)text.setText("");text.setText(text.getText()+"6");clear=true; if (temp=num7)if (clear=false)text.setText("");text.setText(text.getText()+"7");clear=true; if (temp=num8)if (clear=false)text.setText("");text.setText(text.ge
19、tText()+"8"); clear=true; if (temp=num9)if (clear=false)text.setText("");text.setText(text.getText()+"9"); clear=true;/判斷是否含有小數(shù)點(diǎn) if (temp = dotButton) clicked=true; for (int i=0;i<text.getText().length();i+) if ('.'=text.getText().charAt(i) clicked = false; b
20、reak; if (clicked = true) text.setText(text.getText()+".");try if (temp=plusButton) previous=Double.parseDouble(text.getText(); fuhao="+" clear=false; if (temp=minusButton) previous=Double.parseDouble(text.getText(); fuhao="-" clear=false; if (temp=multiplyButton) previ
21、ous=Double.parseDouble(text.getText(); fuhao="*" clear=false; if (temp=divideButton) previous=Double.parseDouble(text.getText(); fuhao="/" clear=false; if (temp=equalButton) double next = Double.parseDouble(text.getText(); text.setText(""); if (fuhao ="+") tex
22、t.setText(previous + next +""); if (fuhao ="-") text.setText(previous - next +""); if (fuhao ="*") text.setText(previous * next +""); if (fuhao ="/") if (next=0) text.setText("除數(shù)不能為零"); else text.setText(previous + next +"&qu
23、ot;); if (fuhao ="%") text.setText(previous % next +""); clear=false; if (temp=sqrtButton) String s=text.getText();if (s.charAt(0)= '-')text.setText("負(fù)數(shù)不能開根號(hào)");elsetext.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText();clear = false;
24、if (temp=reciprocalButton)if (text.getText().charAt(0)='0'&& text.getText().length()=1)text.setText("除數(shù)不能為零");elseboolean isDec = true;int i,j,k;String s=Double.toString(1/Double.parseDouble(text.getText();for (i=0;i<s.length();i+)if (s.charAt(i)='.')break;for (j
25、=i+1;j<s.length();j+)if (s.charAt(j)='0')isDec=false;break;if (isDec = true) String stemp = ""for (k=0;k<i;k+)stemp += s.charAt(k);text.setText(stemp); elsetext.setText(s); clear = false; if ( temp = residueButton) previous=Double.parseDouble(text.getText(); fuhao="%" clear=false; if ( temp = changeButton) boolean isNumber = true; String s = text.getText(); for (int i=0;i<s.length();i+) if (!(s.charAt(i)>= '0'&& s.charAt(i
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度跨境電商貨物保險(xiǎn)運(yùn)輸協(xié)議
- 提升學(xué)生整體素質(zhì)營(yíng)養(yǎng)與智慧教育的融合之路
- 骨科常用護(hù)理診斷
- 護(hù)理容量控制課件
- 油膜渦動(dòng)故障診斷
- 隴南康縣城鎮(zhèn)招聘公益性崗位人員筆試真題2024
- 骨質(zhì)疏松疼痛病例討論
- 園林制圖投影技術(shù)基礎(chǔ)
- 教育機(jī)構(gòu)市場(chǎng)主管職責(zé)
- 中班健康落葉飄飄
- 2025年初中勞動(dòng)教師招聘考試試卷(附答案) 三套
- 銀行電信詐騙培訓(xùn)課件
- 燒結(jié)工藝培訓(xùn)課件
- 2025年4月自考00841第二外語(法語)試題
- 水表安裝培訓(xùn)課件下載
- 國(guó)有企業(yè)招標(biāo)培訓(xùn)課件
- 2025年甘肅省高考物理試卷(含答案解析)
- GB/T 45309-2025企業(yè)采購(gòu)物資分類編碼指南
- 23G409先張法預(yù)應(yīng)力混凝土管樁
- 三年級(jí)下冊(cè)口算天天100題(A4打印版)
- 犟龜 完整版課件
評(píng)論
0/150
提交評(píng)論