java简易计算器

1 篇文章 0 订阅
编写一个Swing,模拟实现一个可视化的简单计算器,至少提供包括加法、减法、乘法、除法、平方根、清零等基本操作
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class Calculator extends JFrame{
	private String string="";
	
	private JButton button0=new JButton("0");
	private JButton button1=new JButton("1");
	private JButton button2=new JButton("2");
	private JButton button3=new JButton("3");
	private JButton button4=new JButton("4");
	private JButton button5=new JButton("5");
	private JButton button6=new JButton("6");
	private JButton button7=new JButton("7");
	private JButton button8=new JButton("8");
	private JButton button9=new JButton("9");
	private JButton buttonadd = new JButton("+");// 加
	private JButton buttonminus = new JButton("-");// 减
	private JButton buttontime = new JButton("×");// 乘
	private JButton buttondivid = new JButton("/");// 除
	private JButton buttonequal = new JButton("=");// 等于
	private JButton buttondecimal = new JButton(".");// 小数点
	private JButton buttondelet = new JButton("←");// 删除
	private JButton buttonclear = new JButton("C");// 清除
	private JButton buttonsqrt = new JButton("√");//开根号
	private JButton buttonend = new JButton("End");
	
	private JTextField text=new JTextField();
	private double first=0,last=0,end=0;
	private char cal='+';
	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		Calculator calculator=new Calculator();
		calculator.setVisible(true);
		
	}
	public Calculator() {
		setSize(420,700);
		setLocation(400,200);
		setTitle("Calculator");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null); 
		text.setBounds(0, 0, 420, 100);
		text.setFont(new Font("dialog", 1, 30));
		
		button0.setBounds(0,550,100,100);
		button0.setFont(new Font("dialog", 1, 50));
		buttondecimal.setBounds(100,550,100,100);
		buttondecimal.setFont(new Font("dialog", 1, 50));
		buttonequal.setBounds(200,550,100,100);
		buttonequal.setFont(new Font("dialog", 1, 50));
		buttonadd.setBounds(300,550,100,100);
		buttonadd.setFont(new Font("dialog", 1, 50));
		
		button1.setBounds(0,450,100,100);
		button1.setFont(new Font("dialog", 1, 50));
		button2.setBounds(100,450,100,100);
		button2.setFont(new Font("dialog", 1, 50));
		button3.setBounds(200,450,100,100);
		button3.setFont(new Font("dialog", 1, 50));
		buttonminus.setBounds(300,450,100,100);
		buttonminus.setFont(new Font("dialog", 1, 50));
		
		button4.setBounds(0,350,100,100);
		button4.setFont(new Font("dialog", 1, 50));
		button5.setBounds(100,350,100,100);
		button5.setFont(new Font("dialog", 1, 50));
		button6.setBounds(200,350,100,100);
		button6.setFont(new Font("dialog", 1, 50));
		buttontime.setBounds(300,350,100,100);
		buttontime.setFont(new Font("dialog", 1, 50));
		
		button7.setBounds(0,250,100,100);
		button7.setFont(new Font("dialog", 1, 50));
		button8.setBounds(100,250,100,100);
		button8.setFont(new Font("dialog", 1, 50));
		button9.setBounds(200,250,100,100);
		button9.setFont(new Font("dialog", 1, 50));
		buttondivid.setBounds(300,250,100,100);
		buttondivid.setFont(new Font("dialog", 1, 50));
		
		buttonend.setBounds(0,150,100,100);
		buttonend.setFont(new Font("dialog", 1, 30));
		buttonsqrt.setBounds(100,150,100,100);
		buttonsqrt.setFont(new Font("dialog", 1, 50));
		buttondelet.setBounds(200,150,100,100);
		buttondelet.setFont(new Font("dialog", 1, 50));
		buttonclear.setBounds(300,150,100,100);
		buttonclear.setFont(new Font("dialog", 1, 50));
		
		
		add(text);
		add(button0);
		add(button1);
		add(button2);
		add(button3);
		add(button4);
		add(button5);
		add(button6);
		add(button7);
		add(button8);
		add(button9);
		add(buttonadd);
		add(buttondecimal);
		add(buttonequal);
		add(buttonminus);
		add(buttontime);
		add(buttondivid);
		add(buttondelet);
		add(buttonclear);
		add(buttonsqrt);
		add(buttonend);
		
		button0.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string=string+"0";
				text.setText(string);
			}
		});
		button1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string=string+"1";
				text.setText(string);
			}
		});
		button2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string=string+"2";
				text.setText(string);
			}
		});
		button3.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string=string+"3";
				text.setText(string);
			}
		});
		button4.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string=string+"4";
				text.setText(string);
			}
		});
		button5.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string=string+"5";
				text.setText(string);
			}
		});
		button6.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string=string+"6";
				text.setText(string);
			}
		});
		button7.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string=string+"7";
				text.setText(string);
			}
		});
		button8.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string=string+"8";
				text.setText(string);
			}
		});
		button9.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string=string+"9";
				text.setText(string);
			}
		});
		buttonadd.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				char[] str=string.toCharArray();
				int flag=10000;
				int pos=0;
				boolean bool=false;
				for(int i=0;i<str.length;i++) {
					first=0;
					if(str[i]=='.') {
						flag=i;
					}
					if(str[0]=='√') {
						bool=true;
						pos=1;
					}
				}
				for(int i=pos;i<str.length;i++) {
					if(i<flag) {
						first=first*10+str[i]-'0';
					}
					else if(i>flag){
						first=first+(str[i]-'0')/(double)Math.pow(10, (i-flag));
					}
				} 
				cal='+';
				string="+";
				text.setText(string);
				if(bool) {
					first=Math.sqrt(first);
					
				}
				string="";
			}
		});
		buttonminus.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				char[] str=string.toCharArray();
				int flag=10000;
				int pos=0;
				boolean bool=false;
				for(int i=0;i<str.length;i++) {
					first=0;
					if(str[i]=='.') {
						flag=i;
					}
					if(str[0]=='√') {
						bool=true;
						pos=1;
					}
				}
				for(int i=pos;i<str.length;i++) {
					if(i<flag) {
						first=first*10+str[i]-'0';
					}
					else if(i>flag){
						first=first+(str[i]-'0')/(double)Math.pow(10, (i-flag));
					}
				} 
				cal='-';
				string="-";
				text.setText(string);
				if(bool) {
					first=Math.sqrt(first);
				}
				string="";
			}
		});
		buttontime.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				char[] str=string.toCharArray();
				int flag=10000;
				int pos=0;
				boolean bool=false;
				for(int i=0;i<str.length;i++) {
					first=0;
					if(str[i]=='.') {
						flag=i;
					}
					if(str[0]=='√') {
						bool=true;
						pos=1;
					}
				}
				for(int i=pos;i<str.length;i++) {
					if(i<flag) {
						first=first*10+str[i]-'0';
					}
					else if(i>flag){
						first=first+(str[i]-'0')/(double)Math.pow(10, (i-flag));
					}
				} 
				cal='*';
				string="*";
				text.setText(string);
				if(bool) {
					first=Math.sqrt(first);
				}
				string="";
			}
		});
		buttondivid.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				char[] str=string.toCharArray();
				int flag=10000;
				int pos=0;
				boolean bool=false;
				for(int i=0;i<str.length;i++) {
					first=0;
					if(str[i]=='.') {
						flag=i;
					}
					if(str[0]=='√') {
						bool=true;
						pos=1;
					}
				}
				for(int i=pos;i<str.length;i++) {
					if(i<flag) {
						first=first*10+str[i]-'0';
					}
					else if(i>flag){
						first=first+(str[i]-'0')/(double)Math.pow(10, (i-flag));
					}
				} 
				cal='/';
				string="/";
				text.setText(string);
				if(bool) {
					first=Math.sqrt(first);
				}
				string="";
			}
		});
		buttonequal.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				char[] str=string.toCharArray();
				int flag=10000;
				int pos=0;
				boolean bool=false;
				for(int i=0;i<str.length;i++) {
					last=0;
					if(str[i]=='.') {
						flag=i;
					}
					if(str[0]=='√') {
						bool=true;
						pos=1;
					}
				}
				for(int i=pos;i<str.length;i++) {
					if(i<flag) {
						last=last*10+str[i]-'0';
					}
					else if(i>flag){
						last=last+(str[i]-'0')/(double)Math.pow(10, (i-flag));
					}
				}
				if(bool) {
					last=Math.sqrt(last);
				}
				switch(cal) {
				case '+':
					end=first+last;
					break;
				case '-':
					end=first-last;
					break;
				case '*':
					end=first*last;
					break;
				case '/':
					end=first/last;
					break;
				}
				double t=end;
				string=end+"0";
				text.setText(string);
				initial();
				first=t;
				string="";
			}
		});
		buttondecimal.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string=string+".";
				text.setText(string);
			}
		});
		buttonsqrt.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string=string+"√";
				text.setText(string);
			}
		});
		buttonclear.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				string="";
				initial();
				text.setText(string);
			}
		});
		buttondelet.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				char[] str=string.toCharArray();
				string="";
				for(int i=0;i<str.length-1;i++) {
					string = string+str[i];
				}
				text.setText(string);
			}
		});
		buttonend.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
				System.exit(0);
			}
		});
	}
	public void initial() {
		first=0;
		last=0;
		end=0;
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值