www.pudn.com > calclulator.rar > calclulator.java, change:2006-09-16,size:13011b


/** 
*计算器的应用bete2版 
*/ 
import java.awt.*; 
import java.awt.event.*; 
class calclulator{ 
	public static void main(String args[]){ 
		proce pr=new proce(); 
	} 
} 
class proce extends Frame implements ActionListener 
{ 
	MenuBar menubar1; 
	Menu menufile; 
	Menu menuedit; 
	Menu menuhelp; 
	MenuItem menufileopen; 
	MenuItem menufilesave; 
	MenuItem menufileclose; 
	MenuItem menufileexit; 
	MenuItem menucopy; 
	MenuItem menupaste; 
	MenuItem menudelete; 
	MenuItem menuhelpabout; 
	MenuItem menuhelpa; 
	 
	Button button0=new Button("0"); 
	Button button1=new Button("1"); 
	Button button2=new Button("2"); 
	Button button3=new Button("3"); 
	Button button4=new Button("4"); 
	Button button5=new Button("5"); 
	Button button6=new Button("6"); 
	Button button7=new Button("7"); 
	Button button8=new Button("8"); 
	Button button9=new Button("9"); 
	Button button10=new Button("."); 
	Button button11=new Button("+"); 
	Button button12=new Button("—"); 
	Button button13=new Button("X"); 
	Button button14=new Button("/"); 
	Button button15=new Button("乘方"); 
	Button button16=new Button("开方"); 
	Button button17=new Button("="); 
	Button button18=new Button("C"); 
	TextField result=new TextField("",80); 
	 
	 
	Panel pan1=new Panel(); 
	Panel pan2=new Panel(); 
	Panel pan3=new Panel(); 
	Panel pan4=new Panel(); 
	Panel pan5=new Panel();	 
	Panel pan0=new Panel(); 
	Panel pan6=new Panel(); 
	Panel manpan=new Panel(); 
	Panel manpan0=new Panel(); 
	Label lab=new Label(); 
	int oper=0,peroper=0,newoper=0;//记录运算符 
	double num1=0.0,num2=0.0;			//记录数字 
	boolean flag=false,flag0=false,hasoper=false; 
	boolean flagnum1=true,flagnum2=false; 
	String num="",numble=""; 
	String str=""; 
	//以上为公共变量;方法 
	final int AND=11, 
	SUB=12, 
	MULT=13, 
	DIV=14, 
	POW=15, 
	SQRT=16, 
	EQUAL=17, 
	INS=18;//小数点 
	 
