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

📄 infojpanel.java

📁 java语言写的简单计算器,原创,有加减乘除 退格,清屏功能
💻 JAVA
字号:
package my_calculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;

public class InfoJPanel extends JFrame implements ActionListener{
    
    private boolean flag=true;
    private int i=0;
    //定义文本框用来显示数据
	private JTextField JTF_result=new JTextField(24);
	//下拉表列表用来定义运算符
	String functions[]={"加","减","乘","除"};
	JComboBox JCBfun=new JComboBox(functions);
	//控制按钮
    private JButton on_off=new JButton("");  
   	private JButton Backspace=new JButton("退格");
	private JButton clean=new JButton("清屏");
	private JButton reset=new JButton("复位");  
	//数字按钮
    private JButton one=new JButton("1");
    private JButton two=new JButton("2");
    private JButton three=new JButton("3");
    private JButton four=new JButton("4");
    private JButton five=new JButton("5");
    private JButton six=new JButton("6");
    private JButton seven=new JButton("7");
    private JButton eight=new JButton("8");
    private JButton night=new JButton("9");
    private JButton zero=new JButton("0");
    private JButton point=new JButton(".");
    //运算按钮
    private JButton add=new JButton("+");
    private JButton sub=new JButton("-");
    private JButton mul=new JButton("*");
    private JButton div=new JButton("/");
    private JButton equ1=new JButton("=");
    private JButton equ2=new JButton("等于");
    //符号按钮
    private JButton pos_or_minus=new JButton("+/-");
    private JButton sqrt=new JButton("sqrt"); 
    private JButton cos=new JButton("cos");
    private JButton sin=new JButton("sin"); 	
	//声明面板
	private JPanel JPTextArea=new JPanel();
	private JPanel JPButton_control=new JPanel();
	private JPanel JPButton_data=new JPanel();
	//构造方法
	public InfoJPanel(JFrame mainframe){
		Container contentPane=mainframe.getContentPane();          
		//建立放置文本框的面板
        JPTextArea.setLayout(new FlowLayout());
        JPTextArea.add(JTF_result);
        JTF_result.setBackground(Color.lightGray.brighter().brighter());
        JTF_result.setForeground(Color.blue);  //字体颜色
        JTF_result.setFont(new Font("楷体",Font.LAYOUT_LEFT_TO_RIGHT,16)); //字体设置
        JTF_result.setVisible(true);
		//建立放置功能按钮的面板
		JPButton_control.setLayout(new GridLayout(1,4,8,8));
	    JPButton_control.add(on_off);
		JPButton_control.add(Backspace);
		JPButton_control.add(clean);
		JPButton_control.add(reset);
        
        //为按钮添加图标
        on_off.setIcon(new ImageIcon("images\\top_exit.gif"));
        
		//建立放置数字和符号按钮的面板
		JPButton_data.setLayout(new GridLayout(4,5,8,4));	
        JPButton_data.add(seven);
        JPButton_data.add(eight);
        JPButton_data.add(night);
        JPButton_data.add(div);
        JPButton_data.add(sqrt);
        JPButton_data.add(four);
        JPButton_data.add(five);
        JPButton_data.add(six);
        JPButton_data.add(mul);
        JPButton_data.add(cos);
        JPButton_data.add(one);
        JPButton_data.add(two);
        JPButton_data.add(three);
        JPButton_data.add(sub);
        JPButton_data.add(sin);
        JPButton_data.add(zero);
        JPButton_data.add(pos_or_minus);
        JPButton_data.add(point);
        JPButton_data.add(add);
        JPButton_data.add(equ1);

    	//把面板放置到框架中
     	contentPane.setLayout(new FlowLayout());
		contentPane.add(JPTextArea);
		contentPane.add(JPButton_control);
		contentPane.add(JPButton_data);
		
		//设置字体颜色
         on_off.setForeground(Color.red);
         Backspace.setForeground(Color.red);
         clean.setForeground(Color.red);
         reset.setForeground(Color.red);
         add.setForeground(Color.red);
         sub.setForeground(Color.red);
         mul.setForeground(Color.red);
         div.setForeground(Color.red);
         equ1.setForeground(Color.red);
         one.setForeground(Color.blue);
         two.setForeground(Color.blue);
         three.setForeground(Color.blue);
         four.setForeground(Color.blue);
         five.setForeground(Color.blue);
         six.setForeground(Color.blue);
         seven.setForeground(Color.blue);
         eight.setForeground(Color.blue);
         night.setForeground(Color.blue);
         zero.setForeground(Color.blue);
         sqrt.setForeground(Color.blue);
         cos.setForeground(Color.blue);
         sin.setForeground(Color.blue);
         point.setForeground(Color.blue);
         pos_or_minus.setForeground(Color.blue);

         mainframe.setVisible(true);
          //设置监听器
          on_off.addActionListener(this);
          Backspace.addActionListener(this);
          clean.addActionListener(this);
          reset.addActionListener(this);
          one.addActionListener(this);
          two.addActionListener(this);
          three.addActionListener(this);
          four.addActionListener(this);
          five.addActionListener(this);
          six.addActionListener(this);
          seven.addActionListener(this);
          eight.addActionListener(this);
          night.addActionListener(this);
          zero.addActionListener(this);
          point.addActionListener(this);
          add.addActionListener(this);
          sub.addActionListener(this);
          mul.addActionListener(this);
          div.addActionListener(this);
          equ1.addActionListener(this);
          equ2.addActionListener(this);
          pos_or_minus.addActionListener(this);
          sqrt.addActionListener(this);
          cos.addActionListener(this);
          sin.addActionListener(this);
          
      	}
	//按钮事件处理方法
	public void actionPerformed(ActionEvent e){
		String Command=e.getActionCommand();
		  //关闭按钮
        if(e.getSource()==on_off){
        	System.exit(0);
        }

        if(e.getSource()==one){
        	   clean_screen();    //判别是否要清屏
        	   Append("1");      	   
        }	
        if(e.getSource()==two){
        	clean_screen();    //判别是否要清屏
        	Append("2");
        }		
        if(e.getSource()==three){
        	clean_screen();    //判别是否要清屏
        	Append("3");
        }		
        if(e.getSource()==four){
        	clean_screen();    //判别是否要清屏
        	Append("4");
        }		
        if(e.getSource()==five){
        	clean_screen();    //判别是否要清屏
        	Append("5");
        }		
        if(e.getSource()==six){
        	clean_screen();    //判别是否要清屏
        	Append("6");
        }		
        if(e.getSource()==seven){
        	clean_screen();    //判别是否要清屏
        	Append("7");
        }		
        if(e.getSource()==eight){
        	clean_screen();    //判别是否要清屏
        	Append("8");
        }		
        if(e.getSource()==night){
        	clean_screen();    //判别是否要清屏
        	Append("9");
        }		
        if(e.getSource()==zero){
        	clean_screen();    //判别是否要清屏
            Append("0");
        }	
        if(Command.equals(".")){
             clean_screen();    //判别是否要清屏
             Append(".");
        }	
        if(Command.equals("+")){
        	clean_screen();    //判别是否要清屏
        	Append("+");
             String s1=JTF_result.getText();
             
        }
        if(Command.equals("-")){
        	 clean_screen();    //判别是否要清屏
             Append("-");
        }
        if(Command.equals("*")){
        	 clean_screen();    //判别是否要清屏
             Append("*");
        }
        if(Command.equals("/")){
        	 clean_screen();    //判别是否要清屏
             Append("/");
        }
        if(Command.equals("=")){
             clean_screen();    //判别是否要清屏
             Append("=");
             String s=JTF_result.getText();
             Cal(s);       
             flag=true;    
        }	
        
        if(Command.equals("+/-")){
        	clean_screen();
        	String s1=JTF_result.getText();
        	String s2=null;
        	String s3=null;
          	if(i%2==0){
        		s2="-".concat(s1);JTF_result.setText(s2);
        	}
            
            else{
              	s3=s1.substring(1,JTF_result.getText().length());
        	    JTF_result.setText(s3);
            }
            ++i;  
        }
        
         if(Command.equals("sqrt")){
         	
         	 clean_screen();    //判别是否要清屏
             Append("sqrt");
         }
         
         if(Command.equals("cos")){
         	
         	 clean_screen();    //判别是否要清屏
             Append("cos");
         }
         
         if(Command.equals("sin")){
         	
         	 clean_screen();    //判别是否要清屏
             Append("sin");
         }
         //退格按钮
        if(Command=="退格"){
        	int len=JTF_result.getText().length();
        	if(len>=1){
        	
        	String s=JTF_result.getText().substring(0,len-1);
         	JTF_result.setText(s);
         	}
         	
         }
		  //清空按钮
        if(Command=="清屏"){
         	JTF_result.setText(null);
         }
		  //复位按钮
         if(Command=="复位"){
         	JTF_result.setText(null);
         }            
       }
 
