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

📄 mycalcapp.java

📁 java经典10例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.lang.*;
import java.util.*;

class MyCalcApp {	
	public static void main(String args[]){
		Frame fr = new Frame();
		fr.setTitle( "计算器程序" );
		fr.setSize( 220, 175 );
		fr.setResizable( false );
		fr.add(new AwtCalc(),BorderLayout.CENTER );
		fr.setVisible( true );
		fr.addWindowListener( new WindowAdapter() {
			public void windowClosing( WindowEvent e ) {
				System.exit( 1 );
   			}
   		});	
	}
}

class AwtCalc extends Panel{
    //数字面板上的Labels
    private String[] numPanelText = { 
    	" 1 ", " 2 ", " 3 "," 4 ", " 5 ", " 6 "," 7 ",  
    	" 8 "," 9 ",Operator.CLEAR  , " 0 ", Operator.DOT };
    //操作面板上的Labels								  				  
    private String[] operPanelText = {  
    	Operator.ADD, Operator.SUBTRACT,Operator.MULTIPLY, Operator.DIVIDE, 
    	Operator.POW, Operator.SQRT, Operator.NEGATE, Operator.EQUALS };	
    private Panel numButtonPanel;  	//放置数字按钮
    private Panel operButtonPanel;	//放置操作按钮
    private Panel3D displayPanel;	//放置显示计算结果的面板 
    private ButtonHandler handler;	//action listener for the buttons							 
    private CalcDisplay display;    //显示计算结果     
    private Font buttonfont;		  
    
	public AwtCalc(){ //Initialize   
    	buttonfont = new Font( "Courier", Font.PLAIN, 13 );
    	setLayout( new BorderLayout() );
    	setBackground( new Color( 212, 208, 200 ) );
    	Panel3D mainPanel = new Panel3D( Border3D.EXCLUDE_TOP_BORDER );
        numButtonPanel = new Panel( new GridLayout(4,3, 1, 1) );
    	operButtonPanel = new Panel( new GridLayout(4, 2, 1, 1) );
    	displayPanel = new Panel3D( Border3D.EXCLUDE_BOTTOM_BORDER );
    	display = new CalcDisplay( 192,26);
    	handler = new ButtonHandler( display );
    	displayPanel.add( display );   
    	mainPanel.add( createNumberPanel() );
    	mainPanel.add( createOperPanel() );
    	add( displayPanel, BorderLayout.NORTH );     
    	add( mainPanel, BorderLayout.CENTER );                     
	}
 
	//Description: 构建并返回数字面板
   	private Panel createNumberPanel(){
   		if ( display != null ) {
      		ButtonComponent btn = null;
      		for ( int i = 0; i < numPanelText.length; i++ ){
      			btn = new ButtonComponent( numPanelText[i] );   
      			btn.addActionListener( handler );
      			btn.setFont( buttonfont );
      			numButtonPanel.add( btn );
   			} 	   	
   		}
		return numButtonPanel;
	}
 
	//构建并返回操作面板   
	private Panel createOperPanel(){
    	ButtonComponent btn = null;
    	
    	for ( int i = 0; i < operPanelText.length; i++ ){
    		btn = new ButtonComponent( operPanelText[i] );
    		btn.setFont( buttonfont );   
    		btn.addActionListener( handler );  
    		operButtonPanel.add( btn );	
    	}
    	return operButtonPanel;
	}   
}

class Border3D {
	static final int EXCLUDE_TOP_BORDER = 1,//越过顶层边界       
    	EXCLUDE_BOTTOM_BORDER = 2,//越过底层边界
        FULL_BORDER = 3;//在边界范围内       
    private int type;//边界类型  
    private Component comp;//存储组件
	public Border3D( Component comp ) { this( comp, FULL_BORDER ); }
	public Border3D( Component comp, int type ){
		this.comp = comp;
		this.type = type;
	}
	