	public proce() 
	{ 
		super("计算器 Bete2版"); 
		this.setLayout(new FlowLayout()); 
		setBackground(Color.PINK); 
		result.setEditable(false); 
		button0.setBackground(Color.green); 
		button17.setBackground(Color.orange); 
		button17.setFont(new Font("Courier",Font.PLAIN,16)); 
		button11.setFont(new Font("Courier",Font.PLAIN,16)); 
		button10.setFont(new Font("Courier",Font.PLAIN,19)); 
		button18.setFont(new Font("Courier",Font.PLAIN,16)); 
		pan0.setLayout(new GridLayout(7,1,3,1)); 
		result.setBackground(Color.lightGray);  
		pan1.setLayout(new GridLayout(1,4,5,1)); 
		pan2.setLayout(new GridLayout(1,4,5,1)); 
		pan3.setLayout(new GridLayout(1,4,5,1)); 
		pan4.setLayout(new GridLayout(1,4,5,1)); 
		pan5.setLayout(new GridLayout(1,4,5,1)); 
		//manpan0.setLayout(new GridLayout(2,1)); 
		//manpan.setLayout(new FlowLayout()); 
		menubar1=new MenuBar(); 
		 
		menufile=new Menu("文件"); 
		menuedit=new Menu("编辑"); 
		menuhelp=new Menu("帮助"); 
		menufileopen=new MenuItem("打开"); 
		menufilesave=new MenuItem("保存"); 
		menufileclose=new MenuItem("关闭"); 
		menufileexit=new MenuItem("退出"); 
		menupaste=new MenuItem("粘贴"); 
		menudelete=new MenuItem("删除"); 
		menucopy=new MenuItem("复制"); 
		menuhelpabout=new MenuItem("使用帮助"); 
		menuhelpa=new MenuItem("关于"); 
		//贴加监听; 
		handle ha=new handle(this); 
		menufileopen.addActionListener(this); 
		menufilesave.addActionListener(this); 
		menufileclose.addActionListener(this); 
		menufileexit.addActionListener(this); 
		menupaste.addActionListener(this); 
		menudelete.addActionListener(this); 
		menucopy.addActionListener(this); 
		menuhelpabout.addActionListener(ha); 
		menuhelpa.addActionListener(ha); 
		 
		//添加到菜单上; 
		menufile.add(menufileopen); 
		menufile.add(menufilesave); 
		menufile.add(menufileclose); 
		menufile.add(menufileexit); 
		menuedit.add(menupaste); 
		menuedit.add(menudelete); 
		menuedit.add(menucopy); 
		menuhelp.add(menuhelpabout); 
		menuhelp.add(menuhelpa); 
		//把菜单添加到菜单栏上; 
		menubar1.add(menufile); 
		menubar1.add(menuedit); 
		menubar1.add(menuhelp); 
		//把菜单栏设置于frame上; 
		this.setMenuBar(menubar1); 
		 
		 
		 
		button0.addActionListener(this); 
		button1.addActionListener(this); 
		button2.addActionListener(this); 
		button3.addActionListener(this); 
		button4.addActionListener(this); 
		button5.addActionListener(this); 
		button6.addActionListener(this); 
		button7.addActionListener(this); 
		button8.addActionListener(this); 
		button9.addActionListener(this); 
		button10.addActionListener(this); 
		button11.addActionListener(this); 
		button12.addActionListener(this); 
		button13.addActionListener(this); 
		button14.addActionListener(this); 
		button15.addActionListener(this); 
		button16.addActionListener(this); 
		button17.addActionListener(this); 
		button18.addActionListener(this); 
		pan0.add(result); 
		pan0.add(lab); 
		pan1.add(button0); 
		pan1.add(button1); 
		pan1.add(button2); 
		pan1.add(button3); 
		pan0.add(pan1); 
		 
		pan2.add(button4); 
		pan2.add(button5); 
		pan2.add(button6); 
		pan2.add(button7); 
		pan0.add(pan2); 
		 
		pan3.add(button8); 
		pan3.add(button9); 
		pan3.add(button10); 
		pan3.add(button11); 
		pan0.add(pan3); 
		 
		pan4.add(button12); 
		pan4.add(button13); 
		pan4.add(button14); 
		pan4.add(button15); 
		pan0.add(pan4); 
		 
		pan5.add(button16); 
		pan5.add(button17); 
		pan5.add(button18); 
		 
		pan0.add(pan5); 
		//pan6.add(result); 
		//manpan.add(pan6); 
		//manpan0.add(manpan,0); 
		//manpan0.add(pan0,1); 
		this.add(pan0); 
		 
		 
		addWindowListener(new WindowAdapter(){ 
			public void windowClosing(WindowEvent e) 
			{ 
				System.exit(0); 
			} 
		}); 
		setSize(600,290); 
		//this.pack(); 
		//this.show(); 
		setVisible(true); 
	} 
   public void actionPerformed(ActionEvent e) 
   {	/*假如信息不是来自于计算符号;则对显示框清屏;*/ 
   	if(e.getSource()!=button11&&e.getSource()!=button12&&e.getSource()!=button13&&e.getSource()!=button14&&e.getSource()!=button15 
   	&&e.getSource()!=button16&&e.getSource()!=button17&&e.getSource()!=menufileexit&&e.getSource()!=button18){													//清屏完毕; 
   		if(e.getSource()==button0){ 
   			initstar(); 
   			numble="0"; 
   			flag=false; 
   			peroperate(numble);                       //peroperate()被调用; 
   		} 
   		else if(e.getSource()==button1){ 
   			initstar(); 
   			numble="1"; 
   			flag=false; 
   			peroperate(numble); 
   		} 
   		else if(e.getSource()==button2){ 
   			initstar(); 
   			numble="2"; 
   			flag=false; 
   			peroperate(numble); 
   		} 
   		else if(e.getSource()==button3){ 
   			initstar(); 
   			numble="3"; 
   			flag=false; 
   			peroperate(numble); 
   		} 
   		else if(e.getSource()==button4){ 
   			initstar(); 
   			numble="4"; 
   			flag=false; 
   			peroperate(numble); 
   		} 
   		else if(e.getSource()==button5){ 
   			initstar(); 
   			numble="5"; 
   			flag=false; 
   			peroperate(numble); 
   		} 
   		else if(e.getSource()==button6){ 
   			initstar(); 
   			numble="6"; 
   			flag=false; 
   			peroperate(numble); 
   		} 
   		else if(e.getSource()==button7){ 
   			initstar(); 
   			numble="7"; 
   			flag=false; 
   			peroperate(numble); 
   		} 
   		else if(e.getSource()==button8){ 
   			initstar(); 
   			numble="8"; 
   			flag=false; 
   			peroperate(numble); 
   		} 
   		else if(e.getSource()==button9){ 
   			initstar(); 
   			numble="9"; 
   			flag=false; 
   			peroperate(numble); 
   		} 
   		else if(e.getSource()==button10){ 
   			initstar(); 
   			numble="."; 
   			flag=false; 
   			peroperate(numble); 
   		} 
   	} 
   						 
   	else if(e.getSource()==button11){ 
   		oper=AND; 
   		flag=true; 
   		 
   		answerto(AND);                                //answerto()被调用; 
   		peroper=oper; 
   		showoper("+"); 
   	} 
   	else if(e.getSource()==button12){ 
   		oper=SUB; 
   		flag=true; 
   		 
   		showoper("-"); 
   		answerto(SUB); 
   		peroper=oper; 
   		 
   	} 
   	else if(e.getSource()==button13){ 
   		oper=MULT; 
   		flag=true; 
   		showoper("*"); 
   		answerto(MULT); 
   		peroper=oper; 
   		 
   	} 
   	else if(e.getSource()==button14){ 
   		oper=DIV; 
   		flag=true; 
   		showoper("/"); 
   		answerto(DIV); 
   		peroper=oper; 
   		 
   	} 
   	else if(e.getSource()==button15){ 
   		oper=POW; 
   		flag=true;  
   		showoper("POW"); 
   		answerto(POW); 
   		peroper=oper; 
   		 
   	} 
   	else if(e.getSource()==button16){ 
   		oper=SQRT;//System.out.println("LLfL"); 
   		flag=true; 
   		showoper("SQRT"); 
   		answerto(SQRT); 
   		peroper=oper; 
   		 
   	} 
   	else if(e.getSource()==button17){ 
   		oper=EQUAL;//System.out.println("LLL"); 
   		showoper("="); 
   		answerto(EQUAL); 
   		peroper=oper; 
   	} 
   	else if(e.getSource()==menufileexit){ 
   	System.exit(0); 
    } 
    else if(e.getSource()==button18){ 
    	initstar(); 
    	show(""); 
    }   	 
  } 
   	 
