java計(jì)算器源代碼



《java計(jì)算器源代碼》由會(huì)員分享,可在線閱讀,更多相關(guān)《java計(jì)算器源代碼(10頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、package 計(jì)算器; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.math.BigDecimal; im
2、port javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLook
3、AndFeelException; import ; import javax.swing.text.BadLocationException; public class Counter1 extends JFrame implements ActionListener { public static final int DEF_DIV_SCALE = 10; //默認(rèn)的精度 private JMenuBar jmBar = new JMenuBar(); // 菜單條 private JMenu edit = new JMenu("編輯(E)"); privat
4、e JMenu find = new JMenu("查看(V)"); private JMenu help = new JMenu("幫助(H)"); // 創(chuàng)建JPanel便于管理 private JPanel panTxt = new JPanel(); private JPanel panBack = new JPanel(); private JPanel panMen = new JPanel(); private JPanel panNum = new JPanel(); // 創(chuàng)建文本域 private JTextField txt = new
5、 JTextField(); // 創(chuàng)建文本標(biāo)簽 private JLabel jlm = new JLabel(); private boolean flag = false; // 是否可以消除文本 private double a = 0; // 數(shù)字a的值 private double b = 0; // 數(shù)字b的值 private double result = 0; // 兩個(gè)數(shù)字運(yùn)算的結(jié)果 private String sign = ""; // 標(biāo)記運(yùn)算法則 private double m = 0; // 暫時(shí)儲(chǔ)存數(shù)據(jù) // 創(chuàng)建按
6、鈕 private NewButton jbBack = new NewButton("Backspace", Color.RED); private NewButton jbCE = new NewButton("CE", Color.RED); private NewButton jbC = new NewButton("C", Color.RED); private NewButton jbMC = new NewButton("MC", Color.RED); private NewButton jbMR = new NewButton("MR", Color.RE
7、D); private NewButton jbMS = new NewButton("MS", Color.RED); private NewButton jbM = new NewButton("M+", Color.RED); public Counter1() { setSize(260, 260); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setLayout(null); setJMenuBar(jmBar); setT
8、itle("計(jì)算器"); setResizable(false); init(); // Container contentPane = getContentPane(); // Controller c = new Controller(this); // contentPane.addKeyListener(c); } private void init() { // 設(shè)置第一個(gè)JPanel里面的內(nèi)容 panTxt.setSize(230, 20); panTxt.setLocation(15, 5); // panTxt.
9、setBorder(BorderFactory.createLineBorder(Color.RED)); panTxt.setLayout(new BorderLayout()); // 設(shè)置布局管理器 txt.setHorizontalAlignment(JTextField.RIGHT); // 使光標(biāo)定位在右邊 txt.setBorder(BorderFactory.createLoweredBevelBorder()); // 設(shè)置邊框凹下去 txt.setEditable(false); // 設(shè)置是否可以編輯 txt.setBackground(Co
10、lor.WHITE); // txt.setEnabled(true); //設(shè)置是否激活 // txt.setFocusable(false); // 設(shè)置是否可以獲得焦點(diǎn)(顯示光標(biāo)) txt.setText("0"); add(panTxt); panTxt.add(txt); Controller c = new Controller(this); txt.addKeyListener(c); // 設(shè)置第二個(gè)JPanel里面的內(nèi)容 panBack.setSize(190, 25); panBack.setLocation(
11、52, 35); // 畫出邊框便于查看 // panBack.setBorder(BorderFactory.createLineBorder(Color.RED)); GridLayout layPanBack = new GridLayout(); // 創(chuàng)建布局管理器 layPanBack.setHgap(5); // 設(shè)置布局管理器的邊距 panBack.setLayout(layPanBack); // 設(shè)置布局管理器 // 添加按鈕 jbBack.setFont(new Font("宋體", 0, 12)); panBack.a
12、dd(jbBack); panBack.add(jbCE); panBack.add(jbC); add(panBack); // 設(shè)置第三個(gè)JPanel里面的內(nèi)容 panMen.setSize(40, 140); panMen.setLocation(5, 65); GridLayout layPanMen = new GridLayout(4, 1); // 創(chuàng)建布局管理器 layPanMen.setVgap(5); // 設(shè)置布局管理器的邊距 panMen.setLayout(layPanMen); // panMen.se
13、tBorder(BorderFactory.createLineBorder(Color.RED)); panMen.add(jbMC); panMen.add(jbMR); panMen.add(jbMS); panMen.add(jbM); add(panMen); // 設(shè)置第四個(gè)JPanel里面的內(nèi)容 panNum.setSize(200, 140); panNum.setLocation(50, 65); // panNum.setBorder(BorderFactory.createLineBorder(Color.RED)
14、); add(panNum); GridLayout layPanNum = new GridLayout(4, 5); // 創(chuàng)建布局管理器 panNum.setLayout(layPanNum); newButtonNum(); // 添加菜單項(xiàng) jmBar.add(edit); jmBar.add(find); jmBar.add(help); // 設(shè)置文本標(biāo)簽的屬性和添加文本標(biāo)簽 jlm.setBorder(BorderFactory.createBevelBorder(1)); jlm.setSize(25,
15、 25); jlm.setLocation(15, 35); add(jlm); } public void newButtonNum() { NewButton jb7 = new NewButton("7", Color.BLUE); NewButton jb8 = new NewButton("8", Color.BLUE); NewButton jb9 = new NewButton("9", Color.BLUE); NewButton jbDivide = new NewButton("/", Color.RED); NewBu
16、tton jbSqrt = new NewButton("sqrt", Color.BLUE); NewButton jb4 = new NewButton("4", Color.BLUE); NewButton jb5 = new NewButton("5", Color.BLUE); NewButton jb6 = new NewButton("6", Color.BLUE); NewButton jbRide = new NewButton("*", Color.RED); NewButton jbPer = new NewButton("%", Color
17、.BLUE); NewButton jb1 = new NewButton("1", Color.BLUE); NewButton jb2 = new NewButton("2", Color.BLUE); NewButton jb3 = new NewButton("3", Color.BLUE); NewButton jbCut = new NewButton("-", Color.RED); NewButton jbReciprocal = new NewButton("1/x", Color.BLUE); NewButton jb0 = new Ne
18、wButton("0", Color.BLUE); NewButton jbMinus = new NewButton("+/-", Color.BLUE); NewButton jbDot = new NewButton(".", Color.BLUE); NewButton jbAdd = new NewButton("+", Color.RED); NewButton jbEquals = new NewButton("=", Color.RED); panNum.add(jb7); panNum.add(jb8); panNum.add(j
19、b9); panNum.add(jbDivide); panNum.add(jbSqrt); panNum.add(jb4); panNum.add(jb5); panNum.add(jb6); panNum.add(jbRide); panNum.add(jbPer); panNum.add(jb1); panNum.add(jb2); panNum.add(jb3); panNum.add(jbCut); panNum.add(jbReciprocal); panNum.add(jb0); panNum.a
20、dd(jbMinus); panNum.add(jbDot); panNum.add(jbAdd); panNum.add(jbEquals); } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // 設(shè)置和win7的布局一樣 } catch (ClassNotFoundException e) { e.printStackTrace(); } c
21、atch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); } new Counter1().setVisible(true); } private class NewButton extends JButton { public N
22、ewButton(String s, Color c) { super(s); // setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); setBorder(BorderFactory.createEmptyBorder()); setForeground(c); setFocusable(false); // 設(shè)置是否可以獲得焦點(diǎn)(顯示光標(biāo)) addActionListener(Counter1.this); } } // 實(shí)現(xiàn)監(jiān)聽 public
23、void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "+") { a = Double.parseDouble(txt.getText()); sign = "+"; flag = true; } else if (e.getActionCommand() == "-") { a = Double.parseDouble(txt.getText()); sign = "-"; flag = true; } else if (e.getActionCo
24、mmand() == "*") { a = Double.parseDouble(txt.getText()); sign = "*"; flag = true; } else if (e.getActionCommand() == "/") { a = Double.parseDouble(txt.getText()); sign = "/"; flag = true; } else if (e.getActionCommand() == "=") { try { b = Double.parseDouble(tx
25、t.getText()); } catch (NumberFormatException nfe) { b = 0; } try { if (sign == "+") { result = doubleAdd(a, b); } else if (sign == "-") { result = doubleSub(a, b); } else if (sign == "*") { result = doubleMul(a, b); } else if (sign == "/" && b
26、 != 0) { result = doubleDiv(a, b, 15); } else { result = b; } } catch (NumberFormatException nfe) { result = 0; } a = result; if (sign == "/" && b == 0) { txt.setText("0"); } else { txt.setText(result + ""); } flag = true; } else if
27、 (e.getActionCommand() == "C") { a = 0; result = 0; sign = ""; txt.setText("0"); } else if (e.getActionCommand() == "CE") { txt.setText("0"); } else if (e.getActionCommand() == "Backspace") { try { if (txt.getText().length() > 1) { txt.setText(txt.getText(0,
28、txt.getText().length() - 1)); } else { txt.setText("0"); } } catch (BadLocationException e1) { txt.setText("0"); } } else if (e.getActionCommand() == "sqrt") { a = Double.parseDouble(txt.getText()); result = Math.sqrt(a); a = result; txt.setText(result
29、 + ""); flag = true; } else if (e.getActionCommand() == "%") { b = Double.parseDouble(txt.getText()); result = doubleMul(doubleDiv(b, 100), a); txt.setText(result + ""); flag = true; } else if (e.getActionCommand() == "1/x") { a = Double.parseDouble(txt.getText());
30、result = 1 / a; a = result; txt.setText(result + ""); flag = true; } else if (e.getActionCommand() == "+/-") { txt.setText("" + (-Double.parseDouble(txt.getText()))); } else if (e.getActionCommand() == "MC") { m = 0; jlm.setText(""); } else if (e.getActionCommand() =
31、= "MR") { txt.setText(m + ""); } else if (e.getActionCommand() == "MS") { m = Double.parseDouble(txt.getText()); jlm.setText(" M"); flag = true; } else if (e.getActionCommand() == "M+") { m += Double.parseDouble(txt.getText()); flag = true; } else { boolean isDo
32、c = false; for (int i = 0; i < txt.getText().length(); i++) { if (txt.getText().charAt(i) == '.') { isDoc = true; } } if (!isDoc || e.getActionCommand() != ".") { if (flag) { txt.setText(""); flag = false; } if (txt.getText().equals("0") && e.
33、getActionCommand() != ".") { txt.setText(""); } txt.setText(txt.getText() + e.getActionCommand()); } } } // 實(shí)現(xiàn)鍵盤監(jiān)聽 public void keyAction(KeyEvent e) { if (e.getKeyChar() == '+') { a = Double.parseDouble(txt.getText()); sign = "+"; flag = true; } else
34、if (e.getKeyChar() == '-') { a = Double.parseDouble(txt.getText()); sign = "-"; flag = true; } else if (e.getKeyChar() == '*') { a = Double.parseDouble(txt.getText()); sign = "*"; flag = true; } else if (e.getKeyChar() == '/') { a = Double.parseDouble(txt.getText())
35、; sign = "/"; flag = true; } else if (e.getKeyChar() == '=') { try { b = Double.parseDouble(txt.getText()); } catch (NumberFormatException nfe) { b = 0; } try { if (sign == "+") { result = doubleAdd(a, b); } else if (sign == "-") { result
36、= doubleSub(a, b); } else if (sign == "*") { result = doubleMul(a, b); } else if (sign == "/") { if (b == 0) { txt.setText("除數(shù)不能為0"); } else { result = doubleDiv(a, b, 15); } } else { result = b; } } catch (NumberFormatException nfe
37、) { result = 0; } a = result; if (sign == "/" && b == 0) { txt.setText("0"); } else { txt.setText(result + ""); } flag = true; } else if (e.getKeyChar() == 'c' || e.getKeyChar() == 'C') { a = 0; result = 0; sign = ""; txt.setText("0"); } e
38、lse if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { try { if (txt.getText().length() > 1) { txt.setText(txt.getText(0, txt.getText().length() - 1)); } else { txt.setText("0"); } } catch (BadLocationException e1) { txt.setText("0"); } } else if (e.getKey
39、Char() == '%') { b = Double.parseDouble(txt.getText()); result = doubleMul(doubleDiv(b, 100), a); txt.setText(result + ""); flag = true; } else { if (e.getKeyChar() >= 0x30 && e.getKeyChar() <= 0x39 || e.getKeyChar() == '.') { boolean isDoc = false; for (int i
40、= 0; i < txt.getText().length(); i++) { if (txt.getText().charAt(i) == '.') { isDoc = true; } } if (!isDoc || e.getKeyChar() != '.') { if (flag) { txt.setText(""); flag = false; } if (txt.getText().equals("0") && e.getKeyChar() != '.') {
41、 txt.setText(""); } txt.setText(txt.getText() + e.getKeyChar()); } } } } private class Controller extends KeyAdapter { private Counter1 frame; public Controller(Counter1 frame) { this.frame = frame; } public void keyPressed(KeyEvent e) {
42、 frame.keyAction(e); } } // 提供相對(duì)精確的加法運(yùn)算 public static double doubleAdd(double d1, double d2) { BigDecimal bd1 = new BigDecimal(Double.toString(d1)); BigDecimal bd2 = new BigDecimal(Double.toString(d2)); return bd1.add(bd2).doubleValue(); } // 提供相對(duì)精確的減法運(yùn)算 public static dou
43、ble doubleSub(double d1, double d2) { BigDecimal bd1 = new BigDecimal(Double.toString(d1)); BigDecimal bd2 = new BigDecimal(Double.toString(d2)); return bd1.subtract(bd2).doubleValue(); } // 提供相對(duì)精確的乘法運(yùn)算 public static double doubleMul(double d1, double d2) { BigDecimal bd1 = new B
44、igDecimal(Double.toString(d1)); BigDecimal bd2 = new BigDecimal(Double.toString(d2)); return bd1.multiply(bd2).doubleValue(); } // 提供相對(duì)精確的除法運(yùn)算,當(dāng)發(fā)生除不盡的情況時(shí),精確到 小數(shù)點(diǎn)以后10位,以后的數(shù)字四舍五入。 public static double doubleDiv(double d1, double d2) { return doubleDiv(d1, d2, DEF_DIV_SCALE); } /
45、/ 提供(相對(duì))精確的除法運(yùn)算。當(dāng)發(fā)生除不盡的情況時(shí),由scale參數(shù)指 定精度,以后的數(shù)字四舍五入。 public static double doubleDiv(double d1, double d2, int scale) { if (scale < 0) { throw new IllegalArgumentException( "The scale must be a positiveinteger or zero"); } BigDecimal bd1 = new BigDecimal(Double.toString(d1)); BigDecimal bd2 = new BigDecimal(Double.toString(d2)); return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } }
- 溫馨提示:
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. 裝配圖網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 離心泵的檢修各零部件檢修標(biāo)準(zhǔn)
- 金屬材料疲勞強(qiáng)度的八大主要影響因素
- 機(jī)械安全知識(shí)
- 電機(jī)的工作原理與種類
- 設(shè)備點(diǎn)檢內(nèi)容
- 有效防止液壓系統(tǒng)漏油的技術(shù)要領(lǐng)
- 鈑金和管工機(jī)械安全操作規(guī)程
- 閥門的100個(gè)專業(yè)術(shù)語(yǔ)
- 某單位機(jī)械設(shè)備安全檢查表
- 離心泵的汽蝕與吸入特性
- 過(guò)濾網(wǎng)目數(shù)標(biāo)準(zhǔn)
- 減少設(shè)備潤(rùn)滑故障的措施
- 離心泵機(jī)械密封安裝使用規(guī)則
- 閥門常見故障與原因
- 呼吸閥和真空破壞閥基礎(chǔ)知識(shí)總結(jié)
相關(guān)資源
更多