![java中用gui編程實(shí)現(xiàn)計(jì)算器模擬_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-8/26/4b278b5d-2a71-44b8-aaac-7a0b34c23ae5/4b278b5d-2a71-44b8-aaac-7a0b34c23ae51.gif)
![java中用gui編程實(shí)現(xiàn)計(jì)算器模擬_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-8/26/4b278b5d-2a71-44b8-aaac-7a0b34c23ae5/4b278b5d-2a71-44b8-aaac-7a0b34c23ae52.gif)
![java中用gui編程實(shí)現(xiàn)計(jì)算器模擬_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-8/26/4b278b5d-2a71-44b8-aaac-7a0b34c23ae5/4b278b5d-2a71-44b8-aaac-7a0b34c23ae53.gif)
![java中用gui編程實(shí)現(xiàn)計(jì)算器模擬_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-8/26/4b278b5d-2a71-44b8-aaac-7a0b34c23ae5/4b278b5d-2a71-44b8-aaac-7a0b34c23ae54.gif)
![java中用gui編程實(shí)現(xiàn)計(jì)算器模擬_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-8/26/4b278b5d-2a71-44b8-aaac-7a0b34c23ae5/4b278b5d-2a71-44b8-aaac-7a0b34c23ae55.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、JAVA中用GUI編程實(shí)現(xiàn)計(jì)算器模擬摘 要:本文論述了用java對計(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)格布局)對數(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è)置整體布局getContentPane().ad
5、d(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(-);/減multiplyButton=new JButton(*);/乘divideButton=new JButton(/);/除r
6、esidueButton=new JButton(%);/余數(shù)equalButton=new JButton(=);/等號(hào)changeButton=new JButton(+/-);/正負(fù)號(hào)切換dotButton=new JButton(.);/小數(shù)點(diǎn)sqrtButton=new JButton(sqrt);/開方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);
7、num5=new JButton(5);num6=new JButton(6);num7=new 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.add(c);cp3.
8、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.addActionLi
9、stener(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.addActionListener
10、(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.addActionListener(this
11、);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)于);mainMenu.add(editMenu);mainMenu.add(help
12、Menu);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);addWindowListener(new WindowDestroyer();/結(jié)束窗口 /響應(yīng)動(dòng)作代碼 public void actionPerforme
13、d(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,請查看幫助文檔,幫助,JOptionPane.INFORMATION_MESSAGE);/退格if (temp= Back)String s=text.getText();text.setText();for (int i=0;is.length()-1;i+)char a=s.ch
14、arAt(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.setText(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.s
15、etText();text.setText(text.getText()+2); clear=true; if (temp=num3)if (clear=false)text.setText();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
16、()+5); clear=true; if (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.getText()+8); clear=true; if (temp=num9)if
17、 (clear=false)text.setText();text.setText(text.getText()+9); clear=true;/判斷是否含有小數(shù)點(diǎn) if (temp = dotButton) clicked=true; for (int i=0;itext.getText().length();i+) if (.=text.getText().charAt(i) clicked = false; break; if (clicked = true)t()+.);try if (temp=plusButton) previous=Double.parseDouble(text.
18、getText(); fuhao=+; clear=false; if (temp=minusButton) previous=Double.parseDouble(text.getText(); fuhao=-; clear=false; if (temp=multiplyButton) previous=Double.parseDouble(text.getText(); fuhao=*; clear=false; if (temp=divideButton) previous=Double.parseDouble(text.getText(); fuhao=/; clear=false;
19、 if (temp=equalButton) double next = Double.parseDouble(text.getText(); text.setText(); if (fuhao =+) text.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(previ
20、ous + next +); 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; if (temp=reciprocalButton)if (text.ge
21、tText().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;is.length();i+)if (s.charAt(i)=.)break;for (j=i+1;js.length();j+)if (s.charAt(j)=0)isDec=false;break;if (isDec = true) String stemp = ;for (k=0;ki;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= 0& s.char
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 聯(lián)創(chuàng)聯(lián)建協(xié)議書
- 供應(yīng)商保密協(xié)議承諾書
- 馬鈴薯種薯購銷合同書
- 2025年山東貨運(yùn)從業(yè)資格證答題技巧與方法
- 電力項(xiàng)目開發(fā)合同(2篇)
- 電力合同結(jié)束協(xié)議(2篇)
- 2024秋六年級語文上冊 第一單元 4 花之歌說課稿 新人教版
- 六年級上冊數(shù)學(xué)計(jì)算題200道(含答案)
- 川教版信息技術(shù)(2019)五年級上冊第三單元 圖形化編程之聰明的角色 3 克隆躲避隕石-說課稿
- 服務(wù)員月初工作計(jì)劃范本
- 《公益性公墓管理章程》-
- DB31-T 1440-2023 臨床研究中心建設(shè)與管理規(guī)范
- 小說標(biāo)題作用探究省名師優(yōu)質(zhì)課賽課獲獎(jiǎng)?wù)n件市賽課一等獎(jiǎng)?wù)n件
- 老客戶維護(hù)方案
- 高處作業(yè)安全教育培訓(xùn)講義課件
- dk膠原蛋白培訓(xùn)課件
- 短視頻拍攝時(shí)間計(jì)劃表
- 萬科物業(yè)管理公司全套制度(2016版)
- 動(dòng)物檢疫技術(shù)-動(dòng)物檢疫處理(動(dòng)物防疫與檢疫技術(shù))
- 英語經(jīng)典口語1000句
- 進(jìn)模模具設(shè)計(jì)
評論
0/150
提交評論