       //判别是否要清屏
       void clean_screen(){
       	   if(flag){
       	   	  System.out.println("要清屏了!!");
       	   	  JTF_result.setText(null);
       	   }
       	      
       }

      //对表达式的处理
       void Cal(String s){
        	int m1=0;
        	int m2=0;
        	m2=s.indexOf("=");
           	double resu=0;
        	char ch=HaveFun(s);
        	String a=String.valueOf(ch);
        	Is_First_Fun(s);  //对运算符出现在首位的错误处理
        	
        	if(s.startsWith("sqrt",0)){
             	int len=JTF_result.getText().length();
        	    s=JTF_result.getText().substring(4,len-1);
                Double data=Double.parseDouble(s);
                System.out.println(s);
                resu=Math.sqrt(data);
                String result=Double.toString(resu);
        
        	 }
        	 
        	if(s.startsWith("cos",0)){
             	int len=JTF_result.getText().length();
        	    s=JTF_result.getText().substring(3,len-1);
                Double data=Double.parseDouble(s);
                resu=Math.cos(data*Math.PI/180);
                String result=Double.toString(resu);
           		 
        	}
        	
        	if(s.startsWith("sin",0)){
             	int len=JTF_result.getText().length();
        	    s=JTF_result.getText().substring(3,len-1);
                Double data=Double.parseDouble(s);
                resu=Math.sin(data*Math.PI/180);
                String result=Double.toString(resu);
                        		 
        	}
        	
        	if(a.equals("+")) 
        	{
        		System.out.println("我是加号2");
        	    m1=s.indexOf("+");
        	 	String	s1=s.substring(0,m1);
        	    String	s2=s.substring(m1+1,m2);  
        	   	double d1=Double.parseDouble(s1);
        	    double d2=Double.parseDouble(s2);
        	    resu=d1+d2;
           	 }
           if(a.equals("-")) 
        	  {
        		System.out.println("我是减号2");
        	    m1=s.indexOf("-");
        	  	String	s1=s.substring(0,m1);
        	    String	s2=s.substring(m1+1,m2);  
        	   	double d1=Double.parseDouble(s1);
            	double d2=Double.parseDouble(s2);
        	    resu=d1-d2;
        	 
        	   }
        	 
        	 
           if(a.equals("*")) 
        	  {
        		System.out.println("我是乘号2");
        	    m1=s.indexOf("*");
        	 	String	s1=s.substring(0,m1);
        	    String	s2=s.substring(m1+1,m2);  
        	   	double d1=Double.parseDouble(s1);
        	    double d2=Double.parseDouble(s2);
        	    resu=d1*d2;
        	 
        	  }
        	 
        	if(a.equals("/")) 
        	  {
        		System.out.println("我是除号2");
        	    m1=s.indexOf("/");
        	 	String	s1=s.substring(0,m1);
        	    String	s2=s.substring(m1+1,m2);  
        	   	double d1=Double.parseDouble(s1);
            	double d2=Double.parseDouble(s2);
                resu=d1/d2;
        	 
        	  }
        	  
                String r=Double.toString(resu);
          	    Append(r);
           }
           
