这个计算器的界面模仿Windows自带的简易计算器。
思路:
不考虑连续计算(如1+2+1)的情况,只是两个数值进行基本运算
1、定义三个变量
2、将第一次按钮的值赋值个第一个变量
3、将第二个按钮的值(也就是加减乘除)赋值给第二个变量
4、将第三个按钮的值赋值给第三个变量
5、用case或者if判断第二个变量,判断是那种计算,比如是+,就直接将第一个和第三个变量相加
6、将得到的结果显示
代码:
import erLayout;
import r;
import Layout;
import onEvent;
import onListener;
import eIcon;
import ton;
import me;
import el;
import tField;
import gConstants;
public class Jisuanqi extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Result result = new Result(); // 定义text的面板
Number_Key number_key = new Number_Key(); // 定义按钮面板
// 当点击按钮+、-、*、/时,com = true
boolean com = false;
// 当i=0时说明是我们第一次输入,字符串text不会累加
int i = 0;
// 存放text的内容
String text = "";
// 存放点击按钮+、-、*、/之前的数值
double defbutton = 0;
// +、-、*、/的代号分别为1,2,3,4
int symbol = 0;
// 构造函数
Jisuanqi() {
super("WangJiao"); // 设定标题
setDefaultCloseOperation(_ON_CLOSE); // 设定关闭窗体时退出程序
JPanel pane = new JPanel(); // 定义主面板
ayout(new BorderLayout());
setBounds(380, 220, 30, 80); // 前两个参数是在屏幕上显示的坐标,后两个是大小
// 替换图标
ImageIcon icon = new ImageIcon("F:1.GIF");
// Jisuanqi.class.getResource("APPLE.GIF")
// );
setIconImage(mage());
(result, H);
(number_key, ER);
(number_l, H);
number_ctionListener(this); // 对1按钮添加监听事件
number_ctionListener(this); // 对2按钮添加监听事件
number_ctionListener(this); // 对3按钮添加监听事件
number_ctionListener(this); // 对4按钮添加监听事件
number_ctionListener(this); // 对5按钮添加监听事件
number_ctionListener(this); // 对6按钮添加监听事件
number_ctionListener(this); // 对7按钮添加监听事件
number_ctionListener(this); // 对8按钮添加监听事件
number_ctionListener(this); // 对9按钮添加监听事件
number_ctionListener(this); // 对0按钮添加监听事件
number_ctionListener(this); // 对置零按钮添加监听事件
number_ctionListener(this); // 对+按钮添加监听事件
number_ctionListener(this); // 对=按钮添加监听事件
number_ctionListener(this); // 对-按钮添加监听事件
number_ctionListener(this); // 对*按钮添加监听事件
number_ctionListener(this); // 对/按钮添加监听事件
number_ctionListener(this); // 对.按钮添加监听事件
setContentPane(pane);
pack(); // 初始化窗体大小为正好盛放所有按钮
}
// 各个按钮触发的事件
public void actionPerformed(ActionEvent e) {
/*
* 如果是点击数字按钮那么先要判断是否在此之前点击了+、-、*、/、=,如果是那么com=true 如果没有com=
* false;或者是否点击数字键,如果是i = 1,如果没有 i = 0;
*/
if (ource() == number_) {
if (com || i == 0) {
ext("1");
com = false;
i = 1;
} else {
text = ext();
ext(text + "1");
}
} else if (ource() == number_) {
if (com || i == 0) {
ext("2");
com = false;
i = 1;
} else {
text = ext();
ext(text + "2");
}
} else if (ource() == number_e) {
if (com || i == 0) {