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

📄 calculator.java

📁 模拟计算器java编程
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Calculator
{
   public static void main(String[] args)
   {  
      CalculatorFrame frame = new CalculatorFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.show();
   }
}

/**
   A frame with a calculator panel.
*/
class CalculatorFrame extends JFrame
{
   public CalculatorFrame()
   {
      super("计算器");
                                                                
      Container contentPane = getContentPane();
      setSize(400,250);
	  setLocation(350,200);
   
      CalculatorPanel panel = new CalculatorPanel();
      contentPane.add(panel);
	
   }
}

class CalculatorPanel extends JPanel
{  
   public CalculatorPanel()
   {  
      setLayout(new BorderLayout());//设定布局
      
	  init();	
      
	  //显示区域
      display = new JTextField("0.");
	  display.setHorizontalAlignment(JTextField.RIGHT );
      
	  display.setBackground(Color.white);
      display.setEditable(false);
      add(display,BorderLayout.CENTER);
      //菜单区域
	  JMenuBar menufield=new JMenuBar();
      menufield.setLayout(new FlowLayout());

      addmenu(menufield,"编辑(E)",KeyEvent.VK_E,"复制(C)",KeyEvent.VK_C,"粘帖(V)",KeyEvent.VK_V);      
	  addmenu(menufield,"查看(V)",KeyEvent.VK_V,"标准型(T)",KeyEvent.VK_T,"科学型(S)",KeyEvent.VK_S);
	  addmenu(menufield,"帮助(H)",KeyEvent.VK_H,"帮助主题(H)",KeyEvent.VK_H,"关于计算器(A)",KeyEvent.VK_A);		                                        
      add(menufield,BorderLayout.NORTH);
     //键盘区域
      ActionListener insert = new InsertAction();
      ActionListener command = new CommandAction();

      panel = new JPanel();
      panel.setLayout(new GridLayout(5, 6,6,5));

      dispM=new JTextField("");
	  dispM.setEditable(false);
      dispM.setBackground(Color.gray.brighter());
	  panel.add(dispM);
	
	  addButton("C", command);
      addButton("CE",command);
      addButton("<-",command);
	  addButton("=", command);
      addButton("pow",command);
      
      addButton("MC",command);
      addButton("7", insert);
      addButton("8", insert);
      addButton("9", insert);
      addButton("/", command);
      addButton("sqrt",command);

      addButton("MR",command);  
      addButton("4", insert);
      addButton("5", insert);
      addButton("6", insert);
      addButton("*", command);
      addButton("%",command);

      addButton("MS",command);
      addButton("1", insert);
      addButton("2", insert);
      addButton("3", insert);
      addButton("-", command);
      addButton("1/x",command);

      addButton("M+", command);
      addButton("0", insert);
      addButton("+/-", command);
      addButton(".", insert);
      addButton("+",command);
	  addButton("ran", command);
      add(panel,BorderLayout.SOUTH);
   }

   private void init()
   {
   	  result = 0;
      lastCommand = "=";
      start = true;
	  flag = true;
     tag=true;
   }

   private void addmenu(JMenuBar menufield,String label, int i,String label1,int lab1,String label2,int lab2)
	{  
	  MenuListener menuListener=new MenuListener();

      JMenu menu=new JMenu(label);
	  menu.setMnemonic(i);
      menufield.add(menu);
     
	  JMenuItem menuItem1=new JMenuItem(label1);
	  menuItem1.setMnemonic(lab1);
	  menuItem1.setAccelerator(KeyStroke.getKeyStroke(lab1,ActionEvent.CTRL_MASK));
	  menuItem1.setBackground(Color.white);
	  menuItem1.addActionListener(menuListener);
	  menu.add(menuItem1);
      menu.addSeparator();
	  JMenuItem menuItem2=new JMenuItem(label2);
	  menuItem2.setMnemonic(lab2);
	  menuItem2.setAccelerator(KeyStroke.getKeyStroke(lab2,ActionEvent.CTRL_MASK));
	  menuItem2.setBackground(Color.white);
	  menuItem2.addActionListener(menuListener);


	  menu.add(menuItem2);
   } 
    //监听菜单项的按下动作
  private class MenuListener implements ActionListener
	 {
	       JFrame d;
		  public void actionPerformed(ActionEvent e)
		   {
			  JMenuItem source=(JMenuItem)(e.getSource());
			  String comm=source.getActionCommand();
			  
			  if(comm.equals("复制(C)"))
				  copy=Double.parseDouble(display.getText());
			  else if(comm.equals("粘帖(V)"))
			      display.setText(""+copy);
			  else if(comm.equals("关于计算器(A)")|comm.equals("帮助主题(H)"))
			   {  
				  if(comm.equals("关于计算器(A)"))
				      d= new JFrame("关于 计算器");
                  else
					  d=new JFrame("帮助主题");
                  d.setSize(320,260);
                  d.setLocation(500,300);
				  d.setBackground(Color.white);
                  Container contentPane =new Container();				  
                  JPanel panel = new JPanel();
                  contentPane.add(panel);     			  				  
				  JLabel infor=new JLabel("  由计算机021陈雪姗制作  ");					  				 
				  panel.add(infor);
				  d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  d.setVisible(true);                   
			   }
		   }
	 }
  
