Java程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告-計(jì)算器.doc
Java程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告題 目 計(jì)算器 姓 名 學(xué) 號 學(xué) 院 軟件學(xué)院 專 業(yè) 軟件工程 年 級 2009 級 指導(dǎo)教師 2010年 4 月 9 日一、實(shí)驗(yàn)?zāi)康?、 熟練掌握java面向?qū)ο缶幊獭?、 選擇合適的數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)需求。3、 熟練使用各種控制結(jié)構(gòu)。4、 掌握GUI組件、事件處理技術(shù)。二、實(shí)驗(yàn)任務(wù)1、 設(shè)計(jì)一個(gè)簡易的計(jì)算器,可以進(jìn)行四則運(yùn)算:加、減、乘、除等(限于十進(jìn)制下)程序要求:(1)應(yīng)具有相應(yīng)的界面,可參考下圖進(jìn)行設(shè)計(jì): 圖示1 圖示2(2)操作符號定為:“+”,“-”,“*”,“/”,“(”,“)”等。(按國際慣例設(shè)計(jì))(3)用戶通過點(diǎn)擊面板中的各種組件輸入的表達(dá)式,應(yīng)在上方文本框中顯示。(4)當(dāng)用戶點(diǎn)擊“=”時(shí),文本框中之前的內(nèi)容應(yīng)清空,直接顯示表達(dá)式運(yùn)算結(jié)果。例如:輸入表達(dá)式“3+2*(8-5)”之后按“=”,顯示結(jié)果“9”。(5)具有菜單欄,內(nèi)有相應(yīng)的編輯“復(fù)制”、“粘貼”等菜單,提供相應(yīng)的功能。(6)以上部分為必須完成的內(nèi)容。2、選作功能,可參考windows系統(tǒng)附帶計(jì)算器: (1)具有相應(yīng)的科學(xué)計(jì)算功能。例如“圖示2”中的各種科學(xué)計(jì)算功能。三、開發(fā)工具與平臺(tái)1.開發(fā)工具:Eclipse默認(rèn)是一個(gè)和Jbuilder類似的Java開發(fā)工具,但它不僅僅只 是Java開發(fā)工具,只要裝上相應(yīng)的插件,eclipse也可作為其它語言的開發(fā)工具。如C/C+插件(CDT)。2.開發(fā)平臺(tái):JDK1.5四、設(shè)計(jì)思路1.界面設(shè)計(jì) (1)制作一個(gè)簡單的面板JFrame,在其上面添加構(gòu)造界面所需的各種組件其構(gòu)造方法為:calculate()super();this.setBounds(10, 10, 300, 250);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setTitle(計(jì)算器);init();/創(chuàng)建菜單及各種組件(如按鈕,文本框等)getContentPane().setLayout(new BorderLayout(3,3);getContentPane().add(panelCenter,BorderLayout.CENTER);getContentPane().add(panelNorth,BorderLayout.NORTH);(2)實(shí)現(xiàn)構(gòu)造菜單及文本框等組件的函數(shù)init(),如下實(shí)現(xiàn):Public void Init()/菜單項(xiàng)的構(gòu)造形成; editor.add(copy);editor.addSeparator();editor.add(paste); editor.setMnemonic(E);look.setMnemonic(V);help.setMnemonic(H); copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK); paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK); help.add(about); bar.add(editor);bar.add(look);bar.add(help); setJMenuBar(bar);/將菜單添加到JFrame; /* 接下來是文本或結(jié)果顯示區(qū)及Backspace、功能鍵(CE、C)和的添加,采用 GridBagLayout布局方式,將它們放在北部panelNorth面板。 其次,是數(shù)字鍵及其他功能鍵的的使用,我這里采用的是網(wǎng)格布局,setLayout(new GridLayout(4,6,5,5);將它們放在主面板中心區(qū)域。 這里給出網(wǎng)格包布布局的兩個(gè)重要方法,由于代碼段較長,故將其他的代碼放在后面附錄;private void makeShow(GridBagLayout gridBag,GridBagConstraints constraints, Component component) gridBag.setConstraints(component, constraints);/ 網(wǎng)格包布布局; panelNorth2.add(component);private void addButton(GridBagLayout gridBag,/網(wǎng)格包布布局重要方法;GridBagConstraints constraints, Component component) gridBag.setConstraints(component, constraints); panelNorth1.add(component);總體界面如下:2.邏輯設(shè)計(jì) 為了能夠得到我們想要的操作,比如讓那些數(shù)字參與運(yùn)算,做什么運(yùn)算等等,我們就需要為每個(gè)按鈕添加響應(yīng)事件,通過響應(yīng)事件來完成我們預(yù)期的結(jié)果。以下我們來分析下重要的事件是如何響應(yīng)的:.String str = e.getActionCommand();/ TODO Auto-generated method stubif(str.equals(Backspace)/點(diǎn)擊 BackspacehanderBackspace();else if(str.equals(CE)/點(diǎn)擊了CE的功能是清除當(dāng)前顯示的數(shù)值bool = false;show.setText(0);else if(str.equals(C)/點(diǎn)擊了C的功能是清零copyText=;Div = 0.0;CalculateResult =;bool = false;show.setText(0);else if(str.equals(MC)/MC清楚存儲(chǔ)器內(nèi)容handerMC();else if(str.equals(MR)/MR將存于存儲(chǔ)器的值顯示在文本框中handerMR();else if(str.equals(MS)/MS將顯示框得文字存于存儲(chǔ)器handerMS();else if(str.equals(M+)/M+將顯示框的數(shù)與存儲(chǔ)器中的數(shù)相加并進(jìn)行存儲(chǔ)handerM();else if(0123456789.indexOf(str)=0)/按下的是數(shù)字鍵或者小數(shù)點(diǎn)正負(fù)號handerCenter(str);else if(str.equals(%)/將當(dāng)前顯示框里的數(shù)字用百分比表示( x/100)String current = show.getText();double a = 0;if(!current.isEmpty() a = Double.parseDouble(current); Double result =(Double) a/100; String s4 = result.toString(); if(!isInt(s4) show.setText(s4);else show.setText(s4.substring(0,s4.indexOf(.); bool = true; else handerOperation(str);/響應(yīng)的是運(yùn)算符;處理運(yùn)算符操作3. 程序測試 程序測試過程中無較大BUG,基本上輕松搞定。五、實(shí)驗(yàn)總結(jié) 這是我第一次正式的java程序設(shè)計(jì)實(shí)驗(yàn),感覺能夠把學(xué)到的東西用到實(shí)際中是件非常有趣的事,更重要的是讓我們真正明白了學(xué)貴致用的道理。 由于時(shí)間有限,本程序未能做到科學(xué)計(jì)算器,同時(shí)在這里也感謝老師的指導(dǎo)和同學(xué)的幫助。6、 附錄public class calculate extends JFrame implements ActionListener /* * param args */private JMenuBar bar = new JMenuBar();private JMenu editor =new JMenu(編輯(E);private JMenuItem copy =new JMenuItem(復(fù)制);private JMenuItem paste =new JMenuItem(粘貼);private JMenu look = new JMenu(查看(V);private JMenu help = new JMenu(幫助(H);private JMenuItem about = new JMenuItem(關(guān)于);private String copyText ;private JPanel panelWest= new JPanel();private JPanel panelCenter = new JPanel();/放Number鍵private JPanel panelNorth = new JPanel();/放功能鍵1與文本框private JPanel panelNorth1 = new JPanel();private JPanel panelNorth2 = new JPanel();/放顯示框private JPanel panelEast = new JPanel();/放運(yùn)算鍵;private String strNorth = new StringBackspace,CE,C;/功能鍵1private String strCenter = new String7,8,9,4,5,6,1,2,3,0,+/-,.;private String strEast = new String/,sqrt,*,%,-,1/x,+,=;private StringstrWest = new StringMC,MR,MS,M+;private JButton buttonCenter = new JButtonstrCenter.length;private JButton buttonEast= new JButtonstrEast.length;private JButton buttonNorth = new JButtonstrNorth.length;private JButton buttonWest = new JButtonstrWest.length;private String ch=new String();/保存當(dāng)前運(yùn)算符private Double Div;/保存中間運(yùn)算數(shù)private String save = new String();/保存存儲(chǔ)器中的數(shù)值private String CalculateResult= new String() ;/保存運(yùn)算結(jié)果private boolean bool = false;/當(dāng)前結(jié)果是否為運(yùn)算結(jié)果private JTextField show = new JTextField(0,24);/輸出顯示private JTextField label;private GridBagLayout gridBag = new GridBagLayout();/網(wǎng)格包布布局;private GridBagConstraints constraints = new GridBagConstraints();calculate()super();this.setBounds(10, 10, 300, 250);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setTitle(計(jì)算器);init();getContentPane().setLayout(new BorderLayout(3,3);getContentPane().add(panelCenter,BorderLayout.CENTER);getContentPane().add(panelNorth,BorderLayout.NORTH);public void init() editor.add(copy);editor.addSeparator();editor.add(paste); editor.setMnemonic(E);look.setMnemonic(V);help.setMnemonic(H); copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK); paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK); help.add(about); bar.add(editor);bar.add(look);bar.add(help); setJMenuBar(bar); copy.addActionListener(this); paste.addActionListener(this); about.addActionListener(this); panelNorth1.setLayout(gridBag);/ panelNorth2.setLayout(gridBag); show.setHorizontalAlignment(JTextField.RIGHT);/右對齊; show.setEditable(false); panelNorth.setLayout(new BorderLayout(); panelNorth.add(show,BorderLayout.NORTH); show.setBorder(new BevelBorder(BevelBorder.LOWERED);/讓顯示框凹陷下去 show.setBackground(Color.WHITE); /網(wǎng)格包布布局; constraints.gridx = 0; constraints.gridy = 0; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; makeShow(gridBag,constraints,show); /* /設(shè)置箱式布局間隔 (另一種布局方式) * / box.add(label); for(int i = 0;istrNorth.length;+i) buttonNorthi = new JButton(strNorthi); box.add(Box.createHorizontalStrut(10); box.add(buttonNorthi); buttonNorthi.addActionListener(this); buttonNorthi.setForeground(Color.RED); */ label = new JTextField(2); label.setEditable(false); label.setBorder(new BevelBorder(BevelBorder.LOWERED);/讓文本框凹陷下去; constraints.gridx = 0; constraints.gridy = 0; constraints.fill = GridBagConstraints.NONE; constraints.insets = new Insets(10,5,10,10); addButton(gridBag,constraints,label); for(int j = 0;jstrNorth.length;+j) buttonNorthj = new JButton(strNorthj); buttonNorthj.addActionListener(this); buttonNorthj.setForeground(Color.RED); constraints.gridx =j+1 ; constraints.gridy = 0; addButton(gridBag,constraints,buttonNorthj); panelNorth.add(panelNorth1,BorderLayout.CENTER); panelNorth.add(panelNorth2,BorderLayout.NORTH); panelCenter.setLayout(new GridLayout(4,6,5,5); for(int i =0;istrWest.length;+i) buttonWesti = new JButton(strWesti); buttonWesti.setBorder(new BevelBorder(BevelBorder.RAISED); buttonWesti.setForeground(Color.RED); buttonWesti.addActionListener(this); for(int i =0;istrCenter.length;+i) buttonCenteri = new JButton(strCenteri); buttonCenteri.setBorder(new BevelBorder(BevelBorder.RAISED); buttonCenteri.setForeground(Color.BLUE); buttonCenteri.addActionListener(this); for(int i =0;istrEast.length;+i) buttonEasti = new JButton(strEasti); buttonEasti.setBorder(new BevelBorder(BevelBorder.RAISED); buttonEasti.addActionListener(this); if(i%2=0|(i=7) buttonEasti.setForeground(Color.RED); else buttonEasti.setForeground(Color.BLUE); int w =0,e = 0, c = 0; for(int i = 0;i24;) if(i%6=0) buttonWestw.setBorder(new LineBorder(Color.GRAY); panelCenter.add(buttonWestw); +i; +w; else for(int j = 0;j3;+j) buttonCenterc.setBorder(new LineBorder(Color.GRAY); panelCenter.add(buttonCenterc); +i; +c; for(int j = 0;j=0)/按下的是數(shù)字鍵或者小數(shù)點(diǎn)正負(fù)號handerCenter(str);else if(str.equals(%)/將當(dāng)前顯示框里的數(shù)字用百分比表示( x/100)String current = show.getText();double a = 0;if(!current.isEmpty() a = Double.parseDouble(current); Double result =(Double) a/100; String s4 = result.toString(); if(!isInt(s4) show.setText(s4);else show.setText(s4.substring(0,s4.indexOf(.); bool = true; else handerOperation(str);private void handerMC() save = ;/ TODO Auto-generated method stublabel.setText();private void handerMR() if(!save.isEmpty() if(!isInt(save) show.setText(save); else show.setText(save.substring(0,save.indexOf(.); private void handerMS() save = show.getText();/ TODO Auto-generated method stubprivate void handerM() String str = show.getText();/ TODO Auto-generated method stubdouble s=0,v=0;if(!str.isEmpty()&!save.isEmpty() s= Double.parseDouble(str); v = Double.parseDouble(save);save = String.valueOf(s+v);label.setText(M);private void handerCenter(String str) String result = show.getText();if(str.equals(.)if(!(result.indexOf(.)=0)/判斷是否已存在小數(shù)點(diǎn)if(bool)show.setText();result=;result+=str;show.setText(result);bool = false;String num = result;if(*-+/.indexOf(ch)=0) handerResult(ch,num); else if(result.equals(0)result=str;show.setText(result);bool = false;String num = result;if(*-+/.indexOf(ch)=0)handerResult(ch,num); else if(bool)show.setText();/文本框不為0.既有數(shù)字或者0和小數(shù)點(diǎn)result = ;result+=str;show.setText(result);bool = false;String num = result;/保存if(*-+/.indexOf(ch)=0) handerResult(ch,num); private void handerOperation(String str) String result = show.getText();if(str.equals(+/-)/處理正負(fù)號 if(result.indexOf(-)=0) StringBuffer e = new StringBuffer(result); e.deleteCharAt(0); show.setText(e.toString(); String num = e.toString(); if(*-+/.indexOf(ch)=0) handerResult(ch,num); else StringBuffer e = new StringBuffer(result); e.insert(0, -); show.setText(e.toString(); String num = e.toString(); if(*-+/.indexOf(ch)=0) handerResult(ch,num); else if(str.equals(sqrt)/平方根功能if(!(result.indexOf(-)=0)double operate=0;if(!result.isEmpty() operate =Double.parseDouble(result); double reult = Math.sqrt(operate); Double a = reult; String s =a.toString(); if(!isInt(s) show.setText(s); else show.setText(s.substring(0,s.indexOf(.); bool = true; if(*-+/.indexOf(ch)=0) handerResult(ch,s); else show.setText(函數(shù)輸入無效);bool= true;else if(str.equals(/)/除法運(yùn)算;String show2 = show.getText();show.setText();ch=/;double operate = 0;if(!show2.isEmpty()operate = Double.parseDouble(show2); Div = (Double)operate;/保存被除數(shù);else if(str.equals(*)String show2 = show.getText();show.setText();ch=*;/保存當(dāng)前運(yùn)算符double operate = 0;if(!show2.isEmpty() operate = Double.parseDouble(show2);Div = (Double)operate;else if(str.equals(-)String show2 = show.getText();show.setText();ch=-;double operate =0;if(!show2.isEmpty() operate= Double.parseDouble(show2);Div = (Double)operate;else if(str.equals(+)String show2 = show.getText();show.setText();ch=+;double operate = 0;if(!show2.isEmpty() operate = Double.parseDouble(show2); Div = (Double)operate;/保存加數(shù)else if(str.equals(1/x)/求倒String show2 = show.getText();if(!show2.equals(0)double operate = 0;if(!show2.isEmpty() operate = Double.parseDouble(show2);double resultReverse = (1/operate);Double resultReverseDouble = resultReverse;show.setText(resultReverseDouble.toString();bool = true;else show.setText(除數(shù)不能為零。);bool = true;else if(str.equals(=)show.setText(CalculateResult);bool = true;private boolean isInt(String s) if(s.indexOf(.)0)for(int i =(s.length()-1);is.indexOf(.);-i) if(!(s.charAt(i)=0) return false;return true;private void handerResult(String ch,String num) /處理運(yùn)算結(jié)果并保存; char swt = 0 ;if(!ch.equals() swt = ch.charAt(0);/將String轉(zhuǎn)換成charswitch(swt)case -: double resultSub = (Div-Double.parseDouble(num);Double resultSubDouble = resultSub;String s1 = resultSubDouble.toString(); if(!isInt(s1) CalculateResult=s1; else CalculateResult = s1.substring(0,s1.indexOf(.); /保存運(yùn)算結(jié)果;break;case +:double resultAdd = (Div+Double.parseDouble(num);Double resultAddDouble = resultAdd;String s2 = resultAddDouble.toString(); if(!isInt(s2) CalculateResult=s2; else CalculateResult = s2.substring(0,s2.indexOf(.); break;case *: double resultMul = (Div*(Double.parseDouble(num); Double resultMulDouble = resultMul; String s3 = resultMulDouble.toString(); if(!isInt(s3) CalculateResult=s3; else CalculateResult = s3.substring(0,s3.indexOf(.); break;case /: if(Double.parseDouble(num)!=0) double resultDiv = (Div/(Double.parseDouble(num); Double resultDivDouble = resultDiv; String s4 = resultDivDouble.toString(); if(!isInt(s4) CalculateResult=s4; else CalculateResult = s4.substring(0,s4.indexOf(.); else CalculateResult = 除數(shù)不能為零。; break;private void handerBackspace() /退格鍵String show1 = show.getText();if(!bool)if(!(show1.equals(0)try String result = show1.substring(0, show1.length()-1); show.setText(result); if(result.isEmpty() show.setText(0); return; catch(ArrayIndexOutOfBoundsException e)e.printStackTrace();