Java实现的简单计算器

    第一次用Java写的,弱爆了,想鄙视的尽管来吐槽吧= =

import javax.swing.*;
import java.awt.Font;
import java.awt.event.*;
class MyTest extends JFrame
{
	private static final long serialVersionUID = 1L;
	private JTextField t=new JTextField("0");
	private String name[]={"清除","退格",
			               "7","8","9","+",
			               "4","5","6","-",
			               "1","2","3","*",
			               "0",".","=","/"};
	private JButton bt[]=new JButton[18];
	private int i,hasdot,hasplan;
	private double a,b;
	private char plan;
	private Font ft=new Font("Serief",Font.BOLD,20);
	public MyTest()
	{
		this.setTitle("计算器");
		for(i=0;i<18;++i)
			bt[i]=new JButton(name[i]);
		hasdot=hasplan=plan=0;
		for(i=0;i<18;++i)addListener(bt[i]);
		this.addKeyListener(new KeyAdapter()   
		{ 
		     public void keyPressed(KeyEvent e)   
		     {
                 Input(e.getKeyChar());
		    	 if(e.getKeyCode()==8)Delete();
		    	 if(e.getKeyCode()==10)work();
		     } 
		});
		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent arg0)
			{
				System.exit(1);
			}
		});
		t.setFont(ft);
		for(i=0;i<18;++i)
			bt[i].setFont(ft);
		t.setHorizontalAlignment(JTextField.RIGHT);
		t.setBounds(2,2,198,40);
		bt[0].setBounds(2,44,97,36);
		bt[1].setBounds(102,44,97,36);
		bt[2].setBounds(2,82,47,47);
		bt[3].setBounds(52,82,47,47);
		bt[4].setBounds(102,82,47,47);
		bt[5].setBounds(152,82,47,47);
		bt[6].setBounds(2,131,47,47);
		bt[7].setBounds(52,131,47,47);
		bt[8].setBounds(102,131,47,47);
		bt[9].setBounds(152,131,47,47);
		bt[10].setBounds(2,180,47,47);
		bt[11].setBounds(52,180,47,47);
		bt[12].setBounds(102,180,47,47);
		bt[13].setBounds(152,180,47,47);
		bt[14].setBounds(2,229,47,47);
		bt[15].setBounds(52,229,47,47);
		bt[16].setBounds(102,229,47,47);
		bt[17].setBounds(152,229,47,47);
		t.setFocusable(false);
		for(i=0;i<18;++i)
			bt[i].setFocusable(false);
		this.add(t);
		for(i=0;i<18;++i)
			this.add(bt[i]);
		this.setLayout(null);
		this.setLocation(350,250);
		this.setSize(209,313);
		this.setVisible(true);
	}
	void addListener(final JButton bt)
	{
		bt.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if(e.getSource()==bt);
				{
					if("清除".equals(bt.getText()))t.setText("0");
					else if("退格".equals(bt.getText()))Delete();
					else if("=".equals(bt.getText()))work();
					else Input(bt.getText().charAt(0));
					
				}
			}
		});
	}
	void Input(char c)
	{
		if((c>='0'&&c<='9')||(c=='.'&&hasdot==0))
		{
			if(hasplan==1)
			{
				hasplan=2;
				a=Double.parseDouble(t.getText());
				t.setText("0");
			}
   		 	if(hasplan==3)
   		 	{
   		 		hasplan=0;
   		 		t.setText("0");
   		 	}
   		 	if(t.getText().length()>15)return;
   		 	if("0".equals(t.getText())&&c!='.')
   		 	{
   		 		t.setText(""+c);
   		 		if(c=='.')hasdot=1;
   		 	}
   		 	else
   		 	{
   		 		t.setText(t.getText()+c);
   		 		if(c=='.')hasdot=1;
   		 	}
   	 	}
   	 	if(c=='+'||c=='-'||c=='*'||c=='/')
   	 	{
   	 		hasplan=1;
   	 		plan=c;
   	 	}
	}
	void Delete()
	{
		int len=t.getText().length();
		if(len==1)t.setText("0");
		else if(len>1)
		{
			if(t.getText().charAt(len-1)=='.')hasdot=0;
			t.select(len-1,len);
			t.cut();
		}
	}
	void work()
	{
		if(hasplan==2)
		{
			hasplan=0;
			b=Double.parseDouble(t.getText());
			if(plan=='+')t.setText(""+(a+b));
			if(plan=='-')t.setText(""+(a-b));
			if(plan=='*')t.setText(""+(a*b));
			if(plan=='/')
			{
				if(b!=0)t.setText(""+(a/b));
				else
				{
					t.setText("不能被零除!");
					hasplan=3;
				}
			}
			hasdot=0;
			for(i=t.getText().length()-1;i>=0;--i)
				if(t.getText().charAt(i)=='.')hasdot=1;
		}
	}
}
public class Main
{
	public static void main(String args[])throws Exception
	{
		new MyTest();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值