  public void answerto(int oper)               //calclutor()被调用    showoper()被调用 
  { 
  	flag=true;flagnum1=false; 
  	flagnum2=true; 
   	if(oper	== EQUAL){ 
   		String ans=""; 
   		ans=Double.toString(calclutor(peroper,num1,num2)); 
   		showoper(ans);//System.out.println(ans); 
   		num1=num2=0.0;   //执行完一次计算加显示之后清空num1,num2; 
   		flag0=true;//已按下"="; 
   		hasoper=true; 
   		 
   	} 
   	else{ 
   		peroper=oper; 
   		flag0=false;//没有按下"=" 
   	} 
	} 
  public double calclutor(int peroper,double num1,double num2) 
  {//oper为计算的代码;num1,num2为输入的数字; 
   	double answer=0.0; 
   	switch(peroper) 
   	{ 
   		case AND://"+" 
   		answer=num1+num2;//System.out.println(num1); 
   		break; 
   		case SUB://"-" 
   		answer=num1-num2; 
   		break; 
   		case MULT://"*" 
   		answer=num1*num2; 
   		break; 
   		case DIV://"/" 
   		answer=num1/num2; 
   		break; 
   		case POW://"乘方" 
   		answer=Math.pow(num1,num2); 
   		break; 
   		case SQRT://"开方" 
   		answer=Math.sqrt(num1); 
   		break; 
   		default: 
   		break; 
   	} 
   	//计算之后清空  num1 , num2;  
   	return (answer); 
  } 
    
    
    
