java實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器_第1頁(yè)
java實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器_第2頁(yè)
java實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器_第3頁(yè)
java實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器_第4頁(yè)
java實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器_第5頁(yè)
已閱讀5頁(yè),還剩15頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、目錄I1需求分析11.1 計(jì)算器的基本功能: 11.1.1加法運(yùn)算:用數(shù)字按鈕和“+ ”按鈕進(jìn)行運(yùn)算;1.1.2減法運(yùn)算:用數(shù)字按鈕和“-"按鈕進(jìn)行運(yùn)算;1.1.3乘法運(yùn)算:用數(shù)字按鈕和“*”按鈕進(jìn)行運(yùn)算;1.1.4除法運(yùn)算:用數(shù)字按鈕和“/”按鈕進(jìn)行運(yùn)算;11111.2 退格鍵和清零鍵:用" Backspace如CT' C酸鈕實(shí)現(xiàn);11.3 計(jì)算器的科學(xué)計(jì)算方法: 11.3.1 開(kāi)方:用數(shù)字按鈕和“ Sqrt”按鈕進(jìn)行運(yùn)算; 11.3.2 百分比:用數(shù)字按鈕和“ ”按鈕進(jìn)行運(yùn)算; 11.3.3 求倒數(shù):用數(shù)字按鈕和“ 1/x”按鈕進(jìn)行運(yùn)算; 11.3.4 求自然對(duì)

2、數(shù):用數(shù)字按鈕和 “1破鈕進(jìn)行運(yùn)算;11.3.5 三角函數(shù):用數(shù)字按鈕和 “tan” “ cos按鈕遂行運(yùn)算 11.3.6 角度換算成弧度:用數(shù)字按鈕和按鈕進(jìn)行運(yùn)算 11.3.7 求反:用數(shù)字按鈕和 -7+'按鈕進(jìn)彳:亍運(yùn)算 11.3.8 平方和三次方:用數(shù)字按鈕和5人2” “掇鈕進(jìn)行運(yùn)算 11.4 常數(shù):21.4.1 兀用 Math類中的PI來(lái)實(shí)現(xiàn) 21.4.2 自然對(duì)數(shù)e:用Math類中的E來(lái)實(shí)現(xiàn) 22 設(shè)計(jì)22.1 用戶界面設(shè)計(jì)22.1.1 該計(jì)算器程序的設(shè)計(jì):用戶界面包括Swing組件,不過(guò)程序中大都使用的是AWT組件22.1.2 在 AWT 組件,22.1.3 這個(gè)界面設(shè)計(jì)中

3、包含了五個(gè)接口,分別控制運(yùn)算符,數(shù)字,清除,存儲(chǔ)功能和小數(shù)點(diǎn)的輸入22.1.4 程序設(shè)計(jì)中,使用了布局管理:32.2 概要設(shè)計(jì)32.2.1 它的功能是使用圖形用戶來(lái)實(shí)現(xiàn)計(jì)算器的界面設(shè)計(jì)和運(yùn)算功能以及一些科學(xué)運(yùn)算方法33 實(shí)現(xiàn)34 測(cè)試144.1 實(shí)現(xiàn)加法運(yùn)算:4+12=16 144.2 實(shí)現(xiàn)乘法運(yùn)算:3*9=27 154.3 用” C實(shí)現(xiàn)清零功能: 164.4 用“BackspaceOE退格功能 :174.5 求倒數(shù):1/4=0.25 18I1需求分析該計(jì)算器程序除了具備加減乘除基本功能外,還有清零鍵 C、CE和退格鍵 Backspace和一些部分的科學(xué)計(jì)算方法,包括開(kāi)方、求倒、百分比,由于時(shí)

