import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.TextField;
import javax.swing.JPanel;
/**
*
* @author Administrator
*测试类
*/
public class Test
{
public static void main(String [] args)
{
Computer com=new Computer();
com.go();
}
}
/**
*
* @author Administrator
*按钮类,继承Button类,添加一个隐藏值;
*/
class ButtonNum extends Button
{
private int TAG;
public void setTAG(int value)
{
this.TAG=value;
}
public int getTAG()
{
return this.TAG;
}
public ButtonNum()
{
super();
}
public ButtonNum(String name)
{
super(name);
}
}
/**
*
* @author Administrator
*自定义一个超级适配器
*/
class SuperGuard implements WindowListener ,MouseListener
{
@Override
public void mouseClicked(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e)
{
// TODO Auto-generated method stub
}
}
/**
*
* @author Administrator 超级事件处理类
*/
class SuperEvent extends SuperGuard
{
private boolean iftrue;//这个变量是用来做判断点完运算符时再点数字时,text内的数值清空;
private NumberValues numberOne;//这个变量是用来存第一个值;
private NumberValues numberTwo;//这个变量是用来存第二个值;
private NumberValues OperateValues;//这个变量是用来存运算符的;
//在这里界面初始化会NEW这个类,这里构造会初始化当前面所有变量的初始值;
public SuperEvent()
{
iftrue = false;
numberOne = new NumberValues();
numberTwo = new NumberValues();
OperateValues = new NumberValues();
numberOne.setStates(false);
numberTwo.setStates(false);
OperateValues.setStates(false);
}
//这里不重要,用来关闭程序!
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
/**
* 鼠标单击事件
*/
@Override
public void mouseReleased(MouseEvent e)
{
ButtonNum button = (ButtonNum) e.getComponent();
int tag = button.getTAG();
//这里判断就输入的是否是数字,是的话,就调用数字键要调用的方法
if (tag >= 0 && tag <= 9 || tag == 46)
{
this.NumberButton(e, button);
return;
}
//这里判断是否点了清空键,是的话,就调用清空的方法;
else if (tag == 67)
{
this.ClearButton(e);
return;
}
//这里判断是否点了运算符,是的话,就调用运算符执行方法;
else if (tag == 42 || tag == 43 || tag == 45 || tag == 47)
{
this.OperateButton(e, button);
return;
}
//这里判断是否点了等于号;点了就调用运算方法;
else if (tag == 61)
{
this.OperateButton(e, button);
return;
}
//这里判断是否点了正负键,点了就调用正负处理方法;
else if (tag == 400)
{
this.plusAndMinus(e, button);
return;
}
}
/**
*
* @param e
* @param button
* 正负方法;
*/
private void plusAndMinus(MouseEvent e, ButtonNum button)
{
//正负需求,TEXT显示的非空非零;但可以是零点(如:0.)
if (null != Computer.field.getText()
&& 0 != Double.parseDouble(Computer.field.getText()))
{
if ('-' == Computer.field.getText().charAt(0))
{
Computer.field.setText(Computer.field.getText().substring(1));
} else
{
Computer.field.setText('-' + Computer.field.getText());
}
} else
{
boolean b = true;
String str = Computer.field.getText();
for (int i = 0; i < str.length(); i++)
{
if ('.' == str.charAt(i))
{
b = false;
break;
}
}
if (b == false)
{
if ('-' == Computer.field.getText().charAt(0))
{
Computer.field.setText(Computer.field.getText()
.substring(1));
} else
{
Computer.field.setText('-' + Computer.field.getText());
}
return;
}
}
}
/**
*
* @param e
* 数字按钮执行方法
*/
public void NumberButton(MouseEvent e, ButtonNum button)
{
this.OperateValues.setStates(true);
this.numberTwo.setStates(false);
if (iftrue)
{
Computer.field.setText("");
iftrue = false;
Computer.field.setText(String.valueOf(button.getTAG()));
} else
{
if (46 != button.getTAG())
{
boolean b = true;
String str = Computer.field.getText();
for (int i = 0; i < str.length(); i++)
{
if ('.' == str.charAt(i))
{
b = false;
break;
}
}
if (0 != Double.parseDouble(Computer.field.getText())
|| false == b)
{
if (this.iftrue)
{
Computer.field.setText("");
this.iftrue = false;
}
Computer.field.setText(Computer.field.getText() + ""
+ button.getTAG());
return;
} else
{
Computer.field.setText((button.getLabel()));
}
} else
{
if (null != Computer.field.getText())
{
boolean b = true;
String str = Computer.field.getText();
for (int i = 0; i < str.length(); i++)
{
if ('.' == str.charAt(i))
{
b = false;
break;
}
}
if (b)
{
Computer.field.setText(Computer.field.getText() + ""
+ ".");
}
}
}
}
}
/**
*
* @param e
* 归零按钮执行方法
*/
public void ClearButton(MouseEvent e)
{
Computer.field.setText("0");
this.iftrue = false;
this.numberOne.setStates(false);
this.numberTwo.setStates(false);
this.OperateValues.setStates(false);
}
/**
*
* @param e
* 运算符执行方法
*/
public void OperateButton(MouseEvent e, ButtonNum button)
{
iftrue = true;
// 这里判断点击的是不是运算符
if (42 == button.getTAG() || 43 == button.getTAG()
|| 45 == button.getTAG() || 47 == button.getTAG())
{
//从这里开始都是为第一次运算;
// 这里判断值1是否有存值
if (!this.numberOne.getStates())
{
double d = Double.parseDouble(Computer.field.getText());
//把第一个值存起来
this.numberOne.setValues(d);
this.numberOne.setStates(true);
//把运算符存起来
this.OperateValues.setValues(button.getTAG());
//这里改为false,是为了下面的判断;
this.OperateValues.setStates(false);
return;
}
// 这里判断值2是否有存值
else if (this.OperateValues.getStates())
{
if (!this.numberTwo.getStates())
{
double d = Double.parseDouble(Computer.field.getText());
this.numberTwo.setValues(d);
this.numberTwo.setStates(true);
double dou = this.OperateValue((int) this.OperateValues
.getValues());
// 因为是第二次点运算否了,所以调用运算类;
Computer.field.setText(String.valueOf(dou));
this.numberOne.setValues(dou);
this.numberOne.setStates(true);
this.OperateValues.setValues(button.getTAG());
this.OperateValues.setStates(true);
return;
}
}
//从这里结束
// 这里在非第一次点运算否时,执行的代码;
this.OperateValues.setValues(button.getTAG());
this.OperateValues.setStates(true);
}
// 这里判断是不是点了等于号
else if (61 == button.getTAG())
{
if (!this.numberTwo.getStates())
{
this.numberTwo.setValues(Double.parseDouble(Computer.field
.getText()));
this.numberTwo.setStates(true);
}
String sun = String.valueOf(this
.OperateValue((int) this.OperateValues.getValues()));
Computer.field.setText(sun);
this.numberOne.setStates(true);
this.numberOne.setValues(Double.parseDouble(sun));
}
}
/**
*
* @param c
* @return 运算类
*/
public double OperateValue(int c)
{
double value = 0;
switch (c)
{
case 43:
value = this.numberOne.getValues() + this.numberTwo.getValues();
break;
case 45:
value = this.numberOne.getValues() - this.numberTwo.getValues();
break;
case 42:
value = this.numberOne.getValues() * this.numberTwo.getValues();
break;
case 47:
value = this.numberOne.getValues() / this.numberTwo.getValues();
break;
default:
break;
}
return value;
}
}
/**
*
* @author Administrator
*存储数值;
*/
class NumberValues
{
private boolean states;
private double values;
public boolean isStates()
{
return states;
}
public void setStates(boolean states)
{
this.states = states;
}
public boolean getStates()
{
return this.states;
}
public double getValues()
{
return this.values;
}
public void setValues(double values)
{
this.values = values;
}
}
/**
*
* @author Administrator
*界面初始类
*/
class Computer
{
SuperEvent event = new SuperEvent();
private Frame frame;
private JPanel panel;
protected static TextField field;
private ButtonNum key1;
private ButtonNum key0;
private ButtonNum key2;
private ButtonNum key3;
private ButtonNum key4;
private ButtonNum key5;
private ButtonNum key6;
private ButtonNum key7;
private ButtonNum key8;
private ButtonNum key9;
private ButtonNum keyposneg;
private ButtonNum keyplus;
private ButtonNum keycut;
private ButtonNum keyride;
private ButtonNum keybut;
private ButtonNum keyvalue;
private ButtonNum keydecimal;
private ButtonNum keyqikong;
public void go()
{
field = new TextField();
frame = new Frame("计算器");
frame.setSize(150, 150);
panel = new JPanel();
key0 = new ButtonNum("0");
key1 = new ButtonNum("1");
key2 = new ButtonNum("2");
key3 = new ButtonNum("3");
key4 = new ButtonNum("4");
key5 = new ButtonNum("5");
key6 = new ButtonNum("6");
key7 = new ButtonNum("7");
key8 = new ButtonNum("8");
key9 = new ButtonNum("9");
keyposneg = new ButtonNum("+/-");
keyplus = new ButtonNum("+");
keycut = new ButtonNum("-");
keyride = new ButtonNum("*");
keybut = new ButtonNum("/");
keyvalue = new ButtonNum("=");
keydecimal = new ButtonNum(".");
keyqikong = new ButtonNum("C");
frame.add(field, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(5, 4));
panel.setSize(200, 300);
panel.add(key7);
panel.add(key8);
panel.add(key9);
panel.add(keyplus);
panel.add(key4);
panel.add(key5);
panel.add(key6);
panel.add(keycut);
panel.add(key1);
panel.add(key2);
panel.add(key3);
panel.add(keyride);
panel.add(key0);
panel.add(keyposneg);
panel.add(keydecimal);
panel.add(keybut);
panel.add(keyvalue);
panel.add(keyqikong);
key0.setTAG(0);
key1.setTAG(1);
key2.setTAG(2);
key3.setTAG(3);
key4.setTAG(4);
key5.setTAG(5);
key6.setTAG(6);
key7.setTAG(7);
key8.setTAG(8);
key9.setTAG(9);
keyposneg.setTAG(400);
keyplus.setTAG(43);
keycut.setTAG(45);
keyride.setTAG(42);
keybut.setTAG(47);
keyvalue.setTAG(61);
keydecimal.setTAG(46);
keyqikong.setTAG(67);
field.setEnabled(false);
field.setText("0");
frame.setResizable(false);
frame.setVisible(true);
this.eventHandle();
}
/**
* 事件调用的通用方法
*/
public void eventHandle()
{
frame.addWindowListener(event);
key0.addMouseListener(event);
key1.addMouseListener(event);
key2.addMouseListener(event);
key3.addMouseListener(event);
key4.addMouseListener(event);
key5.addMouseListener(event);
key6.addMouseListener(event);
key7.addMouseListener(event);
key8.addMouseListener(event);
key9.addMouseListener(event);
keyposneg.addMouseListener(event);
keyplus.addMouseListener(event);
keycut.addMouseListener(event);
keyride.addMouseListener(event);
keybut.addMouseListener(event);
keyvalue.addMouseListener(event);
keydecimal.addMouseListener(event);
keyqikong.addMouseListener(event);
}
}