简单计算器(java)

再次验证了一个真理,隔岸观火不如身临其境,亲自动手写过才知道有多蛋疼,以前光顾着YY了,面壁去……

有些个bug目前还想不到怎么处理,欢迎板砖,欢迎鄙视~~

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.math.BigDecimal;
import javax.swing.JFrame;

public class hello extends Applet{
	static int flag_A=0,flag_B=0;
	static char none = 'n';
	static char last_in=none;		//最近一次合法输入
	static char last_flag=none;		//最近一次合法输入的运算符号
	static BigDecimal a = BigDecimal.valueOf(0),tmp,zero = BigDecimal.valueOf(0);
	static TextField textfield = new TextField();
	SimpleListener ourListener = new SimpleListener();
	
	protected void makebutton(String name,GridBagLayout gridbag,GridBagConstraints c){
		Button button = new Button(name);
		button.addActionListener(ourListener);
		gridbag.setConstraints(button, c);
		add(button);
	}
	public void init(){
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		setFont(new Font("Helvetica",Font.BOLD,20));
		setLayout(gridbag);
		
		c.insets = new Insets(3,5,3,5);
		c.gridheight = 1;
		c.gridwidth = 4;
		c.weightx = 1.5;
		c.weighty = 1.5;
		c.fill = GridBagConstraints.BOTH;
		
		gridbag.setConstraints(textfield, c);
		add(textfield);
		
		/*button 和 textfield 共用一个约束,只是textfield 占用一行四列空间*/
		c.gridwidth = 1;
		
		String button_name[][]={
				{"1","2","3","+"},
				{"4","5","6","-"},
				{"7","8","9","*"},
				{".","0","=","/"}
		};
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				c.gridx = j; c.gridy = i+1;
				makebutton(button_name[i][j],gridbag,c);
			}
		}
	}
	
	private class SimpleListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String buttonName = e.getActionCommand();
			char key = buttonName.charAt(0);
/******************************************************************************/
			if(key=='.'){
				if(textfield.getText().indexOf(".")==-1){
					if(last_in!=none){
						if(last_in=='+' || last_in=='-' || last_in=='*' || last_in=='/'){
							last_flag=last_in;
							a = new BigDecimal(textfield.getText());
							textfield.setText("");
						}
					}
					last_in=key;
					textfield.setText(textfield.getText()+buttonName);
				}
			}
			else if(key>='0' && key<='9'){
				if(last_in!=none){
					if(last_in=='='){
						a = zero;
						last_flag= none;
						textfield.setText("");
					}
					else if(last_in=='+' || last_in=='-' || last_in=='*' || last_in=='/'){
						last_flag=last_in;
						a = new BigDecimal(textfield.getText());
						textfield.setText("");
					}
				}
				last_in=key;
				textfield.setText(textfield.getText()+buttonName);
			}
			else if(key=='+' || key=='-' || key=='*' || key=='/' || key=='='){
				if(last_in>='0' && last_in<='9' || last_in=='='){
					if(last_flag!=none){
						tmp = new BigDecimal(textfield.getText());
						switch(last_flag){
						case '+':a=a.add(tmp);break;
						case '-':a=a.subtract(tmp);break;
						case '*':a=a.multiply(tmp);break;
						case '/':
							if(tmp.equals(zero)==true)textfield.setText("Error!");
							else a=a.divide(tmp,6,BigDecimal.ROUND_HALF_UP);
							break;
						case '=':break;
						}
						last_flag=key;
						textfield.setText(a.toString());
					}
					last_in=key;
				}
			}
/******************************************************************************/
		}
	}
	
	public static void main(String args[]){
	
		JFrame frm = new JFrame("计算器");		//创建窗体
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		hello fuck = new hello();
		fuck.init();
		frm.add("Center",fuck);
		frm.pack();
		frm.setLocation(250,250);
		frm.setSize(200,230);
		
		frm.setResizable(false);
		frm.setVisible(true);
	}
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值