主题:请高手帮我个忙,不胜感激~!
期末老师让完成的JAVA试验内容,让给程序的每条语句后加上注释,本来要选个稍微简单的程序,结果都让其他同学给先选了,由于开学初期的两个月有特殊情况忙了一阵子,所以Java几乎没时间去学,结果现在水平很有限,现在一直在弥补落下的课程,真心希望高手帮我这个忙- -
import java.awt.*;
import java.applet.*;
public class Calculator extends Applet
{
private TextField tfAnswer;
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
private Button bPoint,bEqual,bPlus,bMinus,bClear,bMulti,bDivision;
private String OperatorCurrent,OperatorPre;
private String ForeScreen,BackScreen;
private boolean isFloat = false;
public void init()//初始化程序
{
OperatorCurrent = new String("");
OperatorPre = new String("");
ForeScreen = new String("");
BackScreen = new String("");
this.setBackground(Color.green);
this.setForeground(Color.white);
//初始化显示文本框
setLayout(null);
tfAnswer = new TextField();
tfAnswer.setBounds(5,5,100,20);
this.add(tfAnswer);
//初始化文本区域
bClear = new Button("C");
bClear.setBounds(5,30,20,20);
bClear.setBackground(Color.magenta);
//初始化清屏键
this.add(bClear);
bDivision = new Button("/");
bDivision.setBounds(30,30,20,20);
bDivision.setBackground(Color.magenta);
this.add(bDivision);
//初始化除法键
bMulti = new Button("*");
bMulti.setBounds(55,30,20,20);
bMulti.setBackground(Color.magenta);
this.add(bMulti);
//初始化乘法键
bMinus = new Button("-");
bMinus.setBounds(80,30,20,20);
bMinus.setBackground(Color.blue);
this.add(bMinus);
//初始化减法键
b7 = new Button("7");
b7.setBounds(5,55,20,20);
b7.setBackground(Color.magenta);
this.add(b7);
//初始化数字键7
b8 = new Button("8");
b8.setBounds(30,55,20,20);
b8.setBackground(Color.magenta);
this.add(b8);
//初始化数字键8
b9 = new Button("9");
b9.setBounds(55,55,20,20);
b9.setBackground(Color.magenta);
this.add(b9);
//初始化数字键9
b4 = new Button("4");
b4.setBounds(5,80,20,20);
b4.setBackground(Color.magenta);
this.add(b4);
//初始化数字键4
b5 = new Button("5");
b5.setBounds(30,80,20,20);
b5.setBackground(Color.yellow);
this.add(b5);
//初始化数字键5
b6 = new Button("6");
b6.setBounds(55,80,20,20);
b6.setBackground(Color.magenta);
this.add(b6);
//初始化数字键6
bPlus = new Button("+");
bPlus.setBounds(80,55,20,45);
bPlus.setBackground(Color.blue);
this.add(bPlus);
//初始化数字键+
b1 = new Button("1");
b1.setBounds(5,105,20,20);
b1.setBackground(Color.magenta);
this.add(b1);
//初始化数字键1
b2 = new Button("2");
b2.setBounds(30,105,20,20);
b2.setBackground(Color.magenta);
this.add(b2);
//初始化数字键2
b3 = new Button("3");
b3.setBounds(55,105,20,20);
b3.setBackground(Color.magenta);
this.add(b3);
//初始化数字键3
b0 = new Button("0");
b0.setBounds(5,130,45,20);
b0.setBackground(Color.BLUE);
this.add(b0);
//初始化数字键0
bPoint = new Button(".");
bPoint.setBounds(55,130,20,20);
bPoint.setBackground(Color.magenta);
this.add(bPoint);
//初始化数字键.
bEqual = new Button("=");
bEqual.setBounds(80,105,20,45);
bEqual.setBackground(Color.blue);
this.add(bEqual);
//初始化数字键=
}
public boolean action(Event e, Object o)//事件响影
{
String s = new String("");
if(e.target instanceof Button)
{
if((e.target == b0)||(e.target == b1)||(e.target == b2)||
(e.target == b3)||(e.target == b4)||(e.target == b5)||
(e.target == b6)||(e.target == b7)||(e.target == b8)||
(e.target == b9)||(e.target == bPoint))
{
if(e.target != bPoint)
{
s = (String )o;
System.out.print(s);
doForeScreen(s);
//判断是否使用.
}
if((e.target == bPoint)&&(!isFloat))
{
isFloat = true;
s = (String)o;
if(ForeScreen.equals(""))
{
ForeScreen += "0.";
repaint();//在使用"."后显示"0."
}
else
{
System.out.print(s);
doForeScreen(s);
//如果是其它符号直接显示
}
}
}
if(e.target == bClear)
{
doClear();
//清空小屏幕
}
if((e.target == bMulti)||(e.target == bDivision)||
(e.target == bPlus)||(e.target == bMinus))
{
//判断使用的运算符号
if(ForeScreen != "")//判断是否清屏
{
OperatorCurrent = ((String)o);
doOperator();
System.out.print((String)o);
}
else
{
OperatorPre = ((String)o);
}
}
if(e.target == bEqual)
{
doOperator();
}
}
return true;//返回真假值
}
public void doOperator()
{
double dFore,dBack;
Double d;
if(OperatorPre.equals(""))
{
BackScreen = ForeScreen;
ForeScreen = "";
tfAnswer.setText(BackScreen);//运行操作数
repaint();
}
else
{
dFore = (new Double(ForeScreen)).doubleValue();
dBack = (new Double(BackScreen)).doubleValue();
ForeScreen = "";//清屏
BackScreen = tfAnswer.getText();
if(OperatorPre.equals("+"))
{
d = new Double((dBack + dFore));
tfAnswer.setText(d.toString());//加法答案显示
BackScreen = d.toString();
repaint();//绘屏
}
if(OperatorPre.equals("-"))
{
d = new Double((dBack - dFore));
tfAnswer.setText(d.toString());//减法答案显示
BackScreen = d.toString();
repaint();
}
if(OperatorPre.equals("*"))
{
d = new Double((dBack * dFore));
tfAnswer.setText(d.toString());//乘法答案显示
BackScreen = d.toString();
repaint();
}
if(OperatorPre.equals("/"))
{
d = new Double((dBack / dFore));
tfAnswer.setText(d.toString());//除法答案显示
BackScreen = d.toString();
repaint();
}
}
OperatorPre = OperatorCurrent;
repaint();//确定当前操作并赋值
}
public void paint(Graphics g)
{
//tfAnswer.setText(ForeScreen);绘屏函数
}
public void doForeScreen(String s)
{
ForeScreen += s;
tfAnswer.setText(ForeScreen);
repaint();//小屏显示
}
public void doBackScreen(String s)
{
BackScreen = ForeScreen;
ForeScreen = "";//清屏
tfAnswer.setText(ForeScreen);
repaint();
}
public void doClear()//全部清空
{
OperatorCurrent = "";
OperatorPre = "";
ForeScreen = "";
BackScreen = "";
isFloat = false;
tfAnswer.setText(ForeScreen);
repaint();
}
}
import java.awt.*;
import java.applet.*;
public class Calculator extends Applet
{
private TextField tfAnswer;
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
private Button bPoint,bEqual,bPlus,bMinus,bClear,bMulti,bDivision;
private String OperatorCurrent,OperatorPre;
private String ForeScreen,BackScreen;
private boolean isFloat = false;
public void init()//初始化程序
{
OperatorCurrent = new String("");
OperatorPre = new String("");
ForeScreen = new String("");
BackScreen = new String("");
this.setBackground(Color.green);
this.setForeground(Color.white);
//初始化显示文本框
setLayout(null);
tfAnswer = new TextField();
tfAnswer.setBounds(5,5,100,20);
this.add(tfAnswer);
//初始化文本区域
bClear = new Button("C");
bClear.setBounds(5,30,20,20);
bClear.setBackground(Color.magenta);
//初始化清屏键
this.add(bClear);
bDivision = new Button("/");
bDivision.setBounds(30,30,20,20);
bDivision.setBackground(Color.magenta);
this.add(bDivision);
//初始化除法键
bMulti = new Button("*");
bMulti.setBounds(55,30,20,20);
bMulti.setBackground(Color.magenta);
this.add(bMulti);
//初始化乘法键
bMinus = new Button("-");
bMinus.setBounds(80,30,20,20);
bMinus.setBackground(Color.blue);
this.add(bMinus);
//初始化减法键
b7 = new Button("7");
b7.setBounds(5,55,20,20);
b7.setBackground(Color.magenta);
this.add(b7);
//初始化数字键7
b8 = new Button("8");
b8.setBounds(30,55,20,20);
b8.setBackground(Color.magenta);
this.add(b8);
//初始化数字键8
b9 = new Button("9");
b9.setBounds(55,55,20,20);
b9.setBackground(Color.magenta);
this.add(b9);
//初始化数字键9
b4 = new Button("4");
b4.setBounds(5,80,20,20);
b4.setBackground(Color.magenta);
this.add(b4);
//初始化数字键4
b5 = new Button("5");
b5.setBounds(30,80,20,20);
b5.setBackground(Color.yellow);
this.add(b5);
//初始化数字键5
b6 = new Button("6");
b6.setBounds(55,80,20,20);
b6.setBackground(Color.magenta);
this.add(b6);
//初始化数字键6
bPlus = new Button("+");
bPlus.setBounds(80,55,20,45);
bPlus.setBackground(Color.blue);
this.add(bPlus);
//初始化数字键+
b1 = new Button("1");
b1.setBounds(5,105,20,20);
b1.setBackground(Color.magenta);
this.add(b1);
//初始化数字键1
b2 = new Button("2");
b2.setBounds(30,105,20,20);
b2.setBackground(Color.magenta);
this.add(b2);
//初始化数字键2
b3 = new Button("3");
b3.setBounds(55,105,20,20);
b3.setBackground(Color.magenta);
this.add(b3);
//初始化数字键3
b0 = new Button("0");
b0.setBounds(5,130,45,20);
b0.setBackground(Color.BLUE);
this.add(b0);
//初始化数字键0
bPoint = new Button(".");
bPoint.setBounds(55,130,20,20);
bPoint.setBackground(Color.magenta);
this.add(bPoint);
//初始化数字键.
bEqual = new Button("=");
bEqual.setBounds(80,105,20,45);
bEqual.setBackground(Color.blue);
this.add(bEqual);
//初始化数字键=
}
public boolean action(Event e, Object o)//事件响影
{
String s = new String("");
if(e.target instanceof Button)
{
if((e.target == b0)||(e.target == b1)||(e.target == b2)||
(e.target == b3)||(e.target == b4)||(e.target == b5)||
(e.target == b6)||(e.target == b7)||(e.target == b8)||
(e.target == b9)||(e.target == bPoint))
{
if(e.target != bPoint)
{
s = (String )o;
System.out.print(s);
doForeScreen(s);
//判断是否使用.
}
if((e.target == bPoint)&&(!isFloat))
{
isFloat = true;
s = (String)o;
if(ForeScreen.equals(""))
{
ForeScreen += "0.";
repaint();//在使用"."后显示"0."
}
else
{
System.out.print(s);
doForeScreen(s);
//如果是其它符号直接显示
}
}
}
if(e.target == bClear)
{
doClear();
//清空小屏幕
}
if((e.target == bMulti)||(e.target == bDivision)||
(e.target == bPlus)||(e.target == bMinus))
{
//判断使用的运算符号
if(ForeScreen != "")//判断是否清屏
{
OperatorCurrent = ((String)o);
doOperator();
System.out.print((String)o);
}
else
{
OperatorPre = ((String)o);
}
}
if(e.target == bEqual)
{
doOperator();
}
}
return true;//返回真假值
}
public void doOperator()
{
double dFore,dBack;
Double d;
if(OperatorPre.equals(""))
{
BackScreen = ForeScreen;
ForeScreen = "";
tfAnswer.setText(BackScreen);//运行操作数
repaint();
}
else
{
dFore = (new Double(ForeScreen)).doubleValue();
dBack = (new Double(BackScreen)).doubleValue();
ForeScreen = "";//清屏
BackScreen = tfAnswer.getText();
if(OperatorPre.equals("+"))
{
d = new Double((dBack + dFore));
tfAnswer.setText(d.toString());//加法答案显示
BackScreen = d.toString();
repaint();//绘屏
}
if(OperatorPre.equals("-"))
{
d = new Double((dBack - dFore));
tfAnswer.setText(d.toString());//减法答案显示
BackScreen = d.toString();
repaint();
}
if(OperatorPre.equals("*"))
{
d = new Double((dBack * dFore));
tfAnswer.setText(d.toString());//乘法答案显示
BackScreen = d.toString();
repaint();
}
if(OperatorPre.equals("/"))
{
d = new Double((dBack / dFore));
tfAnswer.setText(d.toString());//除法答案显示
BackScreen = d.toString();
repaint();
}
}
OperatorPre = OperatorCurrent;
repaint();//确定当前操作并赋值
}
public void paint(Graphics g)
{
//tfAnswer.setText(ForeScreen);绘屏函数
}
public void doForeScreen(String s)
{
ForeScreen += s;
tfAnswer.setText(ForeScreen);
repaint();//小屏显示
}
public void doBackScreen(String s)
{
BackScreen = ForeScreen;
ForeScreen = "";//清屏
tfAnswer.setText(ForeScreen);
repaint();
}
public void doClear()//全部清空
{
OperatorCurrent = "";
OperatorPre = "";
ForeScreen = "";
BackScreen = "";
isFloat = false;
tfAnswer.setText(ForeScreen);
repaint();
}
}