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

📄 counter.java

📁 本系统可以实现简单数字运算功能
💻 JAVA
字号:
package counter;
import java.awt.*;
import java.awt.event.*;
public class Counter extends Frame implements ActionListener{
	//定义相关的类对象和变量 
	static Counter frm=new Counter();
	static Button[] btn=new Button[10]; 
	static Panel pn1=new Panel(new GridLayout(4,4,5,5));
	static TextField tx=new TextField("0");
	String str1="";      
    String str2="";
    String ope=null;    
    boolean first=true;    
    int count=0;     
    double result=0.0;     
    double num1=0.0,num2=0.0;
    boolean error=false;
    //主方法   
    public static void main(String args[]){
	frm.setLocation(300,200);
	frm.setLayout(null);
	tx.setBounds(30,40,230,30);
	pn1.setBounds(30,100,230,170);
	for(int i=0;i<=9;i++){
		btn[i]=new Button(Integer.toString(i));
		btn[i].addActionListener(frm);
		btn[i].setActionCommand("number");
		pn1.add(btn[i]);
			 
		}
	 
	Button ad=new Button("+");
	Button sub=new Button("-");
	Button mult=new Button("*");
	Button divi=new Button("/");
	Button equal=new Button("=");
	Button clear=new Button("清零");
	pn1.add(ad);
	pn1.add(sub);
	pn1.add(mult);
	pn1.add(divi);
	pn1.add(equal);
	pn1.add(clear);
	ad.setActionCommand("oper");
    sub.setActionCommand("oper");
    mult.setActionCommand("oper");
    divi.setActionCommand("oper");
    ad.addActionListener(frm);
    sub.addActionListener(frm);
    mult.addActionListener(frm);
	divi.addActionListener(frm);
	equal.addActionListener(frm);
	clear.addActionListener(frm);
	frm.addWindowListener(new window());//窗口监听器	
	frm.add(tx);
	frm.add(pn1);
	tx.setBackground(Color.green);
	
	frm.setSize(300,300);
	frm.setTitle("我的小计算器");
	frm.setBackground(Color.BLUE);
	frm.setVisible(true);
	}
	//按钮的事件处理函数
public void actionPerformed(ActionEvent e){
	 
	Button bn=(Button)e.getSource();
	if(e.getActionCommand().equals("number"))
		{
		   
		if(first){
			str1=str1+bn.getLabel();
		     tx.setText(str1);
		         }
		     
	    else{
	    	 
	    	Button bn3=bn;
	    	str2=str2+bn.getLabel();
		     tx.setText(str2);
		     
		   }
	    }
	else if(e.getActionCommand().equals("oper"))
	    {
        count++;
        ope=bn.getLabel();
        first=false;
        }
        else if(e.getActionCommand().equals("清零"))
        {
        	str1="";
        	str2="";
        	first=true;
        	tx.setText("0");
        }
	else if(e.getActionCommand().equals("="))
        {
        if((str1=="")||(str2=="")){
          count=0;
          first=true;
                }
        else
           {
           result();
           count=0;
           str2="";
           }
  }
	      
}
public void result(){
	num1=Double.parseDouble(str1);
    num2=Double.parseDouble(str2);
    if(ope.equals("+"))
   {
   result=num1+num2;
   }
  else if(ope.equals("-"))
  {
   result=num1-num2;
  }
  else if(ope.equals("*"))
  {
   result=num1*num2;
  }
  else if(ope.equals("/"))
  {
   if(num2==0.0)    //除数为0的处理方法
   {
    error=true;
   }
   else
   {
    result=num1/num2;
   }
  }
 
 tx.setText(String.valueOf(result));
 str1=String.valueOf(result);
}
//窗口正常退出类
static class window extends WindowAdapter{
	public void windowClosing(WindowEvent e){
		System.exit(0);
	}
}

}

⌨️ 快捷键说明

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