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

📄 awtcalcapp.java

📁 java经典10例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    }
	}

}

class ButtonHandler implements ActionListener {
	private CalcDisplay display;	//holds instance of calculator's display
	private String lastOp;			//holds the last operator that was pressed
	private String strVal;			//holds the string value of the number 
	private double total;			//accumulator
	private double number;			//used to store new number's
	//flag used to determine if op is pressed for the first time
	private boolean firsttime;		
	//flag used to determine if an operator has been pressed
	private boolean operatorPressed; 
	
	ButtonHandler( CalcDisplay d ){
	   display = d;	
	   firsttime = true;
	   strVal = "";
	}
	
	// this method is called automatically when a button is pressed
	public void actionPerformed( ActionEvent e ){
		//store the instance of the button that was pressed
		ButtonComponent button = (ButtonComponent) e.getSource();
		
		//get the button's label
		String s = button.getLabel().trim();
		
		//determine if the button was a number or operator button
		if ( Character.isDigit( s.charAt(0) ) )		
			handleNumber( s );					
		else
		    calculate( s );	    	   		
	}
	
	//determine's whether the operator is unary or binary
	//and does calculation's according to operator type
	void calculate( String op ){
	  	operatorPressed = true;
	  	//if it's the firsttime the button has been pressed after:
	  	//
	  	//		- the program first starts
	  	//		- the equal has been pressed
	  	//		- or the calculator has been cleared
	  	//
	  	// set the first number displayed on the display to total
	  
		if( firsttime && !Operator.isUnary( op ) ){
	  		total = getNumberOnDisplay();  	
	  		firsttime = false;
	   	}    
	    if ( Operator.isUnary( op )){   	
	    	handleUnaryOp( op );        
	    }else if ( lastOp != null ){		
			handleBinaryOp( lastOp );		
		}
	   	//store the calculator's last op -- important for binary operators
	   	if ( !Operator.isUnary( op ) )
	         lastOp = op;         
	}	
	//this method handles unary operators
	void handleUnaryOp( String op ){
		if ( op.equals( Operator.NEGATE ) ){  
	         //negate the number on the display screen 	 
	         number = negate( getNumberOnDisplay()+"");    	    	    
	         display.append(  number + "" );
	         return;        	
	    }else if ( op.equals( Operator.DOT ) ){
	       	 handleDecPoint();
	       	 return;
	    }else if ( op.equals( Operator.SQRT ) ){  
	         //calculate the square root of the number on the display    	 			     
		     number = Math.sqrt( getNumberOnDisplay() );
		     display.append( number+"" );
		     return;	     	   
	    }else if ( op.equals( Operator.EQUALS ) ){
	       	 //if a binary operator was pressed before the equals
	       	 //complete the operation first
	       	 if ( lastOp != null && !Operator.isUnary( lastOp ) ) 
	       	      handleBinaryOp( lastOp );
	       	 lastOp = null;
	         firsttime = true;
	         return;
	    }else 
	       clear();   
	}
	
	// handles operators that require two operands
	void handleBinaryOp( String op ){
		if ( op.equals( Operator.ADD ) ) 
			 total += number;	
		else if ( op.equals( Operator.SUBTRACT ) )
			 total -= number;
		else if ( op.equals( Operator.MULTIPLY ) )
			 total *= number;
		else if ( op.equals( Operator.DIVIDE ) )
			 total /= number;
		else if ( op.equals( Operator.POW ) )
		     total = Math.pow( total, number );
						   
	    display.append( total+"" );		    	
	}
	
