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

📄 counter.java

📁 这是我上学期收集的一些我们班高手的JAVA期末作业的源代码
💻 JAVA
字号:
 /*
 * 创建日期 2004-4-2 
 */
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class Counter extends Applet implements ActionListener 
{	
	TextField text1=new TextField(10);
	Button bt0,bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9;
	Button btMorP,btAdd,btSub,btMul,btDiv,btSqrt,btAnti,btPer;
	Button btBack,btC,btCE,btPoint,btEqual;
	int flag=0;
	String strShow1,strShow2=new String();
	double dbOper1=0.0,dbOper2=0.0,dbRes=0.0;
	boolean blEqu=false,blOper=false,blLen=true;

	public void init()
	{
		face();
	}
	
	public void face()
	{
		setBackground(new Color(102,102,204));
		Color cl1=new Color(255,0,0);
		Color cl2=new Color(0,0,255);
		Font ft=new Font("宋体",1,15);
			
		Insets bk=new Insets(5,5,5,5);
		GridBagLayout gBagLay=new GridBagLayout();
		GridBagConstraints gBagCon=new GridBagConstraints();
		setLayout(gBagLay);
		
		gBagCon.fill=GridBagConstraints.BOTH;
		gBagCon.anchor=GridBagConstraints.NORTH;				
		gBagCon.insets=bk;

		gBagCon.gridwidth=1;
		gBagCon.gridwidth=GridBagConstraints.REMAINDER;
		text1=new TextField(" 0");
		gBagLay.setConstraints(text1,gBagCon);
		add(text1);
		text1.setFont(new Font("宋体",1,15));
		text1.setEditable(false);
		text1.addActionListener(this);


		gBagCon.gridwidth=3;
		addButton(btBack,"BackSpace",gBagLay,gBagCon,cl1);
		
		addButton(btCE,"C",gBagLay,gBagCon,cl1);				
		gBagCon.gridwidth=GridBagConstraints.REMAINDER;					
		addButton(btC,"CE",gBagLay,gBagCon,cl1);

		gBagCon.gridwidth=1;
		addButton(bt7,"7",gBagLay,gBagCon,cl2);
		addButton(bt8,"8",gBagLay,gBagCon,cl2);
		addButton(bt9,"9",gBagLay,gBagCon,cl2);
		addButton(btDiv,"/",gBagLay,gBagCon,cl1);		
		gBagCon.gridwidth=GridBagConstraints.REMAINDER;					
		addButton(btSqrt,"Sqrt",gBagLay,gBagCon,cl1);		

		gBagCon.gridwidth=1;
		addButton(bt4,"4",gBagLay,gBagCon,cl2);
		addButton(bt5,"5",gBagLay,gBagCon,cl2);
		addButton(bt6,"6",gBagLay,gBagCon,cl2);
		addButton(btMul,"*",gBagLay,gBagCon,cl1);		
		gBagCon.gridwidth=GridBagConstraints.REMAINDER;					
		addButton(btPer,"Mod",gBagLay,gBagCon,cl1);	

		gBagCon.gridwidth=1;
		addButton(bt1,"1",gBagLay,gBagCon,cl2);
		addButton(bt2,"2",gBagLay,gBagCon,cl2);
		addButton(bt3,"3",gBagLay,gBagCon,cl2);
		addButton(btSub,"-",gBagLay,gBagCon,cl1);		
		gBagCon.gridwidth=GridBagConstraints.REMAINDER;					
		addButton(btAnti,"1/x",gBagLay,gBagCon,cl1);	

		gBagCon.gridwidth=1;
		addButton(bt0,"0",gBagLay,gBagCon,cl2);
		addButton(btMorP,"=",gBagLay,gBagCon,cl2);
		addButton(btPoint,".",gBagLay,gBagCon,cl2);
		addButton(btAdd,"+",gBagLay,gBagCon,cl1);		
		gBagCon.gridwidth=GridBagConstraints.REMAINDER;					
		addButton(btEqual,"+/-",gBagLay,gBagCon,cl1);
		
		
	}		
	
	private void addButton(Button bt,String name, GridBagLayout gridBag, GridBagConstraints gBagCon,Color color)
	{
		Font ft=new Font("宋体",1,15);	
		bt=new Button(name);
		gridBag.setConstraints(bt,gBagCon);
		add(bt);
		bt.addActionListener(this);
		bt.setForeground(color);
		bt.setFont(ft);
	}
		
	public void actionPerformed(ActionEvent e)
	{
		String temp=text1.getText();		
		int len=temp.length();
		
		if((e.getActionCommand()=="0"||e.getActionCommand()=="1"||e.getActionCommand()=="2"||
			e.getActionCommand()=="3"||e.getActionCommand()=="4"||e.getActionCommand()=="5"||
			e.getActionCommand()=="6"||e.getActionCommand()=="7"||e.getActionCommand()=="8"||
			e.getActionCommand()=="9")&&blLen)	
		{
			blOper=true;
			if(blEqu&&!text1.getText().equals(" 0"))
				{
					text1.setText(text1.getText()+e.getActionCommand());
					if(len>=20)
						blLen=false;
				} 
				
			else
			{
				text1.setText(" "+e.getActionCommand());
				blEqu=true;
			}
		}
		if(e.getActionCommand()=="C")
		{
			dbOper1=0.0;
			dbOper2=0.0;
			dbRes=0;
			text1.setText(" 0");
			blEqu=false;
			flag=0;
			blOper=false;
		} 
		if(e.getActionCommand()=="CE")
		{
			text1.setText(" 0");
			blEqu=false;
		} 
		
		if(e.getActionCommand()=="BackSpace")
		{			
			if(len==2||temp.charAt(len-1)=='。')
			{
				blEqu=false;
				text1.setText(" 0");				
			}				
			else
				text1.setText(temp.substring(0,len-1));	
		}	
		
		if(e.getActionCommand()=="+/-")
		{
			if(blOper)
			{			
			if(temp.charAt(0)=='-')
				text1.setText(text1.getText().replace('-',' '));
			else
				text1.setText(text1.getText().replace(' ','-'));
			}		
		}		
		
		if(e.getActionCommand()==".")
		{
			if(blEqu&&text1.getText().indexOf('.')==-1)							
				text1.setText(text1.getText().concat("."));		
			else
			{	
				text1.setText(" 0.");
				blEqu=true;
			}				
		}
				
		if((e.getActionCommand()=="+"||e.getActionCommand()=="-"||
			e.getActionCommand()=="*"||e.getActionCommand()=="/"||
			e.getActionCommand()=="Mod")&&temp.charAt(len-1)!='。')
		{
			blLen=true;
			if(flag!=0&&blOper)							
			{	
				dbOper2=Double.parseDouble(text1.getText());
				doEqu();
				flag=0;
				blOper=false;
			} 			
			else
			{
				dbOper1=Double.parseDouble(text1.getText());
				blOper=false;		
			}						
			blEqu=false;			
			if(e.getActionCommand()=="+")
				flag=1;
			if(e.getActionCommand()=="-")
				flag=2;
			if(e.getActionCommand()=="*")
				flag=3;
			if(e.getActionCommand()=="/")
				flag=4;	
			if(e.getActionCommand()=="Mod")
				flag=5;		
			
			
		}		
		
		if(e.getActionCommand()=="Sqrt"&&temp.charAt(len-1)!='。')
		{
			blLen=true;
			dbOper1=Double.parseDouble(text1.getText());
			
			if(dbOper1<0.0)
			{		
				text1.setText("被开方数不能小于零。");								
			}
			else
			{
				text1.setText(" "+String.valueOf(Math.sqrt(dbOper1)));
				dbRes=0; 
			}
				
			blEqu=false;			
		}
				
		
		if(e.getActionCommand()=="1/x"&&temp.charAt(len-1)!='。')
		{
			blLen=true;
			dbOper1=Double.parseDouble(text1.getText());
			if(dbOper1!=0.0)
				text1.setText(" "+String.valueOf(1/(dbOper1)));				
			else
			{	
				text1.setText(" 除数不能为零。");
				blEqu=false;
			}
		}		
		
		
		if(e.getActionCommand()=="=")
		{			
			blLen=true;
			if(blOper)
			{
				dbOper2=Double.parseDouble(text1.getText());
				blOper=false;
			}
				
			doEqu();
		}
	}
	
	public void doEqu()
	{		
		switch(flag)
		{
			case 1:
				dbRes=dbOper1+dbOper2;
				break;
			case 2:
				dbRes=dbOper1-dbOper2;
				break;
			case 3:
				dbRes=dbOper1*dbOper2;
				break;
			case 4:
				if(dbOper2==0) 
				{
					text1.setText("除数不能为零。");	
					blEqu=false;
				}											
				else
					dbRes=dbOper1/dbOper2;
				break;
			case 5:
				if(dbOper2==0) 
				{
					text1.setText("模数不能为零。");	
					blEqu=false;
				}											
				else
					dbRes=dbOper1%dbOper2;
				break;
		}
		if((flag!=4||flag!=5)&&dbOper2!=0)
		{
			if(dbRes<0)			
				text1.setText(String.valueOf(dbRes));
			else
				text1.setText(" "+String.valueOf(dbRes));
		}
		dbOper1=dbRes;
		blEqu=false;
	}
}

⌨️ 快捷键说明

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