java可鍵盤輸入計(jì)算器代碼_第1頁
java可鍵盤輸入計(jì)算器代碼_第2頁
java可鍵盤輸入計(jì)算器代碼_第3頁
java可鍵盤輸入計(jì)算器代碼_第4頁
java可鍵盤輸入計(jì)算器代碼_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.text.DecimalFormat;import j

2、avax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class Calculator extends JFrame private JTextF

3、ield tf;private JPanel panel1, panel2, panel3, panel4;/組件private JMenuBar myBar;private JMenu menu1;private JMenuItem editItem1, editItem2;private boolean IfResult = true, flag = false;private String oper = "="private double result = 0;private Num numActionListener;private DecimalFormat df

4、; /格式化十進(jìn)制數(shù)StringBuffer memory=new StringBuffer(20);/M系列 動(dòng)態(tài)存放字符串public Calculator()super("科學(xué)計(jì)算器");/設(shè)置標(biāo)題欄 df = new DecimalFormat("#.#");/保留四位小數(shù)this.setLayout(new BorderLayout(10, 5); /總體布局panel1 = new JPanel(new GridLayout(1, 3, 10, 10);/網(wǎng)格布局管理器panel2 = new JPanel(new GridLayout(5,

5、 6, 5, 5);/5行6列panel3 = new JPanel(new GridLayout(5, 1, 5, 5);panel4 = new JPanel(new BorderLayout(5, 5);/* * 菜單欄 */myBar = new JMenuBar();menu1 = new JMenu("查看");menu1.setFont(new Font("宋體", Font.PLAIN, 12);editItem1 = new JMenuItem("幫助(F)",(int)'F');editItem2

6、= new JMenuItem("開發(fā)者信息(M)",(int)'M');editItem1.setFont(new Font("宋體",Font.PLAIN,12);editItem2.setFont(new Font("宋體",Font.PLAIN,12);editItem1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)JOptionPane.showMessageDialog(null,"

7、1.CE :清除文本框 2.BackSpace :回刪剛輸入數(shù)字n " + "3.C :刪除當(dāng)前計(jì)算 4.MC :清除緩沖區(qū)n " + "5.MR :恢復(fù)緩沖區(qū)的數(shù)到文本框 6.MS :將文本框的數(shù)存入緩沖區(qū)n" + "7.M+ :將文本框值與緩沖區(qū)的數(shù)相加但不顯示結(jié)果 8. 1/x :將文本框中的數(shù)據(jù)為它的倒數(shù)n" + "9.sqrt :將文本框中的內(nèi)容求平方根 10.+/- :負(fù)數(shù)符號(hào)n" + "11.n! :n的階乘 12.log :求對(duì)數(shù)n"+ "13.:角度弧度轉(zhuǎn)化

8、 14.sin/cos/tan :三角函數(shù)" ); ); editItem2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) JOptionPane.showMessageDialog(null,"作者:五班第09小組 李興上 李輝 蘇上超(組長(zhǎng)) n " + "本計(jì)算器僅供交流使用,如有錯(cuò)誤,敬請(qǐng)諒解。n本計(jì)算器參考了部分資料。n"););menu1.add(editItem1);menu1.add(editItem2);/菜單

9、myBar.add(menu1);/頂層菜單this.setJMenuBar(myBar);numActionListener = new Num();/實(shí)現(xiàn)數(shù)字監(jiān)聽 class Key extends JPanel implements KeyListener public void keyPressed(KeyEvent e) public void keyTyped(KeyEvent e) char x=e.getKeyChar(); if(x>='0'&&x<='9') if(IfResult)tf.setText("

10、;");IfResult = false; tf.setText(tf.getText().trim() + Character.toString(x);if(tf.getText().equals("0")tf.setText("0");IfResult = true;flag = true; if (x='+' | x='-' | x='*' | x='/' | x='=') if(flag) IfResult = false; if(IfResult) ope

11、r = Character.toString(x); else getResult(Double.parseDouble(tf.getText(); oper = Character.toString(x); IfResult = true; public void keyReleased(KeyEvent e) /* * 文本域,即為計(jì)算器的屏幕顯示區(qū)域 */tf = new JTextField();tf.setEditable(false);/文本區(qū)域不可編輯tf.setBackground(Color.white);/文本區(qū)域的背景色tf.setHorizontalAlignment(

12、JTextField.RIGHT);/文字右對(duì)齊tf.setText("0");tf.setBorder(BorderFactory.createLineBorder(Color.black);/邊框Key sk=new Key(); /鍵盤監(jiān)聽tf.addKeyListener(sk);init();/對(duì)計(jì)算器進(jìn)行初始化/* * 初始化操作 * 添加按鈕 */private void init()addButton(panel1, "Backspace", new Clear(), Color.black);addButton(panel1, &quo

13、t;CE", new Clear(), Color.black);addButton(panel1, "C", new Clear(), Color.black);addButton(panel2, "1/x", new Signs(), Color.black);addButton(panel2, "log", new Signs(), Color.black);addButton(panel2, "7", numActionListener, Color.blue);addButton(panel2,

14、 "8", numActionListener, Color.blue);addButton(panel2, "9", numActionListener, Color.blue);addButton(panel2, "÷", new Signs(), Color.black);addButton(panel2, "n!", new Signs(), Color.black);addButton(panel2, "sqrt", new Signs(), Color.black);add

15、Button(panel2, "4", numActionListener, Color.blue);addButton(panel2, "5", numActionListener, Color.blue);addButton(panel2, "6", numActionListener, Color.blue);addButton(panel2, "×", new Signs(), Color.black);addButton(panel2, "sin", new Signs(),

16、 Color.black);addButton(panel2, "x2", new Signs(), Color.black);addButton(panel2, "1", numActionListener, Color.blue);addButton(panel2, "2", numActionListener, Color.blue);addButton(panel2, "3", numActionListener, Color.blue);addButton(panel2, "-", n

17、ew Signs(), Color.black);addButton(panel2, "cos", new Signs(), Color.black);addButton(panel2, "x3", new Signs(), Color.black); addButton(panel2, "-/+", new Clear(), Color.black);addButton(panel2, "0", numActionListener, Color.blue);addButton(panel2, ".&qu

18、ot;, new Dot(), Color.black);addButton(panel2, "+", new Signs(), Color.black);addButton(panel2, "tan", new Signs(), Color.black);addButton(panel2, "%", new Signs(), Color.black);addButton(panel2, "", numActionListener, Color.black);addButton(panel2, "e&qu

19、ot;, numActionListener, Color.black);addButton(panel2, "", new Signs(), Color.black);addButton(panel2, "=", new Signs(), Color.black); JButton bm = new JButton("計(jì)算器");bm.setBorder(BorderFactory.createLoweredBevelBorder(); /凹陷bm.setEnabled(false);/按鈕不可操作bm.setPreferredSi

20、ze(new Dimension(20, 20);/寬 高panel3.add(bm);/加入按鈕addButton(panel3, "MC", new Signs(), Color.black);addButton(panel3, "MR", new Signs(), Color.black);addButton(panel3, "MS", new Signs(), Color.black);addButton(panel3, "M+", new Signs(), Color.black);panel4.add(

21、panel1, BorderLayout.NORTH);panel4.add(panel2, BorderLayout.CENTER);this.add(tf, BorderLayout.NORTH);this.add(panel3, BorderLayout.WEST);this.add(panel4); /*總窗口*/pack(); /正好容納放置的所有組件this.setResizable(true);/窗口可改變大小this.setLocation(300, 200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/關(guān)閉 /*

22、* 統(tǒng)一設(shè)置按鈕的的使用方式 * param panel * param name * param action * param color */private 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);/增加監(jiān)聽事件/* * 計(jì)算器的基礎(chǔ)操作(+ -

23、 × ÷) * param x */private void getResult (double x)if(oper.equals( "+")result += x;else if(oper.equals("-")result -= x;else if(oper.equals ( "×")result *= x;else if(oper.equals( "÷")result /= x;else if(oper.equals("=")result = x;t

24、f.setText(df.format(result); /文本顯示/* * 運(yùn)算符號(hào)的事件監(jiān)聽 */ class Signs implements ActionListenerpublic void actionPerformed(ActionEvent e) /* * 用ActionEvent對(duì)象的getActionCommand()方法 * 取得與引發(fā)事件對(duì)象相關(guān)的字符串 */String str = e.getActionCommand();/* sqrt求平方根 */if(str.equals("sqrt") /“equals()”比較字符串中所包含的內(nèi)容是否相同

25、double i = Double.parseDouble(tf.getText(); /字符轉(zhuǎn)化為doubleif(i>=0)/* * String.valueOf() 轉(zhuǎn)換為字符串 * df.format() 按要求保留四位小數(shù) * Math.sqrt() 求算數(shù)平方根 */tf.setText(String.valueOf(df.format(Math.sqrt(i);elsetf.setText("負(fù)數(shù)不能開平方根");/* log求常用對(duì)數(shù) */else if(str.equals("log")double i = Double.pars

26、eDouble(tf.getText();if(i>0)tf.setText(String.valueOf(df.format(Math.log(i);elsetf.setText("負(fù)數(shù)不能求對(duì)數(shù)");/*M系列*/ else if(str.equals("MC") String temp = " " memory.replace(0, memory.length(), temp); /replace方法, 第一個(gè)參數(shù)是要修改的字符串,第二個(gè)參數(shù)是要替換的字符串,第三個(gè)參數(shù)是替換成什么樣的字符串. else if(str.eq

27、uals("MR") tf.setText(memory.toString(); else if(str.equals("MS") String temp = tf.getText().trim(); memory.replace(0, memory.length(), temp); else if(str.equals("M+") String temp1 = tf.getText().trim(); Double num = Double.parseDouble(temp1); String temp2 = memory.toSt

28、ring(); num += Double.parseDouble(temp2); temp1 = String.valueOf(num); /整形 memory.replace(0, memory.length(), temp1); /* %求百分比 */else if(str.equals("%")tf.setText(df.format(Double.parseDouble(tf.getText() / 100);/* 1/x求倒數(shù) */else if(str.equals("1/x")if(Double.parseDouble(tf.getTex

29、t() = 0)tf.setText("除數(shù)不能為零");elsetf.setText(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求余弦函數(shù) */else if(str.equals("cos")double i =

30、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);/* n!求階乘 */else if(str.equals("n!")double i = Double.parseDouble(tf.ge

31、tText();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);elsetf.setText("無法進(jìn)行階乘");/* x2求平方 */else if(str.equals("x2")double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format

32、(i*i);/* x3求立方 */else if(str.equals("x3")double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(i*i*i);/* 角度轉(zhuǎn)換 */* * 將角度值轉(zhuǎn)換成弧度值,方便三角函數(shù)的計(jì)算 */else if(str.equals("")double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(i/180*Math.PI);elseif(fla

33、g)IfResult = false;if(IfResult)oper = str;else getResult(Double.parseDouble(tf.getText();oper = str;IfResult = true;/* * 清除按鈕的事件監(jiān)聽 */class Clear implements ActionListenerpublic void actionPerformed(ActionEvent e) /* * 用ActionEvent對(duì)象的getActionCommand()方法 * 取得與引發(fā)事件對(duì)象相關(guān)的字符串 */String str = e.getActionCommand();if(str = "C")tf.setText("0");IfResult = true;result = 0;else if(str = "-/+")double i = 0 - Double.parseDouble(tf.getText().trim();tf.setText(df.format(i);else if(str = "Backspace")if(Double.parseDoubl

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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ǔ)空間,僅對(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)論