package myPacket;
import java.awt.*;
import java.awt.event.*;
public class Calculator
{
private Frame f = new Frame("计算器"); // 主窗口 显示计算器主界面
private Frame info = new Frame("关于"); // 介绍窗口
private Button[] b = new Button[10]; // 声明数字按钮
private Button[] cal = new Button[8]; // 声明功能按钮
private MenuBar mb = new MenuBar(); // 菜单工具栏
private Menu option = new Menu("操作"); // 操作菜单按钮
private Menu other = new Menu("Other"); // 其他菜单按钮
private MenuItem about = new MenuItem("about");
private MenuItem reset = new MenuItem("reset");
private MenuItem exit = new MenuItem("Exit");
private Label information = new Label();
private TextField text = new TextField(30); // 输入文本框
private int flag = 0; // 重置标记
private int flag2 = -1; // 对文本框中是否有内容进行标记 有则设置为 -1 数字键标记
private int Key = -1; // + - * / 运算标记
String load1 = new String();
String load2 = new String();
public void init() // 初始化
{
Panel p = new Panel();
NumListener numListen = new NumListener(); // 数字键
CalListener calListen = new CalListener(); // 功能键
load1 = load2 = null;
for(int i = 0; i <= 9; i++)
b[i] = new Button(""+i); // 初始化数字键按钮
info.setSize(200,400); // 初始化信息窗口大小
information.setText("Java Calculator"); // 设置信息内容
for(int i = 0; i <= 9; i++)
b[i].addActionListener(numListen); // 给按钮添加数字键的事件监听器
cal[0] = new Button("+");
cal[1] = new Button("-");
cal[2] = new Button("*");
cal[3] = new Button("/");
cal[4] = new Button("=");
cal[5] = new Button("退格"); // 退格键
cal[6] = new Button(".");
cal[7] = new Button("AC");
cal[0].addActionListener(calListen);
cal[1].addActionListener(calListen);
cal[2].addActionListener(calListen);
cal[3].addActionListener(calListen);
cal[4].addActionListener(calListen);
cal[5].addActionListener(e->
{
if (text.getText().length() == 0) {
System.out.println("无字符");
}else {
text.setText(text.getText().substring(0,text.getText().length()-1));
}
});
cal[6].addActionListener(e->
{
text.setText(text.getText()+"."); // 添加小数点
});
// 在面板上依次添加数据按钮和功能按钮
p.add(b[1]);p.add(b[2]);p.add(b[3]);p.add(cal[0]);
p.add(b[4]);p.add(b[5]);p.add(b[6]);p.add(cal[1]);
p.add(b[7]);p.add(b[8]);p.add(b[9]);p.add(cal[2]);
p.add(cal[5]);p.add(b[0]);p.add(cal[6]);p.add(cal[3]);
p.add(cal[7]);p.add(cal[4]);
text.setEditable(false); // 禁止对文本框进行编辑
exit.addActionListener(e->System.exit(0)); // 给菜单按钮添加事件监听 退出
reset.addActionListener(e->
{
load1 = load2 = null;
Key = -1;
flag2 = -1;
flag = 0;
text.setText(""); // 重置时清空输入窗格
});
cal[7].addActionListener(e-> // AC 键的功能为重置
{
load1 = load2 = null;
Key = -1;
flag2 = -1;
flag = 0;
text.setText("");
});
about.addActionListener(e->
{
info.setVisible(true); // 弹出信息窗格
});
option.add(reset);
option.add(exit);
other.add(about);
mb.add(option);
mb.add(other);
f.setMenuBar(mb); // 设置工具条
f.add(text,BorderLayout.NORTH); // 设置文本框的布局
p.setLayout(new GridLayout(5,4,4,4)); // 设置布局 5 行 4 列 ;水平 4 个 垂直 4 个
info.add(information);
info.addWindowListener(new WindowListener()); // 设置对窗口事件的监听器
f.addWindowListener(new WindowListener());
f.add(p); // 将面板添加到窗口
f.setSize(640,480); // 设置计算器窗口大小
f.setVisible(true);
}
// 窗口事件监听器
class WindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e) // 对关闭窗口事件进行响应
{
if(e.getSource() == f)
System.exit(0);
else if(e.getSource() == info)
info.setVisible(false);
}
}
// 数字键的事件监听器
class NumListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b[0])
{
if(flag2 == -1) // 文本框中已有内容时
text.setText(text.getText()+"0");
else
{
text.setText("0"); // 数字 0
flag2 = -1;
}
}
else if(e.getSource() == b[1])
{
if(flag2 == -1)
text.setText(text.getText()+"1");
else
{
text.setText("1"); // 数字 1
flag2 = -1;
}
}
else if(e.getSource() == b[2])
{
if(flag2 == -1)
text.setText(text.getText()+"2");
else
{
text.setText("2"); // 数字 2
flag2 = -1;
}
}
else if(e.getSource() == b[3])
{
if(flag2 == -1)
text.setText(text.getText()+"3");
else
{
text.setText("3"); // 数字 3
flag2 = -1;
}
}
else if(e.getSource() == b[4])
{
if(flag2 == -1)
text.setText(text.getText()+"4");
else
{
text.setText("4"); // 数字 4
flag2 = -1;
}
}
else if(e.getSource() == b[5])
{
if(flag2 == -1)
text.setText(text.getText()+"5");
else
{
text.setText("5"); // 数字 5
flag2 = -1;
}
}
else if(e.getSource() == b[6])
{
if(flag2 == -1)
text.setText(text.getText()+"6");
else
{
text.setText("6"); // 数字 6
flag2 = -1;
}
}
else if(e.getSource() == b[7])
{
if(flag2 == -1)
text.setText(text.getText()+"7");
else
{
text.setText("7"); // 数字 7
flag2 = -1;
}
}
else if(e.getSource() == b[8])
{
if(flag2 == -1)
text.setText(text.getText()+"8");
else
{
text.setText("8"); // 数字 8
flag2 = -1;
}
}
else if(e.getSource() == b[9])
{
if(flag2 == -1)
text.setText(text.getText()+"9");
else
{
text.setText("9"); // 数字 9
flag2 = -1;
}
}
}
}
class CalListener implements ActionListener // 功能按键的事件监听
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == cal[0]) // + 按钮
{
if(flag == 0)
{
load1 = text.getText();
flag = 1;
flag2 = Key = 0;
}
else if(flag == 1)
{
load2 = text.getText();
text.setText(String.valueOf(Double.parseDouble(load1) + Double.parseDouble(load2)));
load1 = text.getText();
load2 = null;
//flag = 0;
}
}
else if(e.getSource() == cal[1]) // - 按钮
{
if(flag == 0)
{
load1 = text.getText();
flag = 1;
flag2 = Key = 1;
}
else if(flag == 1)
{
load2 = text.getText();
text.setText(String.valueOf(Double.parseDouble(load1) - Double.parseDouble(load2)));
load1 = text.getText();
load2 = null;
//flag = 0;
}
}
else if(e.getSource() == cal[2]) // * 按钮
{
if(flag == 0)
{
load1 = text.getText();
flag = 1;
flag2 = Key = 2;
}
else if(flag == 1)
{
load2 = text.getText();
text.setText(String.valueOf(Double.parseDouble(load1) * Double.parseDouble(load2)));
load1 = text.getText();
load2 = null;
//flag = 0;
}
}
else if(e.getSource() == cal[3]) // 除法 按钮
{
if(flag == 0)
{
load1 = text.getText();
flag = 1;
flag2 = Key = 3;
}
else if(flag == 1)
{
load2 = text.getText();
text.setText(String.valueOf(Double.parseDouble(load1) / Double.parseDouble(load2)));
load1 = text.getText();
load2 = null;
//flag = 0;
}
}
else if(e.getSource() == cal[4]) // = 按钮
{
if(load1 != null && load2 == null)
{
load2 = text.getText();
if(Key == 0)
text.setText(String.valueOf(Double.parseDouble(load1) + Double.parseDouble(load2)));
else if(Key == 1)
text.setText(String.valueOf(Double.parseDouble(load1) - Double.parseDouble(load2)));
else if(Key == 2)
text.setText(String.valueOf(Double.parseDouble(load1) * Double.parseDouble(load2)));
else if(Key == 3)
text.setText(String.valueOf(Double.parseDouble(load1) / Double.parseDouble(load2)));
Key = -1;
flag = 0;
flag2 = -1;
load1 = text.getText();
load2 = null;
}
}
}
}
public static void main(String[] args)
{
new Calculator().init();
}
}