4、間 問(wèn)題,之后會(huì)完善鍵盤事件的監(jiān)聽(tīng)功能。1.1 計(jì)算器的基本功能:1.1.1 加法運(yùn)算:用數(shù)字按鈕和“ +”按鈕進(jìn)行運(yùn)算;1.1.2 減法運(yùn)算:用數(shù)字按鈕和“-”按鈕進(jìn)行運(yùn)算;1.1.3 乘法運(yùn)算:用數(shù)字按鈕和“ *”按鈕進(jìn)行運(yùn)算;1.1.4 除法運(yùn)算:用數(shù)字按鈕和“ /按鈕進(jìn)行運(yùn)算;1.2 退格鍵和清零鍵:用"Backspace "、“C"和“ C酷鈕實(shí)現(xiàn);1.3 計(jì)算器的科學(xué)計(jì)算方法:1.3.1 開(kāi)方:用數(shù)字按鈕和“ Sqrt”按鈕進(jìn)行運(yùn)算;1.3.2 百分比:用數(shù)字按鈕和“ ”按鈕進(jìn)行運(yùn)算;1.3.3 求倒數(shù):用數(shù)字按鈕和“ 1/x”按鈕進(jìn)行運(yùn)算;1.3.4

5、 求自然對(duì)數(shù):用數(shù)字按鈕和“ ln”按鈕進(jìn)行運(yùn)算;1.3.5 三角函數(shù):用數(shù)字按鈕和“ tan” “coS' "sin”按鈕進(jìn)行運(yùn)算1.3.6 角度換算成弧度:用數(shù)字按鈕和“'”按鈕進(jìn)行運(yùn)算1.3.7 求反:用數(shù)字按鈕和“ -/+”按鈕進(jìn)行運(yùn)算1.3.8 平方和三次方:用數(shù)字按鈕和“ x2” “乂八3”按鈕進(jìn)行運(yùn)算第14頁(yè) 共18頁(yè)1.4 常數(shù):1.4.1 兀:用Math類中的PI來(lái)實(shí)現(xiàn)1.4.2 自然對(duì)數(shù)e:用Math類中的E來(lái)實(shí)現(xiàn)2設(shè)計(jì)2.1 用戶界面設(shè)計(jì)2.1.1 該計(jì)算器程序的設(shè)計(jì):用戶界面包括Swing組件,不過(guò)程序中大都使用的是AWT組件.4 import

6、 java.awt.*;56 import java.awt.event.*j78 import javax.swing_*;2.1.2 在AWT組件,(1)使用了面板和按鈕組:JPanel panell, panel2, panel3, panel4;ButtonGroup bgb;(2)由于該組件按鈕較多,設(shè)計(jì)一個(gè)方法簡(jiǎn)化按鈕的設(shè)置void addButton(JPanel panel , String name, ActionListener action Color color )JButton bt = new JButton( name);panel .add( bt); /在面板

7、上增加按鈕 bt .setForeground( color ); / 設(shè)置字體顏色 bt .addActionListener( action ); / 增加監(jiān)聽(tīng)事件2.1.3 這個(gè)界面設(shè)計(jì)中包含了五個(gè)接口,分別控制運(yùn)算符,數(shù)字,清除,存儲(chǔ)功 能和小數(shù)點(diǎn)的輸入class Signsimplements ActionListenerclass Num implements ActionListenerclass Clearimplements ActionListenerclass Memory implements ActionListenerclass Dot implements Act

