java程序设计---计算器软件

公众号:爱写bug

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame {
	static JTextField t = new JTextField("", 32);

	public static void main(String[] args) {
		Calculator c = new Calculator();
		
	}

	public Calculator() {
		setTitle("简易计算器设计");
		Container app = getContentPane();
		t.setHorizontalAlignment(JTextField.RIGHT);
		app.add(t, BorderLayout.NORTH);
		Panel_t op = new Panel_t();
		app.add(op, BorderLayout.CENTER);
		Panel_n np=new Panel_n();
		app.add(np, BorderLayout.SOUTH);
		setResizable(false);
		pack();
		//setSize(700, 300);
		setVisible(true);
	}
}

class Panel_t extends JPanel {
	public Panel_t() {
		setLayout(new FlowLayout());
		JButton b1 = new JButton("退格");
		JButton b2 = new JButton("清除");
		b1.addActionListener(new ActionLis());
		b2.addActionListener(new ActionLis());
		add(b1);
		add(b2);
	}
}

class Panel_n extends JPanel {
	String s[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "+", "-", "*", "/", "=" };
	JButton b;

	public Panel_n() {
		setLayout(new GridLayout(4, 4));
		for (int i = 0; i <= 10; i++) {
			b = new JButton(s[i]);
			b.setActionCommand("number");
			b.addActionListener(new ActionLis());
			add(b);
		}
		for (int i = 11; i < s.length - 1; i++) {
			b = new JButton(s[i]);
			b.setActionCommand("caculate");
			b.addActionListener( new ActionLis());
			add(b);
		}
		b = new JButton("=");
		b.setActionCommand("result");
		b.addActionListener(new ActionLis());
		add(b);
	}
}

class ActionLis implements ActionListener { // 事件监听器
	static String ops;
	static boolean f = false; // 标识是否为一个新的操作数
	static double a, b, c;
	static String s, bText, text;
	
	@Override
	public void actionPerformed(ActionEvent e) {
		s = e.getActionCommand();
		bText = ((JButton) e.getSource()).getText();
		text = Calculator.t.getText();
		if (s=="number") { // 当按下数字,小数点时
			if (f) {
				Calculator.t.setText(bText); // 重新显示所按下的数字
				f = false; // 设置标志为假
			} else {
				Calculator.t.setText(text + bText); // 原有的数连接刚按下的数
			}
		}
		if (s=="caculate") { // 当按下的是操作符时
			if (!text.equals("")) { // 如果文本框不为空,说明按下操作符正确
				ops = bText; // 记录按下的操作符
				a = Double.parseDouble(text); // 将文本框中的数字转换为第一个操作数
				f = true; // 重新开始另一个操作数
			} else if (bText=="-") { // 如果文本框为空,按下的是“-”
				Calculator.t.setText(bText); // 将负号显示在文本框中
			}
		}
		if (s=="result" && !text.equals("")) { // 按下等号并且文本框不为空,计算结果
			b = Double.parseDouble(text); // 将文本框中的数字转换为第二个操作数
			if (ops=="+")
				c = a + b; // 如果操作数为加号,作加法
			if (ops=="-")
				c = a - b; // 如果操作数为减号,作减法
			if (ops=="*")
				c = a * b; // 如果操作数为乘号,作乘法
			if (ops=="/")
				c = a / b; // 如果操作数为除号,作除法
			Calculator.t.setText(String.valueOf(c)); // 结果显示在文本框中
			f = true; // 开始一个新的操作数

		}
		if (s=="退格") // 如果按下的是退格按钮,则删除最后一个数字
			Calculator.t.setText(text.substring(0, text.length() - 1));
		if (s=="清除") { // 如果按下的是清除按钮,则删除所有数字
			Calculator.t.setText("");
			f = false;
		}
	}
}

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值