




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
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、; /格式化十進制數(shù)StringBuffer memory=new StringBuffer(20);/M系列 動態(tài)存放字符串public Calculator()super("科學計算器");/設(shè)置標題欄 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 :刪除當前計算 4.MC :清除緩沖區(qū)n " + "5.MR :恢復緩沖區(qū)的數(shù)到文本框 6.MS :將文本框的數(shù)存入緩沖區(qū)n" + "7.M+ :將文本框值與緩沖區(qū)的數(shù)相加但不顯示結(jié)果 8. 1/x :將文本框中的數(shù)據(jù)為它的倒數(shù)n" + "9.sqrt :將文本框中的內(nèi)容求平方根 10.+/- :負數(shù)符號n" + "11.n! :n的階乘 12.log :求對數(shù)n"+ "13.:角度弧度轉(zhuǎn)化
8、 14.sin/cos/tan :三角函數(shù)" ); ); editItem2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) JOptionPane.showMessageDialog(null,"作者:五班第09小組 李興上 李輝 蘇上超(組長) n " + "本計算器僅供交流使用,如有錯誤,敬請諒解。n本計算器參考了部分資料。n"););menu1.add(editItem1);menu1.add(editItem2);/菜單
9、myBar.add(menu1);/頂層菜單this.setJMenuBar(myBar);numActionListener = new Num();/實現(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) /* * 文本域,即為計算器的屏幕顯示區(qū)域 */tf = new JTextField();tf.setEditable(false);/文本區(qū)域不可編輯tf.setBackground(Color.white);/文本區(qū)域的背景色tf.setHorizontalAlignment(
12、JTextField.RIGHT);/文字右對齊tf.setText("0");tf.setBorder(BorderFactory.createLineBorder(Color.black);/邊框Key sk=new Key(); /鍵盤監(jiān)聽tf.addKeyListener(sk);init();/對計算器進行初始化/* * 初始化操作 * 添加按鈕 */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("計算器");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)聽事件/* * 計算器的基礎(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); /文本顯示/* * 運算符號的事件監(jiān)聽 */ class Signs implements ActionListenerpublic void actionPerformed(ActionEvent e) /* * 用ActionEvent對象的getActionCommand()方法 * 取得與引發(fā)事件對象相關(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("負數(shù)不能開平方根");/* log求常用對數(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("負數(shù)不能求對數(shù)");/*M系列*/ else if(str.equals("MC") String temp = " " memory.replace(0, memory.length(), temp); /replace方法, 第一個參數(shù)是要修改的字符串,第二個參數(shù)是要替換的字符串,第三個參數(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ù)放進行階乘操作int j = (int)i;/強制類型轉(zhuǎn)換int result=1;for(int k=1;k<=j;k+)result *= k;tf.setText(String.valueOf(result);elsetf.setText("無法進行階乘");/* 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ù)的計算 */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對象的getActionCommand()方法 * 取得與引發(fā)事件對象相關(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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國新式茶飲行業(yè)市場競爭格局及投資方向研究報告(智研咨詢)
- 七年級下冊《三元一次方程組的解法》課件與練習
- 構(gòu)建平臺用戶個人信息保護制度
- 2025年衛(wèi)星數(shù)據(jù)采集系統(tǒng)項目合作計劃書
- 農(nóng)業(yè)技術(shù)研發(fā)推廣合作協(xié)議
- 裝修房屋委托合同
- 餐飲業(yè)食品安全保障與事故免責協(xié)議書
- 2025年工業(yè)廢氣凈化裝置項目發(fā)展計劃
- 2025年微球載體材料項目發(fā)展計劃
- 醫(yī)療美容服務項目風險告知與免責協(xié)議
- 委托書范本授權(quán)代理人辦理房屋租賃解除手續(xù)
- 2024CSCO胃腸間質(zhì)瘤診療指南解讀
- 4-6戴維南定理(部編)課件
- DZ∕T 0356-2020 海洋重力測量技術(shù)規(guī)范
- 天然氣處理廠工藝及自控
- 高鐵乘務人員形體訓練全套教學課件
- 基礎(chǔ)護理學智慧樹知到期末考試答案章節(jié)答案2024年德州學院
- (高清版)JTGT 4320-2022 公路車輛動態(tài)稱重檢測系統(tǒng)技術(shù)規(guī)范
- 小學思政課建設(shè)實施方案及措施
- 團餐服務培訓資料
- 喉全切除術(shù)手術(shù)配合
評論
0/150
提交評論