  public void show(String str)//显示区用textField:1+3=4;(result); 
  { 
  	result.setFont(new Font("Courier",Font.PLAIN,16)); 
   	result.setText(str); 
  } 
    
  void showoper(String operstr)//显示区参数传入方法;             show();被调用 
  {    //传入的都为string格式; 
  	 
  	if(flag){ 
  		num=""; 
  		flag=false; 
  	} 
  	 
    if(flag&&peroper==EQUAL)//第二次计算之前清空显示区; 
   	{ 
   		str=""; 
   		show(str); 
   	} 
   	else 
   	{ 
  	str=str.concat(operstr); 
   	show(str);//同类中的方法可以被随便调用; 
    } 
  } 
   
  public void peroperate(String numble){		//对数字与小数点的先前处理;numble+point 
   	num=num.concat(numble);			//System.out.println("first  ::the num:"+num+"ok");	                                                             //数字合成完成; 
   	showoper(numble); 
   	if(flagnum1){         // System.out.println("the num:"+num+"ok"); 
   		char[] ch=new char[1]; 
   		num.getChars(0,1,ch,0); 
   		String sr=new String(ch); 
   		if(sr.equals(".")){ 
   			num="0"+num;//System.out.println(num); 
   		   
   		}num1=Double.valueOf(num).doubleValue(); 
   		 //System.out.println("num1");  		 
   	} 
   	else if(flagnum2){ 
   		char[] ch=new char[1]; 
   		num.getChars(0,1,ch,0); 
   		String sr=new String(ch); 
   		if(sr.equals(".")){ 
   			num="0"+num;//System.out.println(num); 
   		} 
   		num2=Double.valueOf(num).doubleValue(); 
   		 
   	}	 
   	numble="";																							//数字处理完成已生成:  num1  和  num2; 
  } 
  void initstar() 
  { 
  	if(hasoper) 
  	{ 
      oper=0;peroper=0;newoper=0; 
    	num1=0.0;num2=0.0;			 
    	flag=false;flag0=false; 
    	num="";numble=""; 
    	str="";num1=num2=0.0; 
    	hasoper=false;//System.out.println("the second"); 
    	show(str);flagnum1=true;flagnum2=false; 
    } 
  } 
} 
   
   	 
class handle implements ActionListener   //监听类        //用来实例化主程序 
{ 
	proce pf; 
	public handle(proce pf) 
	{ 
		this.pf=pf; 
		 
	} 
	public void actionPerformed(ActionEvent e) 
	{ 
		 
		  dial da=new dial(pf,"关于");da.setVisible(true); 
		  //da.show(); 
	 
	 } 
	//pf.repaint(); 
} 
 
 
 
 
 
 
 
 
 
 
 
class dial extends Dialog implements ActionListener 
{ 
	String choice0=""; 
	dial(Frame parent,String title) 
	{ 
		super(parent,title,false); 
		setLayout(new FlowLayout(FlowLayout.RIGHT,10,10)); 
		setSize(300,200); 
		choice0=title; 
		 
		 
		Button b; 
		add(b=new Button("确定")); 
		//b.setBounds(20,20,100,100); 
		b.addActionListener(this); 
		 
		this.addWindowListener(new WindowAdapter() 
		{ 
			public void windowClosing(WindowEvent e) 
			{ 
				dispose(); 
			} 
		} 
		); 
	} 
	public void actionPerformed(ActionEvent e) 
	{ 
		dispose(); 
	} 
	public void paint(Graphics g) 
	{ 
		if(choice0.equals("关于")) 
		{ 
			g.drawString("计算器 Bete2 版 " ,40,90); 
		  g.drawString(" 由 Leniux 制 作…… \n\n  ",50,110); 
		  g.drawString("   …… 版 权 所 有",100,130); 
	  } 
	  else if(choice0.equals("帮助")) 
	  { 
	    g.drawString("   无帮助",20,140); 
	  } 
	} 
}