        //对运算符出现在首位的错误处理
        void Is_First_Fun(String s){
       	    if((s.startsWith("/",0)||s.startsWith("*",0)||s.startsWith("=",0))){
       	    	JOptionPane.showMessageDialog(null,"表达式首位符号出错","非法输入",JOptionPane.ERROR_MESSAGE);
       	    	System.out.println("表达式首位不能是运算符");
       	    	flag=false;
       	    }
       	    
       	       
        }
        //在文本框添加数据
       void Append(String str){
               String str1=JTF_result.getText();
        	   String str2=str1+str;
               JTF_result.setText(str2);
        	   flag=false;
           }
        
       //判断含有哪个运算符号
       char HaveFun(String s){
              System.out.println(s);
         	  char temp=0;
         	  String ad="+";
         	  for(int i=0;i<s.length();i++){ 
         	     
                if(s.startsWith("+",i)) { 
                   if(i==0)  Append("Sorry,这个功能尚未实现!");
                   flag=false;
                   System.out.println("我是加号1");
                   return '+'; 
         	 	}
         	 	
       	        else  if(s.startsWith("-",i)) { 
       	           if(i==0)  Append("Sorry,这个功能尚未实现!");
       	           flag=false;
                   System.out.println("我是减号1");
                   return '-'; 
         	 	}
                else if(s.startsWith("*",i)) { 
                    System.out.println("我是乘号1");
                   return '*'; 
         	 	}
         	    else if(s.startsWith("/",i)) { 
                    System.out.println("我是除号1");
                   return '/'; 
         	 	}
         	 	else  if(s.startsWith(".",i)) { 
       	           if(i==0)  Append("Sorry,这个功能尚未实现!");
       	           flag=false;
                    System.out.println("我是点");
                   return '-'; 
         	 	}
         	  }
             return '#';
        
          }

}
























⌨️ 快捷键说明

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