⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculator.java

📁 我做的一个java计算器小程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	        }
	       else if(num==6)
	        {
	        	poly.add(ply1);
	        	poly.add(ply2);
	        	poly.add(ply3);
	        	poly.add(ply4);
	        	poly.add(ply5);
	        	poly.add(ply7);

	        }
	       else if(num==7)
	        {
	        	poly.add(ply1);
	        	poly.add(ply6);
	        	poly.add(ply5);

	        }
	       else if(num==8)
	        {
	        	poly.add(ply1);
	        	poly.add(ply2);
	        	poly.add(ply3);
	        	poly.add(ply4);
	        	poly.add(ply5);
	        	poly.add(ply6);
	        	poly.add(ply7);

	        }
	       else if(num==9)
	        {
	        	poly.add(ply1);
	        	poly.add(ply2);
	        	poly.add(ply4);
	        	poly.add(ply5);
	        	poly.add(ply6);
	        	poly.add(ply7);

	        }

	      else if(E==true)
	       {  
	           poly.add(ply1);
	           poly.add(ply2);
	           poly.add(ply3);
	           poly.add(ply4);
	           poly.add(ply7);
	       }
          return poly;

	}
  }


     private class InsertAction implements ActionListener
      {
		  public void actionPerformed(ActionEvent event)
		  {
		  	  if(start)//初始化
		  	  {
		  	  	count = 0;
		  	  	start = false;
		  	  	minus = true;
		  	  	state = false;
		  	    E = false;
		  	  	negative = false;
		  	  	elem = "";
		  	  }

			  String input = event.getActionCommand();
			  String s = ".";
			  elem = elem + input;

			  if (elem.charAt(0) == '.'&&minus)
			    {
			    	state = true;
			    	minus = false;
			    	display_num[0]=0;
			    	count = 1;
			    	p_point = 1;
			    	display.repaint();
			    }

			  if(elem.charAt(0) == '-' && elem.charAt(1) == '.'&&minus)
			  {
			    	state = true;
			    	minus = false;
			    	display_num[0]=0;
			    	count = 1;
			    	p_point = 1;
			    	display.repaint();
			    }


			  if( s.equals(input) )
			  {
			  	state = true;
			  	p_point = count;
			  	display.repaint();
			  }
			  else
			  {
			    int a1 = Integer.parseInt(input);
			    display_num[count] = a1;
			    count++;
			    display.repaint();
			  }
		  }
	  }

	 private class CommandAction implements ActionListener
     {
        public void actionPerformed(ActionEvent evt)
       {

          String command = evt.getActionCommand();

           if (start)
           {
              if (command.equals("-"))//负号
             {
                 elem = "" + command;
                 count = 0;
                 start = false;
                 negative = true;
                 display.repaint();
            }
             else
                lastCommand = command;
         }
           else
           {
              calculate(Double.parseDouble(elem));
              if (command.equals("="))
              {
                elem = String.valueOf(result);

                Show_result(elem);
              }
              lastCommand = command;
              if(command != "=")
              start = true;
           }
        }
     }
     
      class clear_listener implements ActionListener//清空监听
     {
     	public void actionPerformed(ActionEvent event)//全初始化
     	 {
     	 	count=0;

	        	elem = "";
     			E = false;
     		    display_num[0]=0;
     		    start = true;
                state = false;
                negative = false;
                display.repaint();
     	 }
     }

     private class FunctionAction implements ActionListener
     {
     	public void actionPerformed(ActionEvent evt)
     	{
     		String function = evt.getActionCommand();//函数方法
     	    double Fun= Double.parseDouble(elem);
     	    if(function.equals("sinx"))  Fun =Math.sin(Fun);
     	    if(function.equals("cosx"))  Fun = Math.cos(Fun);
     	    if(function.equals("1/x"))   Fun = 1/Fun;
     	    elem = String.valueOf(Fun);
     	  	Show_result(elem);
     	}
     }


     public void calculate(double x)
       {
        if (lastCommand.equals("+")) result += x;//运算方法
        if (lastCommand.equals("-")) result -= x;
        if (lastCommand.equals("x")) result *= x;
        if (lastCommand.equals("/"))
         if(x==0)//除数为,
            E=true;
         else
            result /= x;
        if (lastCommand.equals("=")) result = x;
       }

     public void Show_result(String	Sresult)//显示结果
     {
     	int j=0;
     	int temp = -2;
        for(int i=0;i<Sresult.length()&&i<11;i++)
                  {

                  	char ch = Sresult.charAt(i);
                   	String chr = "" + ch;
                   	if(chr.equals("-"))//结果为负,数字显示少一位
                   	{
                   		negative = true;
                   		temp = Sresult.length()-1;
                   		continue;
                   	}

                   	if(chr.equals("."))//
                      	{
          	        	  state = true;
          	        	  if(negative)  p_point = i;
          	        	  else p_point = i;
          	        	  count = Sresult.length()-1;
          	        	  continue;
          	             }

                   	display_num[j] = Integer.parseInt(chr);
                   	j++;
                  }
                 // if(temp!=-2)
                   // count = temp - 1;
                  display.repaint();

     }

     
     
     public void paint_negative(Graphics2D g3)//画"-"
     {
     	    int y = 20;
		 	int[] a = new int []{15,20,60,65,60,20};
		 	int[] b =new int[] {y+44,y+39,y+39,y+44,y+49,y+49};
		 	Polygon poly=new Polygon(a,b,6);
		 	g3.fill(poly);

		 	if(state == true)
		     {
		        for(int i=0;i<p_point&&i<10;i++)
	              {
                    Seven_Seg number=new Seven_Seg(display_num[i],i+1);
                    ArrayList arry=number.show();
                    for(int x=0;x<arry.size();x++)
                     {
                      g3.fill((Polygon)arry.get(x));
                     }
	 	           }

	 	       Rectangle2D rect = new Rectangle2D.Double(90+90*(p_point-1),105,8,8);
	 	       g3.fill(rect);

	 	       for(int i=p_point;i<count&&i<10;i++)
	              {
                    Seven_Seg number=new Seven_Seg(display_num[i],i+1);
                    ArrayList arry=number.show();
                    for(int x=0;x<arry.size();x++)
                     {
                     g3.fill((Polygon)arry.get(x));
                     }
	 	           }
	     	  }

	    	else
	    	{
	        	for(int i=0;i<count&&i<10;i++)
	            {
                  Seven_Seg number=new Seven_Seg(display_num[i],i+1);
                  ArrayList arry=number.show();
                  for(int x=0;x<arry.size();x++)
                      {
                        g3.fill((Polygon)arry.get(x));
                      }
	 	         }
	    	 }
     }

     public void paint_state(Graphics2D g3)//画小数点
     {
     			 	for(int i=0;i<p_point&&i<10;i++)//小数点前
	          {
              Seven_Seg number=new Seven_Seg(display_num[i],i);
               ArrayList arry=number.show();
               for(int x=0;x<arry.size();x++)
                  {
                     g3.fill((Polygon)arry.get(x));
                  }
	 	      }

	 	    Rectangle2D rect = new Rectangle2D.Double(90+90*(p_point-1),105,8,8);//小数点
	 	     g3.fill(rect);

	 	      for(int i=p_point;i<count&&i<10;i++)
	          {
               Seven_Seg number=new Seven_Seg(display_num[i],i);//小数点后
               ArrayList arry=number.show();
               for(int x=0;x<arry.size();x++)
                  {
                     g3.fill((Polygon)arry.get(x));
                  }
	 	      }
     }

    public void paint_E(Graphics2D g3)//画"E"
     {
 
    	g3.setColor(Color.red);
		Seven_Seg number=new Seven_Seg(111,1);
        ArrayList arry=number.show();

        for(int x=0;x<arry.size();x++)
             {
               g3.fill((Polygon)arry.get(x));
             }

     }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -