您好,欢迎光临本网站![请登录][注册会员]  
文件名称: Java编写的简易计算器
  所属分类: Java
  开发工具:
  文件大小: 7kb
  下载次数: 0
  上传时间: 2012-10-17
  提 供 者: s_z****
 详细说明: package chapter11_3; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class Calculator extends JFrame implements ActionListener { private String[] str = { "7", "8", "9", "/", "sqrt", &q uot;4", "5", "6", "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" }; private JTextField tf_out; private JButton jb_bk, jb_ce, jb_c; private JButton[] jb_key; private char ch = '#'; private boolean can = false; private double num1; public void creatGUI() { tf_out = new JTextField(); tf_out.setHorizontalAlignment(JTextField.RIGHT); tf_out.setColumns(18); tf_out.setEditable(false); // 设置输出不可编辑 tf_out.setText("0"); this.add(tf_out, BorderLayout.NORTH); JPanel p = new JPanel(new BorderLayout(3, 8)); JPanel p1 = new JPanel(new GridLayout(1, 3, 3, 10)); p.add(p1, "North"); jb_bk = new JButton("Backspace"); jb_bk.setForeground(Color.RED); jb_bk.addActionListener(this); jb_ce = new JButton("CE"); jb_ce.setForeground(Color.RED); jb_ce.addActionListener(this); jb_c = new JButton("C"); jb_c.setForeground(Color.RED); jb_c.addActionListener(this); p1.add(jb_bk); p1.add(jb_ce); p1.add(jb_c); JPanel p2 = new JPanel(new GridLayout(4, 5, 3, 3)); p.add(p2, BorderLayout.CENTER); jb_key = new JButton[str.length]; for (int i = 0; i < str.length; i++) { jb_key[i] = new JButton(str[i]); jb_key[i].addActionListener(this); if (i == 3 || i == 8 || i == 13 || i == 18 || i == 19) { jb_key[i].setForeground(Color.RED); } else { jb_key[i].setForeground(Color.BLUE); } p2.add(jb_key[i]); } this.add(p, BorderLayout.CENTER); this.setTitle("计算器"); this.setIconImage(new ImageIcon("image/1.jpg").getImage()); this.setBackground(Color.LIGHT_GRAY); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds((1280 - 300) / 2, (768 - 200) / 2, 308, 225); this.setVisible(true); } public static void main(String[] args) { new Calculator().creatGUI(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String act = e.getActionCommand(); // 控制键 if (e.getSource() == jb_bk) { if (tf_out.getText().length() > 1){ tf_out.setText(tf_out.getText().substring(0, tf_out.getText().length() - 1)); }else{ tf_out.setText("0"); } return; }else if (e.getSource() == jb_ce || e.getSource() == jb_c) { tf_out.setText("0"); ch = '#'; return; } // 数字键 if (act == "0") { if (!tf_out.getText().equals("0")) { tf_out.setText(tf_out.getText()); } } else if (act == "1" || act == "2" || act == "3" || act == "4" || act == "5" || act == "6" || act == "7" || act == "9") { tf_out.setText(tf_out.getText()); } // 运算符 if (act.equals("+/-")) { if (tf_out.getText().charAt(0) != '-') { tf_out.setText("-" + tf_out.getText()); } else { tf_out.setText(tf_out.getText().substring(1)); } } else if (act.equals(".")) { tf_out.setText(tf_out.getText() + act); ch = '#'; } else if (act != "1/x" && act.charAt(0) >= '0' && act.charAt(0) <= '9') { if (can) { tf_out.setText(act); can = false; } else { try { if (Double.parseDouble(tf_out.getText()) == 0) { if (tf_out.getText().equals("0.")) { tf_out.setText(tf_out.getText() + act); } else { tf_out.setText(act); } } else { tf_out.setText(tf_out.getText() + act); } } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE); } } } else if (act.equals("+") || act.equals("-") || act.equals("*") || act.equals("/")) { if (ch != '#') { try { num1 = operation(num1, ch, Double.parseDouble(tf_out.getText())); tf_out.setText(String.valueOf(num1)); ch = act.charAt(0); can = true; return; } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE); return; } } else { try { num1 = Double.parseDouble(tf_out.getText()); ch = act.charAt(0); can = true; return; } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE); return; } } } else if (act.equals("sqrt")) { try { double num = (double) Math.sqrt(Double.parseDouble(tf_out.getText())); tf_out.setText(String.valueOf(num)); can = true; return; } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE); } } else if (act.equals("1/x")) { try { double num = 1 / Double.parseDouble(tf_out.getText()); tf_out.setText(String.valueOf(num)); can = true; return; } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE); return; } catch (ArithmeticException e1) { JOptionPane.showMessageDialog(null, "除0错误!", "警告!", JOptionPane.ERROR_MESSAGE); } } else if (act.equals("=")) { can = true; try { if (ch == '#') { return; } double num = Double.parseDouble(tf_out.getText()); num1 = operation(num1, ch, num); tf_out.setText(String.valueOf(num1)); ch = '#'; return; } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE); return; } } else if (act.equals("%")) { double num = Double.valueOf(tf_out.getText()).doubleValue(); tf_out.setText(String.valueOf(num1)); double sum = (num1 * num) / 100; tf_out.setText(String.valueOf(sum)); return; } } // 运算 public double operation(double a, char c, double b) { double sum; switch (c) { case '+': sum = a + b; break; case '-': sum = a - b; break; case '*': sum = a * b; break; case '/': if (b == 0) { JOptionPane.showMessageDialog(null, "除0错误!", "警告!",JOptionPane.ERROR_MESSAGE); return 0; } sum = a / b; break; default: return 0; } return sum; } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: JAVA 计算器
 输入关键字,在本站1000多万海量源码库中尽情搜索: