Easy Calculator Code

package jisuanqi;

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

public class jisuanqiC extends JFrame implements ActionListener {
    
    
	 
	 JLabel jlabResult =new JLabel("计算结果:",JLabel.LEFT);  //设置标签并左对齐
	 JTextArea f1,f2;  //声明文本区
	 JButton d1,d2,d3,d4,d5;
	 JButton dsin,dcos,dtan,dcot;
	 
	 JButton b[]; //声明按钮的一维数组
	 boolean flag=false;

	 public jisuanqiC(){
    
    
		
		 JPanel p1=new JPanel(); //创建面板
		 JPanel p2=new JPanel();
		 JPanel p3=new JPanel();
		 JPanel p4=new JPanel();
		 
		 
		 b=new JButton[16]; 
		 String str="123+456-789x0.=/";
		 for(int i=0;i<b.length;i++)   //编写按钮名称  
		 {
    
    
		    b[i]=new JButton(str.substring(i,i+1));//获取介于i-i+1之间的子字符串
		 }
		 
		 
		 f1=new JTextArea(1,20);  //设置文本区大小
		 f2=new JTextArea(9,240);
		 d1=new JButton("清零");    //创建按钮名称
		 d2=new JButton("平方根");
		 d3=new JButton("立方根");
		 d4=new JButton("平方");
		 d5=new JButton("立方");
		 dsin=new JButton("sin");
		 dcos=new JButton("cos");
		 dtan=new JButton("tan");
		 dcot=new JButton("cot");
		 
								 setLayout(new BorderLayout()); //容器布局
								 add(p1,BorderLayout.NORTH);   //面板1添加在容器的北区域
								 add(p2, BorderLayout.CENTER); //面板2添加在容器的中间区域
								 add(p3, BorderLayout.SOUTH);  //面板3添加在容器的南区域
								
		 
					//面板p1添加组件			 
					 p1.setLayout(new FlowLayout()); //面板1流式布局
					 p1.add(jlabResult);//把标签加入到面板p1中
					 p1.add(f1);//显示输入 输出 结果
					 p1.add(d1);//清零
					 d1.addActionListener(this);//注册一个监视器
		 
					 
					 //面板p2添加组件
					 p2.setLayout(new GridLayout(4,4)); //面板2网格布局
					
					 for(int i=0;i<b.length;i++) 
					 {
    
    
						 p2.add(b[i]);
						 b[i].addActionListener(this);  //注册监听器
					 }   
					 
					 
					 //p3面板添加组件
					 p3.setLayout(new GridLayout(2,4));  //面板3网格布局
					 p3.add(d2);//平方根
					 p3.add(d3);
					 p3.add(d4);
					 p3.add(d5);
					 p3.add(dsin);
					 p3.add(dcos);
					 p3.add(dtan);
					 p3.add(dcot);
					 //p3.add(new JScrollPane(f2));
					 d2.addActionListener(this);  //注册监听器
					 d3.addActionListener(this);
					 d4.addActionListener(this);
					 d5.addActionListener(this);
					 dsin.addActionListener(this);
					 dcos.addActionListener(this);
					 dtan.addActionListener(this);
					 dcot.addActionListener(this);
					 
					 
					 
					 setSize(450,250 );  //设置窗口大小
					 setVisible(true);  //设置窗口可见 
		
	 }
	 

