Java实现简单计算器

package 计算器;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class A {
	public static void main(String [] args) {
		JFrame a=new JFrame("计算器");
		a.setBounds(0,0,800,800);
		a.setLayout(null);
		JButton b1=new JButton("CE");
		a.add(b1);
		b1.setForeground(Color.red);
		b1.setBounds(0,665,100,100);
		b1.setFont(new Font("宋体",Font.PLAIN,60));	
		JButton b2=new JButton("0");
		a.add(b2);
		b2.setBounds(100,665,100,100);
		b2.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b3=new JButton("=");
		a.add(b3);
		b3.setBounds(200,665,100,100);
		b3.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b4=new JButton("/");
		a.add(b4);
		b4.setBounds(300,665,100,100);
		b4.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b5=new JButton("1");
		a.add(b5);
		b5.setBounds(0,566,100,100);
		b5.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b6=new JButton("2");
		a.add(b6);
		b6.setBounds(100,566,100,100);
		b6.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b7=new JButton("3");
		a.add(b7);
		b7.setBounds(200,566,100,100);
		b7.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b8=new JButton("*");
		a.add(b8);
		b8.setBounds(300,566,100,100);
		b8.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b9=new JButton("4");
		a.add(b9);
		b9.setBounds(0,467,100,100);
		b9.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b10=new JButton("5");
		a.add(b10);
		b10.setBounds(100,467,100,100);
		b10.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b11=new JButton("6");
		a.add(b11);
		b11.setBounds(200,467,100,100);
		b11.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b12=new JButton("-");
		a.add(b12);
		b12.setBounds(300,467,100,100);
		b12.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b13=new JButton("7");
		a.add(b13);
		b13.setBounds(0,367,100,100);
		b13.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b14=new JButton("8");
		a.add(b14);
		b14.setBounds(100,367,100,100);
		b14.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b15=new JButton("9");
		a.add(b15);
		b15.setBounds(200,367,100,100);
		b15.setFont(new Font("宋体",Font.PLAIN,60));
		JButton b16=new JButton("+");
		a.add(b16);
		b16.setBounds(300,367,100,100);
		b16.setFont(new Font("宋体",Font.PLAIN,60));
		JLabel l1=new JLabel("式子");
		JLabel l2=new JLabel("答案");
		l1.setFont(new Font("宋体",Font.PLAIN,40));
		l2.setFont(new Font("宋体",Font.PLAIN,40));
		l1.setBounds(20,50,100,100);
		l2.setBounds(20,200,100,100);
		a.add(l1);
		a.add(l2);
		JTextArea t1=new JTextArea();
		JTextArea t2=new JTextArea();
		t1.setFont(new Font("宋体",Font.PLAIN,100));
		t2.setFont(new Font("宋体",Font.PLAIN,100));
		t1.setBounds(120,50,500,100);
		t2.setBounds(120,200, 500, 100);
		a.add(t2);
		a.add(t1);
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					t1.setText("");
					t2.setText("");
			}	
		});
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("0");
			}	
		});
		b3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int n1=0;
				int n2=0;
				int i=0;
				char c;
				String s=t1.getText();
				for(i=0;s.charAt(i)>='0'&&s.charAt(i)<='9';i++) {
					n1=n1*10+(s.charAt(i)-'0');
				}
				c=s.charAt(i++);
				for(;i<s.length();i++) {
					n2=n2*10+(s.charAt(i)-'0');
				}
				switch(c) {
				case '+':n1=n1+n2; break;
				case '-':n1=n1-n2; break;
				case '*':n1=n1*n2; break;
				case '/':n1=n1/n2; break;
				}
				t2.setText(String.valueOf(n1));
			}	
		});
		b4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("/");
			}	
		});
		b5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("1");
			}	
		});
		b6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("2");
			}	
		});
		b7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("3");
			}	
		});
		b8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("*");
			}	
		});
		b9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("4");
			}	
		});
		b10.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("5");
			}	
		});
		b11.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("6");
			}	
		});
		b12.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("-");
			}	
		});
		b13.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("7");
			}	
		});
		b14.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("8");
			}	
		});
		b15.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("9");
			}	
		});
		b16.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t1.append("+");
			}	
		});
		a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		a.setVisible(true);
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值