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

📄 calculator.java

📁 这是一个计算器!
💻 JAVA
字号:
// Created on 2004-5-26
/**
 * @author don
 *This program will creat a GUI for calculator.	Use the Grid Layout to arrange the GUI.
 *At the same time,creat two JPanel object,one for the textfield,one for the button field.
 *
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
	public static final int WIDTH = 300;
	public static final int HEIGHT = 200;
    public static double sum=0;
    public static String str="";
    public static String charstr="";
    public static double temp=0;
	private final String digits[] = {"0", "1", "2", "3", "4", "5","6", "7", "8", "9",".", "+", "-", "*", "/","="};
    private JTextField inputOutputField;
	public static void main(String[] args){
		Calculator calculate = new Calculator( );
		calculate.setVisible(true);
	}

	public Calculator( )
	{
		setSize(WIDTH, HEIGHT);
		addWindowListener(new WindowDestroyer());
		setTitle("Calculator");   //set title for calculator
		Container contentPane = getContentPane( );
		contentPane.setBackground(Color.lightGray);
		contentPane.setLayout(new BorderLayout( ));
		JPanel Panel = new JPanel( );
		Panel.setLayout(new GridLayout(4,4)); //for the button arragement
		JButton buttons[]= new JButton[ digits.length ]; 
		for ( int count = 0; count < digits.length; count++ ) { //give definition to each buttons
		   buttons[ count ] = new JButton( digits[ count ] );
		   buttons[ count ].addActionListener( this );
		   Panel.add( buttons[ count ] );
		}
		contentPane.add(Panel,BorderLayout.CENTER);
		JPanel textPanel = new JPanel( );
		textPanel.setLayout(new GridLayout(1,1));
		inputOutputField=new JTextField("0");
		inputOutputField.setBackground(Color.WHITE);
		textPanel.add(inputOutputField);
		contentPane.add( textPanel,BorderLayout.NORTH);//the location of the textfield 
	}

	public void actionPerformed(ActionEvent e)
	{
	   Container contentPane = getContentPane( );
	   int i=0;boolean bool=true;
	   for(i=0;i<digits.length;i++)
	   if(e.getActionCommand().equals(digits[i]))
			 break;
	   if (i>=0&&i<=10){
	   	
	   for(int a=0;a<str.length();a++)
		if(str.charAt(a)=='.'&&e.getActionCommand().equals(".")){ 
		bool=false;
		break;
		}
	   if(bool){
	       if(str.equals("0")&&e.getActionCommand().equals("."))
		   inputOutputField.setText( str+=e.getActionCommand());
		   else if(str.equals("0")&&!e.getActionCommand().equals(".")){
		   inputOutputField.setText( str=e.getActionCommand());
		   temp=Double.parseDouble(str);
		   } 
		   else if(str.equals("")&&e.getActionCommand().equals("."))
		   inputOutputField.setText( str=0+e.getActionCommand());
		   else{
		   inputOutputField.setText( str+=e.getActionCommand());
		   temp=Double.parseDouble(str);
		   }
		}
	   }  
	   else if(i>=11&&i<=14){
	      calculator(Double.toString(temp));
		  str="";
		  charstr=e.getActionCommand();
	   }
	   else{
			 if(temp==0&&charstr.equals("/")){
			 inputOutputField.setText("Cannot divide by zero");
			 inputOutputField.setEditable(false);
		     }
		     else{
		     calculator(Double.toString(temp));
			 inputOutputField.setText(Double.toString(sum));
			 temp=sum;
			 str="";
			 charstr="";
		    }
      } 
	  			
}
private void calculator(String digit){
	if(charstr.equals("+"))
	sum=sum+Double.parseDouble(digit);
	else if(charstr.equals("-"))
	sum=sum-Double.parseDouble(digit);
	else if(charstr.equals("*"))
	sum=sum*Double.parseDouble(digit);
	else if(charstr.equals("/"))
    sum=sum/Double.parseDouble(digit);
    else
	sum=Double.parseDouble(digit);
	}
}

⌨️ 快捷键说明

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