   private void addButton(String label, ActionListener listener)
   {  
      JButton button = new JButton(label);
      button.addActionListener(listener);
	  button.setBackground(Color.gray.brighter());
      panel.add(button);
   }
  
   private class InsertAction implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         String input = event.getActionCommand();
         
        if(input.equals("0"))
		  {
			int tem=(int)(Double.parseDouble(display.getText()));
			if(tem==0&&flag) display.setText("0");
		  }
		 else if(input.equals("."))
		  {
            String tem=display.getText();
			if(flag) display.setText(tem+".");
			flag=false;
		  }
		else
		  {
			String tem=display.getText();
			if(tem.equals("0")) {init();display.setText(input);}
		  }

		if(!input.equals("."))
		  {
           if (start) 
           {
            display.setText("");
            start = false;
			flag=true;
           }
		   if(!display.getText().equals("0"))
                   display.setText(display.getText() + input);
		  }
      }
   }

   //监听按钮项的按下动作
   private class CommandAction implements ActionListener
   {
      public void actionPerformed(ActionEvent evt)
      {  
         String command = evt.getActionCommand();
		 if (command.equals("C"))
		 	{
		 		init();//之前的数被清空
	            display.setText("0");
	        }		  
		else if(command.equals("CE"))
		    {
			display.setText(" ");//之前的数还在result中
			start=true;
		    }
		else if(command.equals("<-"))
		  {
			String tem=display.getText();
			String t=tem.substring(0,tem.length()-1);
			display.setText(t);
		  }
		else if(command.equals("ran"))
		  {
			
			display.setText(""+Math.random());
		  }
		else if(command.equals("MS"))
		  {
			mem=Double.parseDouble(display.getText());
			dispM.setText("M");
			start=true;
		  }
		else if(command.equals("MR"))
		  {
			display.setText(""+mem);
			
		  }
		else if(command.equals("MC"))
		  {
			dispM.setText("");
			mem=0;
		  }
		else if(command.equals("M+"))
		  {
			dispM.setText("M");
			double p=Double.parseDouble(display.getText());
			mem+=p;
			display.setText(""+mem);
			start=true;
		  }
        else if (command.equals("+/-")) 
            { 
			   String tmp=display.getText();
			   char sign=tmp.charAt(0);
			   if (sign=='-')tag=false;
			   else tag=true;
               if(tag)
               {    
               		display.setText("-"+display.getText()); 
               		tag=false;
               	}
               	else
         		{
         			String t=tmp.substring(1,tmp.length());
         			display.setText(t);
         			tag=true;
         		}
            }
         else
         {  
            calculate(Double.parseDouble(display.getText()));//将运算符前的字符串转化成数字
            lastCommand = command;
            start = true;
         }
      }
   }

   public void calculate(double x)
   { 
      if (lastCommand.equals("+")) result += x;
      else if (lastCommand.equals("-")) result -= x;
      else if (lastCommand.equals("*")) result *= x;
      else if (lastCommand.equals("/")) result /= x;
	  else if (lastCommand.equals("%")) result =0.01*x;
	 
	  else if (lastCommand.equals("1/x")) result=1/x;
	  else if (lastCommand.equals("sqrt")) result=Math.sqrt(x);
	  else if (lastCommand.equals("pow")) result=x*x;
      else if (lastCommand.equals("=")) result = x;
	  
      display.setText("" + result);  	 
   }
   
   private JTextField display;
   private JTextField dispM;
   private JPanel panel;
   private double result;
   private boolean tag;
   private String lastCommand;
   private boolean start;
   private boolean flag;
   private double mem;
   private double copy;
}

/*使用存储的数
要存储显示的数据,请单击“MS”。 
要重新调用存储的数据,请单击“MR”。 
要清除内存,请单击“MC”。 
把所显示的数字与内存中的数字相加,请单击“M+”。要查看新数据,请单击“MR”。 
 注意

存储数据时,存储选项上方的框中会显示出“M”。存入其他数据时,存储器中的数据将被替换。 
*/

⌨️ 快捷键说明

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