8、ionListener2.1.4 程序設(shè)計(jì)中,使用了布局管理:(1)用邊布局管理器(BorderLayout)設(shè)置計(jì)算器容器各方位組件:panel4 = new JPanel( new BorderLayout(5, 5)panel4 .add( panel1 , BorderLayout.NORTH;panel4 .add( panel2 , BorderLayout.CENTERthis .add( tf , BorderLayout. NORTH this .add( panel3 , BorderLayout. WEST(2)用網(wǎng)格布局管理器(GridLayout)設(shè)置面板panel1

9、 = new JPanel( new GridLayout(1,3, 10, 10);panel2 = new JPanel( new GridLayout(5, 6, 5, 5);panel3 = new JPanel( new GridLayout(5, 1,5, 5);2.2概要設(shè)計(jì)計(jì)算器的整個(gè)程序包括:一個(gè)Calculator類2.2.1 它的功能是使用圖形用戶來(lái)實(shí)現(xiàn)計(jì)算器的界面設(shè)計(jì)和運(yùn)算功能以及一些 科學(xué)運(yùn)算方法.類中包含了 Calculator。構(gòu)造方法,和其他重要的成員方法,最終在main方法中實(shí)例化一個(gè)ca對(duì)象實(shí)現(xiàn)計(jì)算器的功能。3實(shí)現(xiàn)package calculator;imp

10、ort java.awt.*;import java.awt.event.*;import javax.swing.*;import java.text.DecimalFormat;public class Calculator extends JFrame JTextField tf ;JPanel panel1 , panel2 , panel3 , panel4 ;JMenuBar myBar;JMenu menul, menu2, JMenuItem editlteml JRadioButtonMenuItemmenu3;editItem2 seeltemlJCheckBoxMenuI

11、tem seeItem3 ; / ButtonGroup bgb;boolean IfResult = true , flag String oper ="="double result = 0, memory = 0;,helpl , help2 , help3 ;,seeItem2 ; / 單選框復(fù)選框false ;DecimalFormat df;Calculator()super ("科學(xué)計(jì)算器");/設(shè)置標(biāo)題欄df = new DecimalFormat( "#.#" ); / 保留四位小數(shù)this .setLayout(

12、new BorderLayout(10, 5);setDefaultCloseOperation(JFrame. EXIT_ON_CLOSEsetVisible( true );/設(shè)置每個(gè)JPanel的參數(shù)panel1 =panel2 =panel3 =panel4 =new JPanel( new JPanel( new JPanel( new JPanel(new GridLayout(1,3, 10, 10);new GridLayout(5, 6, 5, 5);new GridLayout(5, 1,5, 5);new BorderLayout(5, 5);/*菜單欄*/myBar =

13、 new JMenuBar();menu1 = new JMenu("編輯(E)”)menu2 = new JMenu("查看(V)”)menu3 = new JMenu("幫助(H)” )menu1.setFont(menu2setFont(menu3setFont(new Font( " 宋體”,Font. new Font( " 宋體”,Font. new Font( " 宋體”,Font.PLAIN, 12);PLAIN, 12);PLAIN, 12);*編輯欄*/editItem1 =editItem2 =new JMenu

14、Item( " 復(fù)制(C) Ctrl+C");new JMenuItem( " 粘貼(P) Ctrl+V" );editlteml .setFont( new Font("宋體",Font. PLAIN,12);/*查看欄*/seeltemlseeItem2seeItem3editItem2 .setFont( new Font("宋體",Font. PLAIN,12);new JRadioButtonMenuItem("科學(xué)型(T)");new JRadioButtonMenuItem(&qu

15、ot;標(biāo)準(zhǔn)型(S)");new JCheckBoxMenuItem("數(shù)字分組(I)");seeItem1 .setFont(seeItem2 .setFont(seeItem3 .setFont(new Font("宋體",F(xiàn)ont. PLAIN,12);new Font("宋體",F(xiàn)ont. PLAIN,12);new Font("宋體",F(xiàn)ont. PLAIN,12);*幫助欄*/helpl = new JMenuItem("幫助主題(H)");help2 = new JMenu

16、Item("關(guān)于計(jì)算器(A)");helpl .setFont( new Font("宋體",F(xiàn)ont. PLAIN,12); help2 .setFont( new Font("宋體",F(xiàn)ont. PLAIN,12);bgb = new ButtonGroup(); / 選項(xiàng)組menu1.add( editlteml );menu1.add( editItem2 );menu2add( seelteml );menu2add( seeItem2 );menu2addSeparator();/ 添加一條分割線menu2add( see

17、Item3 );menu3add( helpl );menu3addSeparator();/ 添加一條分割線menu3add( help2 );myBar.add( menul);myBar.add( menu2);myBar.add( menu3);this .setJMenuBar( myBar);*文本域,即為計(jì)算器的屏幕顯示區(qū)域*/tf = new JTextField();tf .setEditable( false ); /文本區(qū)域不可編輯tf .setBackground(Color. white ); / 文本區(qū)域的背景色tf .setHorizontalAlignment(

18、JTextField.RIGHT); / 文字右對(duì)齊tf .setText( "0");init(); /對(duì)計(jì)算器進(jìn)行初始化/*初始化操作*添加按鈕*/void init()addButton( panel1 , "Backspace" , new Clear(), Color. red);addButton( panel1 , "CE", new Clear(), Color. red );addButton( panel1 , "C", new Clear(), Color. red );addButton(p

19、anel2"1/x",new Signs(), Color.magenta);addButton(panel2"ln",new Signs(), Color.magenta);addButton(panel2"7" ,new Num(), Color.blue )addButton(panel2"8",new Num(), Color.blue )addButton(panel2"9" D ,new Num(), Color.blue )addButton(panel2,new Signs(),

20、 Color.red );addButton(panel2"n!",new Signs(), Color.magenta);addButton(panel2"sqrt",new Signs(), Color.magenta)addButton(panel2"4"new Num(), Color.blue )addButton(panel2節(jié)"new Num(), Color.blue )addButton(panel2"6",new Num(), Color.blue )addButton(panel2&

21、quot;X,new Signs(), Color.red );addButton(panel2"sin",new Signs(), Color.magenta);addButton(panel2"xA2",new Signs(), Color.magenta);addButton(panel2"1" 1 ,new Num(), Color.blue )addButton(panel2"2"new Num(), Color.blue )addButton(panel2“a”3 ,new Num(), Color.b

22、lue )addButton(panel2,new Signs(), Color.red );addButton(panel2"cos",new Signs(), Color.magenta);addButton(panel2"xA3",new Signs(), Color.magenta);addButton(panel2"0",new Num(), Color.blue );addButton(panel2"-/+”,new Clear(), Color.blue );addButton(panel2. ,new Dot

23、(), Color.blue );addButton(panel2"+",new Signs(), Color.red );addButton(panel2"tan",new Signs(), Color.magenta);addButton(panel2"%",new Signs(), Color.magenta);addButton(panel2"冗心new Num(), Color.orange );addButton(panel2"e",new Num(), Color.orange );addB

24、utton(panel2"',''new Signs(), Color.orange );addButton(panel2=,new Signs(), Color.red );addButton(panel3 ,"MC",new Memory(), Color.red );addButton(panel3 ,"MS",new Memory(), Color.red );addButton(panel3 ,"MR",new Memory(), Color.red );addButton(panel3 ,

25、"M-",new Memory(), Color.red );addButton(panel3 ,"M+",new Memory(), Color.red );panel4 .add( panell , BorderLayout.NORTH;panel4 .add( panel2 , BorderLayout.CENTERthis .add( tf , BorderLayout. NORTHthis .add( panel3 , BorderLayout.WESTthis .add( panel4 );pack(); /調(diào)整此窗口的大小,以適合其子組件的

26、首選大小和布局 this .setResizable( false ); / 窗口不可改變大小this .setLocation(300, 200);/*統(tǒng)一設(shè)置按鈕的的使用方式*/void addButton(JPanel panel , String name, ActionListener action Color color )JButton bt = new JButton( name);panel .add( bt); /在面板上增加按鈕bt .setForeground( color ); / 設(shè)置字體顏色bt .addActionListener( action ); / 增加

27、監(jiān)聽(tīng)事件 /*計(jì)算器的基礎(chǔ)操作(+ - x片*/void getResult ( double x)if (oper .equals( "+") result += x;else elseif (oper .equals( if (oper .equals("-") result "x) result-=x*= xelseif (oper .equals("+) result/= xelse if (oper .equals( tf .setText( df .format("=") result result )

28、;=x;運(yùn)算符號(hào)的事件監(jiān)聽(tīng); ;/*/class Signs implements ActionListener public void actionPerformed(ActionEvente) /*用ActionEvent 對(duì)象的getActionCommand() 方法取得與引發(fā)事件對(duì)象的字符串*/String str = e.getActionCommand();/* sqrt求平方根*/if (str .equals( "sqrt" ) double i = Double. parseDouble (tf .getText();/將tf_文本框中已經(jīng)輸入的數(shù)字字符

29、串轉(zhuǎn)化為double類型的變 量if (i >=0) /* * String.valueOf()轉(zhuǎn)換為字符串* df.format()按要求保留四位小數(shù)* Math.sqrt()求算數(shù)平方根* /tf .setText(String. valueOf (df .format(Math. sqrt (i);else tf .setText("負(fù)數(shù)不能開(kāi)平方根"); /*用10g函數(shù)求自然對(duì)數(shù)*/else if (str .equals( "ln" )double i if (i >0)tf .setText(String.valueOf (df

30、 .format(Math.log (i );=Double. parseDouble (tf .getText();else tf .setText("負(fù)數(shù)不能求對(duì)數(shù)");/* %求百分比*/ else if (str .equals( "%")tf .setText(String. valueOf (df .format(Double. parseDouble (tf .g etText() /100);/* 1/x 求倒數(shù)*/else if (str .equals( "1/x" )if (Double. parseDouble

31、 (tf .getText() = 0) tf .setText("除數(shù)不能為零");else tf .setText(String. valueOf ( df .format(1 / Double. parseDouble (tf .getText();/* sin求正弦函數(shù)*/else if (str .equals( "sin" ) double i = Double. parseDouble (tf .getText();tf .setText(String. valueOf (df .format(Math. sin (i );/* cos一求

32、余弦函數(shù)*/ else if (str .equals( "cos" ) double i = Double. parseDouble (tf .getText();tf .setText(String. valueOf (df .format(Math. cos (i );/* tan求正切函數(shù)*/else if (str .equals( "tan" ) double i = Double. parseDouble (tf .getText();tf .setText(String. valueOf (df .format(Math. tan (i

33、); /* n! 求階乘*/else if (str .equals( "n!" )double i = Double. parseDouble (tf .getText();if ( i %2=0)|( i %2=1)判斷為整數(shù)放進(jìn)行階乘操作 int j = ( int ) i ; /強(qiáng)制類型轉(zhuǎn)換 int result =1;for (int k=1; k<=j; k+) result *= k;tf .setText(String. valueOf (result ); else tf .setText("無(wú)法進(jìn)行階乘"); /* xA2 求平

34、方*/else if (str .equals( "xA2" )double i = Double. parseDouble (tf .getText();tf .setText(String. valueOf (df.format( i * i ); /* xA3 求立方*/else if (str .equals( "xA3" )double i = Double. parseDouble (tf .getText();tf .setText(String. valueOf (df .format( i *i *i ); /* ''角

35、度轉(zhuǎn)換*/*將角度值轉(zhuǎn)換成弧度值,方便三角函數(shù)的計(jì)算*/else if (str .equals(" double i = Double. tf .setText(String. )parseDouble (tf .getText();valueOf (i/180*Math. PI);else if (flag )IfResult = false ;if (IfResult ) oper = str ;parseDouble (tf .getText();else getResult(Double.oper = str ; IfResult = true ; /*清除按鈕的事件監(jiān)聽(tīng)*

36、/class Clear implements ActionListener public void actionPerformed(ActionEvent e) /*用ActionEvent 對(duì)象的getActionCommand() 方法取得與引發(fā) 事件對(duì)象的字符串*/String str = e.getActionCommand(); if (str = "C" )tf .setText( "0");IfResult = true ;result = 0; else if (str = "-/+" )double i = 0 -

37、 Double. parseDouble (tf .getText();tf .setText(String. valueOf (df.format( i ); else if (str = "Backspace" )if (Double. parseDouble (tf .getText() > 0)if (tf .getText().length() > 1)tf .setText( tf .getText().substring(0, tf .getText().length() - 1);/使用退格刪除最后一位字符 /* 函數(shù):substring(int

38、 begjnlndex, int endindex)*它的返回值為一個(gè)新的字符串,為原字符串的子用* beginindex為子字符串開(kāi)始的地方,endindex為子 字符串結(jié)束的地方*/ else tf .setText( "0"); IfResult = true ;else if (tf .getText().length() > 1)tf .setText( tf .getText().substring(0,tf .getText().length() - 1); else tf .setText( "0"); IfResult = tru

39、e ; else if (str = "CE") tf .setText( "0"); IfResult = true ; /* *數(shù)字輸入的事件監(jiān)聽(tīng) */class Num implements ActionListenerpublic void actionPerformed(ActionEvent e) String str = e.getActionCommand();if (IfResult )tf .setText(""); IfResult = false ;if (str ="兀 i) tf .setText

40、(String. valueOf (Math. PI);else if ( str ="e") tf .setText(String. valueOf (Math. E); else tf .setText( tf .getText()+ str ); if (tf .getText().equals( "0" )/為了能夠輸入絕對(duì)值小于1的小數(shù)并不會(huì)重復(fù)顯示多個(gè)0 tf .setText( "0");IfResult = true ; flag = true ; /* 小數(shù)點(diǎn)的事件監(jiān)聽(tīng)*/class Dot implements ActionListener public void

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論