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

📄 calculator.java

📁 java电脑 计算器
💻 JAVA
字号:
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.math.*;
public class Calculator extends Frame implements ActionListener
{
	MenuBar menubar;
	Menu menu1,menu2,menu3;  //编辑、查看和帮助
	JTextField text,text1;
	Panel pcentre,pwest;//pcentre面板pwest
	
    String number[]={"Backspace","CE","C","MC","7","8","9","/","sqrt","MR","4","5","6",
                       "*","%","MS","1","2","3","-","1/x","M+","0","+/-",".","+","="};
    boolean start=true,flag=true;
    int sw1=0,sw2=0,a=1,b,c;
    double x,result,ms;
    String command1,command2;//command1控制首先输入"0"的时候后面就不能再输入"0";comand2其限制一个数字中有且仅有一个小数点。
   
    Button button[]=new Button[27];
    public Calculator(String s)
    {
    	super(s);
        menu1=new Menu("编辑");
        menu1.add(new MenuItem("复制"));
        menu1.add(new MenuItem("科学型"));
        menu2=new Menu("查看");
        menu2.add(new MenuItem("标准型"));
        menu2.add(new MenuItem("科学型"));
        menu2.addSeparator();//分格
        menu2.add(new MenuItem("查看分组")); 
        menu3=new Menu("帮助");
        menu3.add(new MenuItem("帮助助主题")); 
        menu3.addSeparator(); 
        menu3.add(new MenuItem("关于计算器"));
        menubar=new MenuBar();
        menubar.add(menu1);
        menubar.add(menu2);
        menubar.add(menu3);
        setMenuBar(menubar);
        addWindowListener(new WindowAdapter()//窗口
        {
            public void windowClosing(WindowEvent e)
            {System.exit(0);
            }
        });
        text=new JTextField("0.");
        text.setHorizontalAlignment(JTextField.RIGHT );
        text.setBackground(Color.white);
        text.setEditable(false);
        add(text,BorderLayout.NORTH);
        
        pcentre=new Panel();
        FlowLayout flow=new FlowLayout();
		flow.setAlignment(FlowLayout.LEFT);
		flow.setHgap(2);
		flow.setVgap(4);
        pcentre.setLayout(flow);
        pwest=new Panel();
        pwest.setSize(90,31);
        text1=new JTextField(2);
        text1.setEditable(false);
        text1.setBackground(Color.gray);
        pwest.add(text1);
        pcentre.add(pwest);
        for(int i=0;i<=2;i++)
        {
        	button[i]=new Button(number[i]);
            button[i].setPreferredSize(new Dimension(69,31));
            button[i].setForeground(Color.RED); 
            button[i].addActionListener(this);
            pcentre.add(button[i]);
        }
        for(int i=3;i<=26;i++)
        {
        	button[i]=new Button(number[i]);
            button[i].setPreferredSize(new Dimension(40,30));
            if(i==3||i==7||i==9||i==13||i==15||i==19||i==21||i==25||i==26)
            	button[i].setForeground(Color.red);
            else
            	button[i].setForeground(Color.blue);
            button[i].addActionListener(this);
            pcentre.add(button[i]);	
        }
        add(pcentre,BorderLayout.CENTER);
        setBackground(Color.lightGray);
        setBounds(100,150,262,248);
        setVisible(true);
        validate();
        
    }
   ////////////////////////////// //布局完毕
    public void actionPerformed(ActionEvent e)
    {   
        if(e.getSource()==button[0])     //"Backspace"
    	{
    		String s=text.getText();
    		char[] s1=s.toCharArray();
    		if(s.length()>=3)
    		{
    			String s2=new String(s1,0,s.length()-1);
    			text.setText(s2);
    		}
    		else
    		{
    			text.setText("0.");
    			start=true;
    		}
    	}
    	else if(e.getSource()==button[1])//ce
    	{
    		text.setText("0.");
    		start=true;
    		flag=true;
    		result=x=0;
    		sw1=sw2=0;                   //sw1,sw2记录运算符号
    		command1=command2=null;
    		text.setForeground(Color.blue);
    		a=1;
    	}
    	else if(e.getSource()==button[2])//c
    	{
    		text.setText("0.");
    		start=true;
    		flag=true;
    		result=x=0;
    		sw1=sw2=0;
    		command1=command2=null;
    		text.setForeground(Color.blue);
    		a=1;
    	}
    	else if(e.getSource()==button[3])//MC
    	{
    		ms=0;
    		text.setText(""+ms);
    		text1.setText("");
    		text.setForeground(Color.red);
    		command1=command2=null;
    		start=true;
    	}
    	else if(e.getSource()==button[9])  //MR
    	{
    		text.setText(""+ms);
    		result=ms;                     //result就值赋予ms;ms起记忆作用
    		text.setForeground(Color.red);
    		command1=command2=null;
    		start=true;
    	}
    	else if(e.getSource()==button[15])//MS
    	{
    		ms=result;
    		text1.setText(" M");
    		text.setForeground(Color.red);
    		command1=command2=null;
    		start=true;
    	}
    	else if(e.getSource()==button[21])//M+
    	{
    		result+=ms;
    		text.setText(""+result);
    		text.setForeground(Color.red);
    		command1=command2=null;
    		start=true;
    	}
    	else if(e.getSource()==button[8])//sqrt
    	{
    		result=Math.sqrt(result);
    		text.setText(""+result);
    		text.setForeground(Color.red);
    	}
    	else if(e.getSource()==button[20])//1/X
    	{
    		result=1/result;
    		text.setText(""+result);
    		text.setForeground(Color.red);
    	}
    	else if(e.getSource()==button[14])//%
    	{
    		cal(5);
    	}
    	else if(e.getSource()==button[7])// /
    	{
    		cal(1);
    	}
    	else if(e.getSource()==button[13])//*
    	{
    		cal(2);
    	}
    	else if(e.getSource()==button[19])//-
    	{
    		cal(3);
    	}
    	else if(e.getSource()==button[25])//+
    	{
    	    cal(4);
    	}
    	else if(e.getSource()==button[23])//+/-
    	{
    		if(result!=0)
    		{
    			result=0-result;
    			text.setText(""+result);
    		}
    		
    	}
    	else if(e.getSource()==button[26])//=
    	{  
    		switch(sw1)
    		{
    			case 1:
		     		if(x!=0)
		     		{
		     			BigDecimal a1=new BigDecimal(Double.toString(result));
                        BigDecimal a2=new BigDecimal(Double.toString(x));
                        result=a1.divide(a2,16,BigDecimal.ROUND_HALF_EVEN).doubleValue();
		     		}
		     		else
                    {
                        text.setText("?0"); 
                        start=false; 
                        flag=false;
                        text.setForeground(Color.red); 
                        return; 
                    } 
                    break;
		     	case 2:
		     		BigDecimal b1=new BigDecimal(Double.toString(result));
                    BigDecimal b2=new BigDecimal(Double.toString(x));
                    result=b1.multiply(b2).doubleValue();
		     	    break;
		     	case 3:
		     		BigDecimal c1=new BigDecimal(Double.toString(result));
                    BigDecimal c2=new BigDecimal(Double.toString(x));
                    result=c1.subtract(c2).doubleValue();
                    break;
		     	case 4:
		     	    BigDecimal d1 = new BigDecimal(Double.toString(result));
                    BigDecimal d2 = new BigDecimal(Double.toString(x));
                    result=d1.add(d2).doubleValue();
                    break;
                case 5:
                	result=result%x;
                	break;
    		}
    		text.setText(""+result);
    		text.setForeground(Color.red);
    		command1=command2=null;
    		start=true;
    		sw2=0;
    		a=2;
    		b++;
    	}
    	else
    	{
    		String command=e.getActionCommand();
    		if(command!=command2)
    		{
    		if(command1=="0")
    		{
    		    command1=".";
    	        if(flag=true&&(command=="."))
    			text.setText(text.getText()+command);
    		}
    		else 
    		{
    			if(start&&flag)
    		    {
    		    command1=command;
    			text.setText("");
    			start=false;
    		    }
    		    if(flag)
    			text.setText(text.getText()+command);
    		
    		}
    		if(command==".")
    		command2=command;
    		}
    		if(a==1)
    			result=Double.parseDouble(text.getText());
    		else
    			x=Double.parseDouble(text.getText());
    		text.setForeground(Color.blue);
    		b++;		
    	}
    }
    public void cal(int i)
    {   if(c!=b)
    	{if(sw2==0)
    	{
    		sw2=sw1=i;
    		text.setText(""+result);
    		a=2;
    	}
    	else
    	{   
    		sw2=sw1;
    		sw1=i;
		     	switch(sw2)
		     	{
		     		case 1:
		     		if(x!=0)
		     		{
		     			BigDecimal a1=new BigDecimal(Double.toString(result));
                        BigDecimal a2=new BigDecimal(Double.toString(x));
                        result=a1.divide(a2,16,BigDecimal.ROUND_HALF_EVEN).doubleValue();
		     		}
		     		else
                    {
                        text.setText("?0"); 
                        start=false; 
                        flag=false;
                        text.setForeground(Color.red); 
                        return; 
                    } 
                    break;
		     	case 2:
		     		BigDecimal b1=new BigDecimal(Double.toString(result));
                    BigDecimal b2=new BigDecimal(Double.toString(x));
                    result=b1.multiply(b2).doubleValue();
		     	    break;
		     	case 3:
		     		BigDecimal c1=new BigDecimal(Double.toString(result));
                    BigDecimal c2=new BigDecimal(Double.toString(x));
                    result=c1.subtract(c2).doubleValue();
                    break;
		     	case 4:
		     	    BigDecimal d1 = new BigDecimal(Double.toString(result));
                    BigDecimal d2 = new BigDecimal(Double.toString(x));
                    result=d1.add(d2).doubleValue();
                    break;
                 case 5:
                 	result=result%x;
                 	break;
		     	}
		     	text.setText(""+result);  	
    	}
    	b++;
    	c=b;
    }
    text.setForeground(Color.red);
    command1=command2=null;
    start=true;  
    }




	public static void main(String args[])
	{
		Calculator myCalculator=new Calculator("计算器");
	}
}

⌨️ 快捷键说明

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