	//在组件组的边界绘制3D边界
	public void draw3DBorder( Graphics g ){
		draw3DBorder( g, comp.size() );	
	}
	public void draw3DBorder( Graphics g, Dimension area ) {
		draw3DBorder( g, 0, 0, area.width, area.height );
	}
	public void draw3DBorder( Graphics g, int x, int y, int width, int height ) {
		//绘制边界的顶层部分		                            
		if ( type == EXCLUDE_BOTTOM_BORDER || type == FULL_BORDER ){
        	g.setColor( comp.getBackground().darker() );
      		g.drawLine( x, y, width, y );	 
      		g.setColor( Color.white );
      		g.drawLine( x+1, y+1, width-1, y+1 );    		      	           	     
   		}
   		//绘制边界的底层部分     
   		if ( type == EXCLUDE_TOP_BORDER || type == FULL_BORDER ){            
   			g.setColor( comp.getBackground().brighter() );     	          	
      		g.drawLine( x, height, width, height);     	      	    	
      		g.setColor( comp.getBackground().darker() );          	    	
      		g.drawLine( x+1, height-1, width-1, height -1);
   		}
   
   		//绘制边界的顶层和底层部分   
   		g.setColor( comp.getBackground().brighter() );
   		g.drawLine( x+1, y+1, x+1, height - 1 );
   		g.drawLine( width, y, width, height );   
   		g.setColor( comp.getBackground().darker() );
   		g.drawLine( x, y, x, height ); 
   		g.drawLine( width-1, y+1, width-1, height - 1);     
	}	
}

class ButtonComponent extends Component {
    private String label;                 //the button's label
    private boolean pressed = false;	  //used to determine button presses
    private ActionListener action;		  
    private boolean mouseOver = false;    //used to determine mouse overs 
    private Border3D border;			  //to draw a 3D border around button

	public ButtonComponent(String label){
	    this.label = label;
	    border = new Border3D( this );    
	    enableEvents(AWTEvent.MOUSE_EVENT_MASK);
	}
    
	/**
	* Returns the preferred size of the button. This method is called automatically
	* when the component is painted.
	*
	*/
	public Dimension getPreferredSize() {
	    Font f = getFont();
	    if(f != null) {
	       FontMetrics fm = getFontMetrics(getFont());
	       return new Dimension(fm.stringWidth(label) + 10, fm.getHeight() + 5);
	    } 
	    else {
	       return new Dimension(25, 25);
	    }
	}
  
	/**
	* Returns the minimum size of the button. 
	*/
	public Dimension getMinimumSize() { return new Dimension(20, 20); }
	   
	/**
	* Detects mouse events on the component.
	*/
	public void processMouseEvent(MouseEvent e){  
	    switch(e.getID()) {
	    	case MouseEvent.MOUSE_PRESSED:
	        	//Create a new actionevent and pass to the action listener -
	        	//( the Button Handler )
	          	if ( action != null ) {            
	             	ActionEvent event = new ActionEvent( this, e.getID(), label );
	             	action.actionPerformed( event );
	             }
	        	pressed = true;                    	        
	        	//Invoke the repaint method which draws the button to appear pressed    
	        	repaint(); 
	        	break;
	      	case MouseEvent.MOUSE_RELEASED:
		        //When repaint is invoked, component color is returned to normal   
		        if(pressed == true) {
		           pressed = false;
		           repaint();
		         }
		         break;
	      	case MouseEvent.MOUSE_ENTERED:
		         //repaint method lighten's component's color to give a hover effect 
		         mouseOver = true;
		         repaint();
		         break;
	      	case MouseEvent.MOUSE_EXITED:	         
		         //cancel hover effect
		         mouseOver = false;
		         if(pressed == true) pressed = false;               
		            
		         repaint();
		         break;
		}
	    super.processMouseEvent(e);
	}
	//Add's action listener   
	public void addActionListener( ActionListener a ){
		action = a;    //this is the ButtonHandler
	}
	 
	//returns the buttons label  
	public String getLabel() { return label; }
	
	/*
	* repaints the background
	*/
	public void paint(Graphics g) {
	    int width = getSize().width - 1;
	    int height = getSize().height - 1;
	    //set background darker to give a pressed effect
	    if(pressed) {
	       	g.setColor(getBackground().darker().darker());
	    }else if(mouseOver){//set background lighter to give hover effect 
	    	g.setColor( getBackground().brighter() );
	    	//set background to normal    
	    }else{
	       	g.setColor(getBackground());
	    }
	    //fill background                          
	    g.fillRect(0, 0, width, height );  
	    //draws 3D border
	    border.draw3DBorder(g, 0, 0, width, height );
	    //center and draw the button's label         		    
	    Font f = getFont();
	    if(f != null) {
	    	FontMetrics fm = getFontMetrics(getFont());
	    	g.setColor(getForeground());
	    	g.drawString(label,width/2 - fm.stringWidth(label)/2,
	                    height/2 + fm.getMaxDescent());
	    }
	}

}

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; 
	

⌨️ 快捷键说明

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