	//This method is called each time a number is pressed.
	//A string object is used to concatenate each number pressed in succession
	//which then is converted into a double data type.
	void handleNumber( String s ){
		//concatenate to strVal if an operator was pressed before the current
		//button
		if ( !operatorPressed )
			strVal+=s;
		//if an operator was pressed, clear strVal and store the first number
		//pressed
		else{
			operatorPressed = false;
			strVal = s;		
		}
		//convert strVal to double 
		number = new Double( strVal ).doubleValue();	
		display.append( strVal );
	}
	//this method is called when the decimal point button has been pressed
	void handleDecPoint(){
		operatorPressed = false;
		//put a decimal point at the end of strVal only if there is no
		//decimal point already
		if ( strVal.indexOf( "." ) < 0 ) {
		     strVal+=Operator.DOT;	
		}       
		display.append( strVal );
	}
	//Method used to negate a value
	double negate( String s ){
		operatorPressed = false;
		//if number is a whole number, get rid of the '0' at the end of the
		//number to allow more number's to be added to the right of the
		//decimal point.
		if ( number == ( int ) number )
		     s = s.substring( 0, s.indexOf( "." ) );
		//add negative sign to the number if it doesn't exist	
		if ( s.indexOf( "-" ) < 0 )
		     strVal = "-"+s;
		//If a negative sign exist's, remove it
		else
		     strVal = s.substring( 1 );	  
		return new Double( strVal ).doubleValue();  
	}
	
	//get's the number that is currently on the display screen and
	//convert it to double
	double getNumberOnDisplay() {
	   	return new Double( display.getText() ).doubleValue();	
	}
	
	//clear's screen and reset's all variables
	void clear(){
		firsttime = true;
		lastOp = null;
		strVal = "";
		total = 0;
		number = 0;
		display.clear();
		display.append( "0" );
	}
}

class CalcDisplay extends Canvas {
   	private String text;
   	private Rectangle area;
   	private Border3D border;
   
	CalcDisplay( int w, int h){
		setSize( w , h  );
		//Determine the center of the component and create Rectangle object
		//which will be used to draw the white portion and the borders of the
		//display
		int x = ( size().width - w ) / 2;
		int y = ( size().height - h ) / 2 + 6;		
		border = new Border3D( this );	
		area = new Rectangle( x, y, w, h );	  				
	   	setFont( new Font( "monospace", Font.PLAIN, 14 ) ); 	
	   	setForeground( Color.black );
	   	text = "0";  		   
	}
	
	public Dimension getPreferredSize(){
	    return new Dimension( size().width, size().height );	
	}
	
	//set's text ( numbers ) to the display
	void append( String s ){
		//clear screen first before updating text
		clear();
		text = s;	
		repaint();
	}
	
	//clear's screen
	void clear(){
	    text = ""; 
	    repaint();	
	}
	
	//get's the text on the display
	String getText() { return text; }
	
	//paints all contents of the component
	public void paint( Graphics g ){
	    drawDisplayScreen( g );
	    drawOutput( g );				
	}
	
	//draw's the display
	protected void drawDisplayScreen( Graphics g ){
		g.setColor( Color.white );
		g.fillRect( area.x, area.y, area.width, area.height );
		border.draw3DBorder( g, area.x, area.y, area.width, area.height );
	}
	
	//draw's the text ( numbers ) on the screen
	protected void drawOutput( Graphics g ){
		g.setColor( Color.black );		
		g.drawString( text, area.x+3, area.y+15 );
	}

}

class Operator {
	final static String ADD = "+",
	    				SUBTRACT = "-",
	    				MULTIPLY = "x",
	    				DIVIDE = "/",
	    				POW = "pow",
	    				SQRT = "sqrt",
	    				CLEAR = "C",
	    				EQUALS = "=",
	    				NEGATE = "+/-",
	    				DOT = ".";
	
	//prevent's class from being instantiated  				    	
	private Operator() {}
	//determine's if the operator is unary ( and operator that need's only one
	//operand ).	
	static boolean isUnary( String s ) {
		return    s.equals( EQUALS )
		       || s.equals( CLEAR )
		       || s.equals( SQRT ) 
		       || s.equals( NEGATE )
		       || s.equals( DOT );	       		
	}
}

class Panel3D extends Panel {	                
	private Border3D border;//used for drawing the border
	//create a full border by default
	public Panel3D() { this( Border3D.FULL_BORDER ); }
	public Panel3D( int type ){
  		border = new Border3D( this, type );
  	}
  	public void paint( Graphics g ){
		border.draw3DBorder( g );		
	}
}

⌨️ 快捷键说明

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