import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class CalculatorDemo extends JFrame{
/**
* @param args
*/
JTextField answerText;
Button pointButton,equalButton,plusButton,minusButton,clearButton,multiButton,divisionButton;
Button[] b=new Button[10];
String currentOp ,preOp;
String foreText,backText;
boolean isFloat=false;
public CalculatorDemo()
{ super("计算器");
setSize(400,400);
Container container=getContentPane();
setVisible(true);
Panel panel1=new Panel();
Panel panel2=new Panel();
Panel panel3=new Panel();
currentOp=new String("");
preOp=new String("");
foreText=new String("");
backText=new String("");
answerText=new JTextField(8);
container.setBackground(Color.lightGray);
//container.setForeground(Color.blue);
for(int i=9;i>=0;i--)
{
b[i]=new Button(Integer.toString(i));
panel2.add(b[i]);
}
pointButton=new Button(".");
equalButton=new Button("=");
clearButton=new Button("清除");
clearButton.setForeground(Color.red);
divisionButton=new Button("/");
divisionButton.setForeground(Color.red);
multiButton=new Button("*");
plusButton=new Button("+");
plusButton.setForeground(Color.red);
minusButton=new Button("-");
minusButton.setForeground(Color.red);
container.setLayout(new FlowLayout());
panel1.setLayout(new FlowLayout());
panel2.setLayout(new GridLayout(4,3));
panel3.setLayout(new GridLayout(4,1));
panel1.add(answerText);
panel1.add(clearButton);
panel2.add(pointButton);
panel2.add(equalButton);
panel3.add(plusButton);
panel3.add(minusButton);
panel3.add(multiButton);
panel3.add(divisionButton);
container.add(panel1);
container.add(panel2);
container.add(panel3);
}
/*public boolean action(Event e,Object o)
{
String s=new String("");
for(int i=0;i<10;i++)
{
if(e.target==b[i]||e.target==pointButton)
{
if(e.target!=pointButton)
{
s=(String)o;
doForeText(s);
}
if((e.target==pointButton)&&(!isFloat))
{
isFloat=true;
s=(String)o;
if(foreText.equals(""))
{
foreText+="0.";
}
else
{
doForeText(s);
}
}
}
}
if(e.target==clearButton)
{
doClear();
}
if((e.target==multiButton)||(e.target==divisionButton)||(e.target==plusButton)||(e.target==minusButton))
{
if(foreText!="")
{
currentOp=(String)o;
doOprator();
}
else
{
preOp=(String)o;
}
}
if(e.target==equalButton)
{
doOprator();
}
return true;
}
public void doOprator()
{
double dFore,dBack;
Double d;
if(preOp.equals(""))
{
backText=foreText;
foreText="";
answerText.setText(backText);
}
else
{
dFore=(new Double(foreText)).doubleValue();
dBack=(new Double(backText)).doubleValue();
foreText="";
backText=answerText.getText();
if(preOp.equals("+"))
{
d=new Double((dBack+dFore));
answerText.setText(d.toString());
backText=d.toString();
}
if(preOp.equals("-"))
{
d=new Double((dBack-dFore));
answerText.setText(d.toString());
backText=d.toString();
}
if(preOp.equals("*"))
{
d=new Double((dBack*dFore));
answerText.setText(d.toString());
backText=d.toString();
}
if(preOp.equals("/"))
{ if(dFore==0)
{
answerText.setText("除数不能为0");
return;
}
d=new Double((dBack/dFore));
answerText.setText(d.toString());
backText=d.toString();
}
}
preOp=currentOp;
}
public void doForeText(String s)
{
foreText+=s;
answerText.setText(foreText);
}
public void doBackText(String s)
{
backText=foreText;
foreText="";
answerText.setText(foreText);
}
public void doClear()
{
currentOp="";
preOp="";
foreText="";
backText="";
isFloat=false;
answerText.setText("");
}*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CalculatorDemo c=new CalculatorDemo();
}
}