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

📄 calculator.java

📁 j2se5.0全方位学习一书源代码
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;

public class Calculator extends WindowAdapter implements ActionListener
{
	private Frame f;
	private Button b[] = new Button[17];
	private Label result; //显示结果
	
	private double num = 0; //存放输入的数字
	private char op = '0'; //选择的运算模式
	private boolean isOp = false; //是否按过运算符号
	private boolean hasDot = false; //是否按过小数子
	
	public static void main(String argv[])
	{
		new Calculator();
	}
	
	public Calculator()
	{
		f = new Frame("我的计算机");
		f.addWindowListener(this);
		
		Panel p = new Panel(new GridBagLayout());

		String str[] = { "C", "/", "*", "-", "7", "8", "9", "+",
					   "4", "5", "6", "1", "2", "3", "=", "0", "."};
					   
		int fill[] = { GridBagConstraints.BOTH,
			      	   GridBagConstraints.VERTICAL,
			      	   GridBagConstraints.HORIZONTAL,
			      	   GridBagConstraints.NONE };

		int anchor[] = { GridBagConstraints.CENTER,
						 GridBagConstraints.EAST,
						 GridBagConstraints.SOUTH,
						 GridBagConstraints.SOUTHEAST,
						 GridBagConstraints.SOUTHWEST,
						 GridBagConstraints.WEST,
						 GridBagConstraints.NORTH,
						 GridBagConstraints.NORTHEAST,
						 GridBagConstraints.NORTHWEST };

		int att[][] = { {0, 0, 1, 1, 1, 1, fill[0], anchor[0]}, //1
				{1, 0, 1, 1, 1, 1, fill[0], anchor[0]}, //2
				{2, 0, 1, 1, 1, 1, fill[0], anchor[0]}, //3
				{3, 0, 1, 1, 1, 1, fill[0], anchor[0]}, //4
				{0, 1, 1, 1, 1, 1, fill[0], anchor[0]}, //5
				{1, 1, 1, 1, 1, 1, fill[0], anchor[0]}, //6
				{2, 1, 1, 1, 1, 1, fill[0], anchor[0]}, //7
				{3, 1, 1, 2, 1, 1, fill[0], anchor[0]}, //8
				{0, 2, 1, 1, 1, 1, fill[0], anchor[0]}, //9
				{1, 2, 1, 1, 1, 1, fill[0], anchor[0]}, //10
				{2, 2, 1, 1, 1, 1, fill[0], anchor[0]}, //11
				{0, 3, 1, 1, 1, 1, fill[0], anchor[0]}, //12
				{1, 3, 1, 1, 1, 1, fill[0], anchor[0]}, //13
				{2, 3, 1, 1, 1, 1, fill[0], anchor[0]}, //14
				{3, 3, 1, 2, 1, 1, fill[0], anchor[0]}, //15
				{0, 4, 2, 1, 1, 1, fill[0], anchor[0]}, //16
				{2, 4, 1, 1, 1, 1, fill[0], anchor[0]} }; //17

		for (int i=0; i<b.length; i++)
		{
			b[i] = new Button(str[i]);
			b[i].addActionListener(this);
			b[i].setActionCommand(str[i]);
			add(p, b[i], att[i]);
		}

		result = new Label("0");
		result.setAlignment(Label.RIGHT);
		
		f.add(p, BorderLayout.CENTER);
		f.add(result, BorderLayout.NORTH);
		f.pack();
		f.setVisible(true);
	}

	private void add(Container con, Component com, int att[])
	{
		GridBagConstraints cons = new GridBagConstraints();
		cons.gridx = att[0];
		cons.gridy = att[1];
		cons.gridwidth = att[2];
		cons.gridheight = att[3];
		cons.weightx = att[4];
		cons.weighty = att[5];
		cons.fill = att[6];
		cons.anchor = att[7];

		con.add(com, cons);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		String cmd = e.getActionCommand();
		char c = cmd.charAt(0);
		
		switch(c)
		{
		case '+':
		case '-':
		case '*':
		case '/': //按下运算符号
    		//如果已经按过运算符号
    		//且没有连按两次
    		//则先把刚刚的结果计算出来
    		if (op!='0' && !isOp) //模拟等号键被按下
    			calResult();
    			
    		num = Double.valueOf(result.getText()).doubleValue();
    		op = c;
    		isOp = true;
		    hasDot = false;
			break;
		case '=': //按下等号
			calResult();
			break;
		case '.': //按下小数点
			if ( !hasDot )
			{
				result.setText(result.getText()+".");
				hasDot = true;
			}
			break;
		case 'C': //按下清除
			num = 0;
			op = '0';
			isOp = false;
			hasDot = false;
			result.setText("0");
			break;
		default: //按下数字
			//判断如果result目前显示0的或是有按下运算符号的话
			//则直接显示按下的数字
    		//否则把按下的数字加在原来显示的数字之后
    		if (result.getText().equals("0") || isOp)
    		{
    			result.setText(cmd);
    			isOp = false;
    		}
    		else
    			result.setText(result.getText()+cmd);
		}
	}

	//计算结果
	private void calResult()
	{
    	double ans = 0;
    	double num2 = Double.valueOf(result.getText()).doubleValue();
    
    
	    switch (op)
	    {
    	case '+':
	        ans = num + num2;
	        break;
    	case '-':
	        ans = num - num2;
	        break;
    	case '*':
	        ans = num * num2;
	        break;
    	case '/':
	        ans = num / num2;
    	}
    
    	op = '0';
    	isOp = true;
    	hasDot = false;
    	num = ans;
    	result.setText(String.valueOf(ans));
	}
	
	public void windowClosing(WindowEvent e)
	{
		System.exit(0);
	}
}

⌨️ 快捷键说明

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