[Java] 计算器 →→→→→进入此内容的聊天室

来自 , 2020-11-19, 写在 Java, 查看 116 次.
URL http://www.code666.cn/view/19b65066
  1. /**
  2.  * 计算器
  3.  * @author
  4.  *
  5.  */
  6. public class Calculator extends JFrame implements ActionListener{
  7.      
  8.     //定义创建组件
  9.     JTextField t=new JTextField(20);
  10.     JButton[] b=new JButton[27];
  11.     String[] s={"Backspace","CE","C",
  12.                 "MC","7","8","9","/","sqrt",
  13.                 "MR","4","5","6","*","%",
  14.                 "MS","1","2","3","-","1/x",
  15.                 "M+","0","+/-",".","+","="
  16.                 };
  17.     JMenuBar jmb = new JMenuBar();
  18.     JMenu jm1 = new JMenu("编辑(E)");
  19.     JMenu jm2 = new JMenu("查看(V)");
  20.     JMenu jm3 = new JMenu("帮助(H)");
  21.     JMenuItem jmi1 = new JMenuItem("复制(C)");
  22.     JMenuItem jmi2 = new JMenuItem("粘贴(P)");
  23.     public Calculator() {
  24.          
  25.         this.setLayout(null);
  26.         this.add(t);
  27.         t.setHorizontalAlignment(JTextField.RIGHT);
  28.         t.setEditable(false);
  29.         t.setBounds(10,10,400,30);
  30.          
  31.         //添加按钮,同时注册监听
  32.         for(int i=0;i<27;i++){
  33.             b[i]=new JButton(s[i]);
  34.             b[i].addActionListener(this);//注册监听
  35.             b[i].setActionCommand(s[i]);
  36.             this.add(b[i]);
  37.         }
  38.          
  39.         //下面设置各个按钮之间的距离以及按钮的大小
  40.         //前三个按钮设置为大小100,30,间距为10
  41.         for(int i=0;i<3;i++){
  42.             b[i].setBounds(90+110*i,50,100,30);
  43.         }
  44.          
  45.         //剩余的按钮大小相同,只是间距有所不同
  46.         int n=3;
  47.         for(int i=0;i<4;i++){
  48.             for(int j=0;j<6;j++){
  49.                 if((n-3)%6==0){
  50.                     b[n].setBounds(10,90+35*i,60,30);
  51.                 }else{
  52.                     b[n].setBounds(90+65*(j-1),90+35*i,60,30);
  53.                 }
  54.                 n++;
  55.             }
  56.         }
  57.          
  58.       //设置菜单
  59.         this.setJMenuBar(jmb);
  60.         jmb.add(jm1);
  61.         jmb.add(jm2);
  62.         jmb.add(jm3);
  63.         jmi1.setMnemonic('C');
  64.         jmi1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
  65.         jmi2.setMnemonic('V');
  66.         jmi2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
  67.         jm1.add(jmi1);
  68.         jm1.add(jmi2);
  69.         jmi1.addActionListener(this);//注册菜单监听
  70.         jmi2.addActionListener(this);
  71.         jmi1.setActionCommand("copy");
  72.         jmi2.setActionCommand("paste");
  73.       //设置窗体
  74.         this.setTitle("计算器");
  75.         ImageIcon icon = new ImageIcon("images/jisuan.jpg");
  76.         this.setIconImage(icon.getImage());
  77.         this.setSize(430,290);
  78.         this.setLocation(200,200);
  79.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  80.         this.setResizable(false);
  81.         this.setVisible(true);
  82.     }
  83.      
  84.     public static void main(String[] args) {
  85.         new Calculator();
  86.     }
  87.  
  88.     String text[] = {"",""} ;
  89.     String showString = "";//要显示的字符串
  90.     int dotCount = 0;//点击小数点的次数
  91.     int j = 0;//用来表示是第几个操作数
  92.     int op = -1; //操作类型,0:/;1:*;2:-;3:+;4:%
  93.     int flag = 0;
  94.     String copyedString = "";
  95.     @Override
  96.     public void actionPerformed(ActionEvent e) {
  97.         String comand = e.getActionCommand();
  98.         if("copy".equals(comand)){
  99.             copyedString =  t.getText();
  100.         }
  101.         if("paste".equals(comand)){
  102.             if(!("".equals(copyedString))){
  103.                 showString = copyedString;
  104.                 text[j] = copyedString;
  105.             }
  106.         }
  107.         for(int i = 4; i<7; i++){ //点击7、8、9
  108.             if(s[i].equals(comand)) text[j] += s[i];
  109.             showString = text[j];
  110.         }
  111.         for(int i = 10; i<13; i++){//点击4、5、6
  112.             if(s[i].equals(comand)) text[j] += s[i];
  113.             showString = text[j];
  114.         }
  115.         for(int i = 16; i<19; i++){//点击1、2、3
  116.             if(s[i].equals(comand)) text[j] += s[i];
  117.             showString = text[j];
  118.         }
  119.         if(s[22].equals(comand)) {//点击0
  120.             text[j] += s[22];
  121.             showString = text[j];
  122.         }
  123.         if(s[24].equals(comand)){ //点击".",只允许点一次,点第二次不给反应
  124.             if(dotCount == 0) {
  125.                 text[j] += s[24];
  126.                 showString = text[j];
  127.                 dotCount ++;
  128.             }
  129.         }
  130.         if(s[7].equals(comand)){//点击"/"
  131.                 showString = operator();
  132.                 text[0] = showString;
  133.                 text[1] = "";
  134.                 op = 0;
  135.                 j = 1;//下面要换成text[1]来记录第二个操作数
  136.         }
  137.         if(s[13].equals(comand)){//点击"*",只能点一次
  138.                 showString = operator();
  139.                 text[0] = showString;
  140.                 text[1] = "";
  141.                 op = 1;
  142.                 j = 1;//下面要换成text[1]来记录第二个操作数
  143.         }
  144.         if(s[19].equals(comand)){//点击"-",只能点一次
  145.                 showString = operator();
  146.                 text[0] = showString;
  147.                 text[1] = "";
  148.                 op = 2;
  149.                 j = 1;//下面要换成text[1]来记录第二个操作数
  150.         }
  151.         if(s[25].equals(comand)){//点击"+",只能点一次
  152.                 showString = operator();
  153.                 text[0] = showString;
  154.                 text[1] = "";
  155.                 op = 3;
  156.                 j = 1;//下面要换成text[1]来记录第二个操作数
  157.         }
  158.         if(s[14].equals(comand)){//点击%
  159.             if(j==0) {
  160.                 op = 4;
  161.                 j = 1;
  162.             }
  163.         }
  164.         if(s[0].equals(comand)){//点击Backspace
  165.             int len = text[j].length();
  166.             if(len > 0){
  167.                 text[j] = text[j].substring(0,text[j].length()-1);
  168.                 showString = text[j];
  169.             }
  170.         }
  171.         if(s[2].equals(comand)){//点击c
  172.                 text[j] = "";
  173.                 showString = "";
  174.         }
  175.         if(s[23].equals(comand)){//点击+/-
  176.             if(flag == 0){
  177.                 text[j] = "-"+text[j];
  178.                 showString = text[j];
  179.                 flag = 1;
  180.             }else if(flag==1){
  181.                 text[j] = showString.substring(1,showString.length());
  182.                 showString = text[j];
  183.                 flag = 0;
  184.             }
  185.             System.out.println("flag===="+flag);
  186.         }
  187.         if(s[26].equals(comand)){//点击=
  188.             showString = operator();
  189.         }
  190.         if(s[1].equals(comand)){//点击CE
  191.             //此处直接将所有的数据全部恢复初始值,同时将文本框中的数值清空
  192.             text[0] = "";
  193.             text[1] = "";
  194.             t.setText("");
  195.             showString = "";
  196.             dotCount = 0;
  197.             op = -1;
  198.             j = 0;
  199.         }
  200.         if(s[20].equals(comand)){//点击1/x
  201.             if(!("".equals(text[j]))){
  202.                 double num = Double.parseDouble(text[j]);
  203.                 num = 1/num;
  204.                 text[j] = num+"";
  205.                 showString = num+"";
  206.             }
  207.         }
  208.         if(s[8].equals(comand)){//点击sqrt
  209.             if(!("".equals(text[j]))){
  210.                 double num = Double.parseDouble(text[j]);
  211.                 num = Math.sqrt(num);
  212.                 text[j] = dealResult(num+"");
  213.                 showString = text[j];
  214.             }
  215.         }
  216.  
  217.         t.setText(showString);
  218.     }
  219.      
  220.     public String operator(){//进行计算
  221.         if("".equals(text[1]) || "".equals(text[0])){
  222.             return text[0];
  223.         }
  224.         double op1 = Double.parseDouble(text[0]);//得到第一个操作数
  225.         double op2 = Double.parseDouble(text[1]);//得到第二个操作数
  226.         double result = 0.0;
  227.         //进行计算
  228.         switch (op) {
  229.         case 0://除
  230.             if(op2 != 0){//除法时第二操作数不能是0
  231.                 result = op1 / op2;
  232.             }
  233.             break;
  234.         case 1://乘
  235.             result = op1*op2;
  236.             break;
  237.         case 2://减
  238.             result = op1-op2;
  239.             break;
  240.         case 3://加
  241.             result = op1 + op2;
  242.             break;
  243.         case 4:
  244.             int mu1 = (int)op1;
  245.             int mu2 = (int)op2;
  246.             if((op1 == mu1) && (op1 == mu2)){//整除需要两个操作数都是整数
  247.                 result = (int)op1%(int)op2;
  248.                 System.out.println("zhen");
  249.             }else{
  250.                 result = op2;
  251.             }
  252.             break;
  253.         default:
  254.             break;
  255.         }
  256.         String resString = result+"";
  257.         resString = dealResult(resString);
  258.         return resString;
  259.     }
  260.      
  261.     String dealResult(String resString){
  262.         int dotpos = resString.indexOf(".");
  263.         if(resString.substring(dotpos+1, resString.length()).equals("0")){
  264.             resString = resString.substring(0,dotpos);
  265.         }
  266.         return resString;
  267.     }
  268. }
  269.  

回复 "计算器"

这儿你可以回复上面这条便签

captcha