	 //重写ActionListener接口中的方法
public void actionPerformed(ActionEvent e) 
	 {
    
    
		 if(e.getSource()==d1)        //清零
			 f1.setText(null);
		 
		 else if(e.getSource()==d2)   //求平方根
		   {
    
    
			 String s=f1.getText().trim();  //在获得的文本中除去空格
			 double d=Math.sqrt(Double.parseDouble(s));
			 f1.setText(String.valueOf(d));
			 f2.setText(String.valueOf(d));
		   }
		 
		 else if(e.getSource()==d3)   //求立方根
		   {
    
    
			 String s=f1.getText().trim();
			 double d=Math.pow(Double.parseDouble(s),1.0/3.0);
			 f1.setText(String.valueOf(d));
			 f2.setText(String.valueOf(d));
		   }
		 
		 else if(e.getSource()==d4)   //求平方
		   {
    
    
			 String s=f1.getText().trim();
			 double d=Math.pow(Double.parseDouble(s),2.0);
			 f1.setText(String.valueOf(d));
			 f2.setText(String.valueOf(d));
		   }
		 
		 else if(e.getSource()==d5)   //求立方
		   {
    
    
			 String s=f1.getText().trim();
			 double d=Math.pow(Double.parseDouble(s),3.0);
			 f1.setText(String.valueOf(d));
			 f2.setText(String.valueOf(d));
		   }
		 else if(e.getSource()==dsin)   //求正弦
		   {
    
    
			 String s=f1.getText().trim();
			 double radians=Math.toRadians(Double.parseDouble(s));
			 double d=Math.sin(radians);
			 f1.setText(String.valueOf(String.format("%.2f", d))); //保留两位小数v
		   }
		 
		 else if(e.getSource()==dcos)   //求余弦
		   {
    
    
			 String s=f1.getText().trim();
			 double radians=Math.toRadians(Double.parseDouble(s));
			 double d=Math.cos(radians);
			 f1.setText(String.valueOf(String.format("%.2f", d))); //保留两位小数
		   }
		 
		 else if(e.getSource()==dtan)   //求正切
		   {
    
    
			 String s=f1.getText().trim();
			 double radians=Math.toRadians(Double.parseDouble(s));
			 double d=Math.tan(radians);
			 f1.setText(String.valueOf(String.format("%.2f", d))); //保留两位小数
		   }
		 
		 else if(e.getSource()==dcot)   //求余切
		   {
    
    
			 String s=f1.getText().trim();
			 double radians=Math.toRadians(Double.parseDouble(s));
			 double d=1.0/Math.tan(radians);
			 f1.setText(String.valueOf(String.format("%.2f", d))); //保留两位小数
		   }
		 
		 
         else if(e.getSource()==b[14])  //判断是否为‘=’运算符
		 {
    
    
			 String s=f1.getText().trim();
			 String []rs;
			 if(s.contains("+"))
			 {
    
    
				 rs=s.split("\\+");  //用‘+’分开文本框的字符串  进行加法运算
				 double d=Double.parseDouble(rs[0])+Double.parseDouble(rs[1]);
				 f1.setText(String.valueOf(d));
				 f2.setText(s+"="+String.valueOf(d));
			 }
			 
		 else if(s.contains("x"))
			 {
    
    
				 rs=s.split("x");
				 double d=Double.parseDouble(rs[0])*Double.parseDouble(rs[1]);
				 f1.setText(String.valueOf(d));
				 f2.setText(s+"="+String.valueOf(d));
			 }
			 
			 
			 else if(s.contains("-"))
			 {
    
    
				 rs=s.split("-");
				 double d=Double.parseDouble(rs[0])-Double.parseDouble(rs[1]);
				 f1.setText(String.valueOf(d));
				 f2.setText(s+"="+String.valueOf(d));
			 }
			 
			 
			 else if(s.contains("/"))
			 {
    
    
				 rs=s.split("/");
				 double d=Double.parseDouble(rs[0])/Double.parseDouble(rs[1]);
				 f1.setText(String.valueOf(d));
				 f2.setText(s+"="+String.valueOf(d));
			 }
			 flag=true;
		 }
		 
		 
		    
			 else 
			    {
    
     
			        if(flag)//为假时
			        {
    
     
			           f1.setText(""); 
			        } // 不点击=和清零时将字符串追加在文本域中  
			        f1.append(e.getActionCommand());  
			        flag=false; 
			    }  
			}  
				 	 
	 
	 public static void main(String[] args)
	 {
    
    
		 jisuanqiC sc=new jisuanqiC();  
		 sc.setTitle("多功能计算器");  //设置窗口标题
		 sc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //结束窗口所在的应用程序
		 
	 }
}


insert image description here

Guess you like

Origin blog.csdn.net/weixin_44659084/article/details/109842161