
一个使用Java开发的简单计算器。
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
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, 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; }}
全部评论 (0)


