ò???Javaêμ??μ??????÷D?3ìDò
ê±??:2011-01-22
/**
@author Song Liang Peng
@version 2005.7.23
*/
import java.awt.*;
import java.awt.event.*;
public class Counter extends Frame implements ActionListener
{
TextField t=new TextField("",15);
Panel p1=new Panel();
Panel p2=new Panel();
Button[] b=new Button[10];
Button bAdd=new Button("+");
Button bSub=new Button("-");
Button bMul=new Button("*");
Button bDiv=new Button("/");
Button bPoint=new Button(".");
Button bEqual=new Button("=");
Button bSqrt=new Button("?a??·?");
Button bNull=new Button("????");
String str1="";???? //str1oístr2′?·?á???ê?è?μ?êy
String str2="";
String operator=null;?? //′?·??ó??3?3yò??°?a??·?μ?·?o?
boolean first=true;????//?ì?éê?è?μ?ê?·??aμúò???êy
int countOper=0;????//à???ê?è?·?o?μ???êy£?á??óá???μè2ù×÷?D?áó?μ?
double result=0.0;????//?Y′??á1?
double num1=0.0,num2=0.0;??//á???ê?è?μ?êy×?????ê±×a?ˉ?adouble′?·?
boolean error=false;?? //?ì?é3yêyê?·??a0
//11?ì·?·¨
public Counter()
{
??super("counter");
??t.setEditable(false);
for(int i=0;i<b.length;i++)
??{
?? b[i]=new Button(String.valueOf(i));
?? p1.add(b[i]);
?? b[i].setActionCommand("number");
?? b[i].addActionListener(this);
??}
??p1.add(bPoint);
??bPoint.setActionCommand("number");
??p1.add(bAdd);??????//êy×??ü£?·?o??ü·????úpanelμ?p1?D
??p1.add(bSub);
??p1.add(bMul);
??p1.add(bDiv);
??p1.add(bEqual);
??p2.add(bSqrt);??????//?a??·?oí?????ü·????úpanelμ?p2?D
??p2.add(bNull);
??bAdd.setActionCommand("oper");
??bSub.setActionCommand("oper");
??bMul.setActionCommand("oper");
??bDiv.setActionCommand("oper");
bAdd.addActionListener(this);
??bSub.addActionListener(this);
??bMul.addActionListener(this);
??bDiv.addActionListener(this);
??bPoint.addActionListener(this);
??bEqual.addActionListener(this);
??bSqrt.addActionListener(this);
??bNull.addActionListener(this);
p1.setLayout(new GridLayout(4,4,5,5));
??p2.setLayout(new FlowLayout());
??add(t,"North");??????//frameμ?north·???ê?è??ò£?panel·????úcenteroísouth
??add(p1,"Center");
??add(p2,"South");
??setLocation(400,200);
??setSize(200,200);
??setBackground(Color.red);
??setVisible(true);
addWindowListener(new WindowAdapter(){?? //1?±?′°?ú
?? public void windowClosing(WindowEvent e)
?? {
????System.exit(0);
?? }
??});
}
//êμ???ó?úActionListener
public void actionPerformed(ActionEvent e)
{
??Button temp=(Button)e.getSource();
if(e.getActionCommand().equals("number"))
??{
?? if(first)
?? {
????str1=str1+temp.getLabel();
????t.setText(str1);
?? }
?? else
?? {
????str2=str2+temp.getLabel();
????t.setText(str2);
?? }
??}
??else if(e.getActionCommand().equals("oper"))
??{
?? if(str1=="")????//è?1??1??óDê?è?êy?íμ??÷????·??′DDif
|