java规范的计算器的实现

import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;

public class Calc extends JFrame {
 // 声明三个面板的布局
 GridLayout gl1, gl2, gl3;

 Panel p0, p1, p2, p3;

 JTextField inputText;

 TextField displayText;

 JButton backspaceBtn, ceBtn, cBtn, mcBtn, mrBtn, msBtn, mPlusBtn;

 JButton btns[] = new JButton[20];

 String[] btNames = { "7", "8", "9", "/", "sqrt", "4", "5", "6", "*", "%",
   "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" };

 StringBuffer str;// 显示屏所显示的字符串

 double x, y;// x和y都是运算数

 int z;// Z表示单击了那一个运算符.0表示"+",1表示"-",2表示"*",3表示"/"

 static double m;// 记忆的数字

 public Calc() {
  super("计算器");
  MenuBar mb = new MenuBar();
  setMenuBar(mb);
  Menu m1 = new Menu("编辑(E)");
  MenuItem copy = new MenuItem("复制(C) Ctrl+C");
  MenuItem paste = new MenuItem("粘贴(P) Ctrl+V");
  m1.add(copy);
  m1.add(paste);
  Menu m2 = new Menu("查看");
  Menu m3 = new Menu("帮助");
  mb.add(m1);
  mb.add(m2);
  mb.add(m3);

  gl1 = new GridLayout(1, 3, 4, 0);// 实例化三个面板的布局
  gl2 = new GridLayout(4, 1, 2, 3);
  gl3 = new GridLayout(4, 5, 2, 3);

  inputText = new JTextField(38);// 显示屏
  inputText.setHorizontalAlignment(JTextField.RIGHT);
  // inputText.setEnabled(false);
  inputText.setText("0.");
  displayText = new TextField(1);// 显示记忆的索引值
  displayText.setEditable(false);

  // 实例化所有按钮、设置其前景色并注册监听器
  backspaceBtn = new JButton("Backspace");
  backspaceBtn.setMargin(new Insets(2, 1, 2, 1));
  backspaceBtn.setForeground(Color.red);
  backspaceBtn.addActionListener(new Bt());
  ceBtn = new JButton("CE");
  ceBtn.setMargin(new Insets(4, 1, 4, 1));
  ceBtn.setForeground(Color.red);
  ceBtn.addActionListener(new Bt());
  cBtn = new JButton("C");
  cBtn.setMargin(new Insets(4, 1, 4, 1));
  cBtn.setForeground(Color.red);
  cBtn.addActionListener(new Bt());
  mcBtn = new JButton("MC");
  mcBtn.setMargin(new Insets(4, 1, 4, 1));
  mcBtn.setForeground(Color.red);
  mcBtn.addActionListener(new Bt());
  mrBtn = new JButton("MR");
  mrBtn.setMargin(new Insets(4, 1, 4, 1));
  mrBtn.setForeground(Color.red);
  mrBtn.addActionListener(new Bt());
  msBtn = new JButton("MS");
  msBtn.setMargin(new Insets(4, 1, 4, 1));
  msBtn.setForeground(Color.red);
  msBtn.addActionListener(new Bt());
  mPlusBtn = new JButton("M+");
  mPlusBtn.setMargin(new Insets(4, 1, 4, 1));
  mPlusBtn.setForeground(Color.red);
  mPlusBtn.addActionListener(new Bt());
  for (int i = 0; i < btns.length; i++) {
   btns[i] = new JButton(btNames[i]);
   btns[i].setMargin(new Insets(4, 1, 4, 1));
   btns[i].setForeground(Color.blue);
   if ((i + 1) % 5 == 4 || i == btns.length - 1) {
    btns[i].setForeground(Color.red);
   }
   btns[i].addActionListener(new Bt());
  }
  // 实例化四个面板
  p0 = new Panel();
  p1 = new Panel();
  p2 = new Panel();
  p3 = new Panel();
  // 创建一个空字符串缓冲区
  str = new StringBuffer();

  // 添加面板p0中的组件和设置其在框架中的位置和大小
  p0.add(inputText);
  p0.setLayout(new FlowLayout(FlowLayout.LEFT));
  p0.setBounds(1, 2, 260, 30);
  // 添加面板p1中的组件和设置其在框架中的位置和大小
  Panel p11 = new Panel();
  p11.setLayout(gl1);
  Panel p = new Panel();
  p.setBounds(4, 5, 50, 55);
  p11.setBounds(55, 10, 190, 25);
  p.add(displayText);
  p1.setLayout(null);
  p1.add(p);
  p11.add(backspaceBtn);
  p11.add(ceBtn);
  p11.add(cBtn);
  p1.add(p);
  p1.add(p11);
  p1.setBounds(0, 30, 260, 40);
  // 添加面板p2中的组件并设置其的框架中的位置和大小
  p2.setLayout(gl2);
  p2.add(mcBtn);
  p2.add(mrBtn);
  p2.add(msBtn);
  p2.add(mPlusBtn);
  p2.setBounds(10, 70, 35, 120);
  // 添加面板p3中的组件并设置其在框架中的位置和大小
  p3.setLayout(gl3);// 设置p3的布局
  for (int i = 0; i < btns.length; i++) {
   p3.add(btns[i]);
  }
  p3.setBounds(55, 70, 190, 120);
  // 设置框架中的布局为空布局并添加4个面板
  setLayout(null);
  add(p0);
  add(p1);
  add(p2);
  add(p3);
  setResizable(false);// 禁止调整框架的大小
  // 匿名类关闭窗口
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e1) {
    System.exit(0);
   }
  });
  setBackground(Color.lightGray);
  setBounds(400, 200, 260, 250);
  setVisible(true);
  enableInputMethods(true);

 }

 // 构造监听器
 class Bt implements ActionListener {
  public void actionPerformed(ActionEvent e2) {
   try {

    if (e2.getSource() == ceBtn)// 选择"CE"清零
    {
     inputText.setText("0.");// 把显示屏清零
     str.setLength(0);// 清空字符串缓冲区以准备接收新的输入运算数
    } else if (e2.getSource() == cBtn)// 选择"C"清零
    {
     inputText.setText("0.");// 把显示屏清零
     str.setLength(0);
     x = 0d;
     y = 0d;
     z = 0;
    } else if (e2.getSource() == btns[16])// 单击"+/-"选择输入的运算数是正数还是负数
    {
     x = Double.parseDouble(inputText.getText().trim());
     inputText.setText("" + (-x));
    } else if (e2.getSource() == btns[18])// 单击加号按钮获得x的值和z的值并清空y的值
    {
     x = Double.parseDouble(inputText.getText().trim());
     str.setLength(0);// 清空缓冲区以便接收新的另一个运算数
     y = 0d;
     z = 0;
    } else if (e2.getSource() == btns[13])// 单击减号按钮获得x的值和z的值并清空y的值
    {
     x = Double.parseDouble(inputText.getText().trim());
     str.setLength(0);
     y = 0d;
     z = 1;
    } else if (e2.getSource() == btns[8])// 单击乘号按钮获得x的值和z的值并清空y的值
    {
     x = Double.parseDouble(inputText.getText().trim());
     str.setLength(0);
     y = 0d;
     z = 2;
    } else if (e2.getSource() == btns[3])// 单击除号按钮获得x的值和z的值并空y的值
    {
     x = Double.parseDouble(inputText.getText().trim());
     str.setLength(0);
     y = 0d;
     z = 3;
    } else if (e2.getSource() == btns[19])// 单击等号按钮输出计算结果
    {
     str.setLength(0);
     switch (z) {
     case 0:
      inputText.setText("" + (x + y));
      break;
     case 1:
      inputText.setText("" + (x - y));
      break;
     case 2:
      inputText.setText("" + (x * y));
      break;
     case 3:
      inputText.setText("" + (x / y));
      break;
     }
    } else if (e2.getSource() == btns[17])// 单击"."按钮输入小数
    {
     System.out.println("sdfsfd");
     if (inputText.getText().trim().indexOf(".") != -1)// 判断字符串中是否已经包含了小数点
     {
      if (inputText.getText().trim().equals("0."))// 如果初时显示为0
      {
       str.setLength(0);
       inputText.setText((str.append("0.")).toString());
      }
     } else// 如果没数点有小
     {
      if (inputText.getText().trim().equals(""))// 如果初时显示为空则不做任何操作
      {
      } else {
       inputText.setText(str.append(e2.getActionCommand())
         .toString());
      }
     }

     y = 0d;

    } else if (e2.getSource() == btns[4])// 求平方根
    {
     x = Double.parseDouble(inputText.getText().trim());
     inputText.setText("数字格式异常");
     if (x < 0)
      inputText.setText("负数没有平方根");
     else
      inputText.setText("" + Math.sqrt(x));
     str.setLength(0);
     y = 0d;
    } else if (e2.getSource() == btns[9])// 单击了"%"按钮
    {
     x = Double.parseDouble(inputText.getText().trim());
     inputText.setText("" + (0.01 * x));
     str.setLength(0);
     y = 0d;
    } else if (e2.getSource() == btns[14])// 单击了"1/X"按钮
    {

     x = Double.parseDouble(inputText.getText().trim());
     if (x == 0) {

      inputText.setText("除数不能为零");
     } else {
      inputText.setText("" + (1 / x));
     }
     str.setLength(0);
     y = 0d;
    } else if (e2.getSource() == mcBtn)// MC为清除内存
    {
     m = 0d;
     displayText.setText("");
     str.setLength(0);
    } else if (e2.getSource() == mrBtn)// MR为重新调用存储的数据
    {
     if (displayText.getText().trim() != "")// 有记忆数字
     {
      inputText.setText("" + m);
     }
    } else if (e2.getSource() == msBtn)// MS为存储显示的数据
    {

     m = Double.parseDouble(inputText.getText().trim());
     displayText.setText("M");
     inputText.setText("0");
     str.setLength(0);
    } else if (e2.getSource() == mPlusBtn)// M+为将显示的数字与已经存储的数据相加要查看新的数字单击MR
    {
     m = m + Double.parseDouble(inputText.getText().trim());
    } else// 选择的是其他的按钮
    {
     if (e2.getSource() == btns[15])// 如果选择的是"0"这个数字键
     {
      if (inputText.getText().trim().equals("0."))// 如果显示屏显示的为零不做操作
      {

      } else {
       inputText.setText(str.append(e2.getActionCommand())
         .toString());
       y = Double.parseDouble(inputText.getText().trim());
      }
     } else if (e2.getSource() == backspaceBtn)// 选择的是“BackSpace”按钮
     {
      if (!inputText.getText().trim().equals("0."))// 如果显示屏显示的不是零
      {
       if (str.length() != 1) {
        inputText.setText(str.delete(str.length() - 1,
          str.length()).toString());// 可能抛出字符串越界异常
       } else {
        inputText.setText("0.");
        str.setLength(0);
       }
      }
      y = Double.parseDouble(inputText.getText().trim());
     } else// 其他的数字键
     {
      inputText.setText(str.append(e2.getActionCommand())
        .toString());
      y = Double.parseDouble(inputText.getText().trim());
     }
    }
   } catch (NumberFormatException e) {
    inputText.setText("数字格式异常");
   } catch (StringIndexOutOfBoundsException e) {
    inputText.setText("字符串索引越界");
   }
  }
 }

 public static void main(String args[]) {
  try {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InstantiationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (UnsupportedLookAndFeelException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  new Calc();
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值