| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 587 人关注过本帖
标题:看一个小程序但是不懂,请大家指教
只看楼主 加入收藏
中国
Rank: 1
等 级:新手上路
帖 子:218
专家分:0
注 册:2009-1-4
结帖率:90.91%
收藏
已结贴  问题点数:30 回复次数:2 
看一个小程序但是不懂,请大家指教
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bPoint = new Button(".");
bAdd = new Button("+");
bDec = new Button("-");
bMul = new Button("*");
bDiv = new Button("/");
bCal = new Button("=");
b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
b2.addActionListener(new setLabelText_ActionListener());
b3.addActionListener(new setLabelText_ActionListener());
b4.addActionListener(new setLabelText_ActionListener());
b5.addActionListener(new setLabelText_ActionListener());
b6.addActionListener(new setLabelText_ActionListener());
b7.addActionListener(new setLabelText_ActionListener());
b8.addActionListener(new setLabelText_ActionListener());
b9.addActionListener(new setLabelText_ActionListener());
bPoint.addActionListener(new setLabelText_ActionListener());
bAdd.addActionListener(new setOperator_ActionListener());
bDec.addActionListener(new setOperator_ActionListener());
bMul.addActionListener(new setOperator_ActionListener());
bDiv.addActionListener(new setOperator_ActionListener());
bCal.addActionListener(new setOperator_ActionListener());

class setLabelText_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button tempB = (Button)e.getSource(); //e.getSource();这个函数是什么意思?我可以理解我我按了一个数字比如6,然后就把6付给tempB这个按钮吗?
s = tempB.getLabel();
setTextFieldText(); 按3就显示3的函数
}
}

class setOperator_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button tempB = (Button)e.getSource();
op = tempB.getLabel();
if(op.equals("+")){
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
}else if(op.equals("-")){
tf.setText(cal.opSubtract(tf.getText()));
ifOp = true;
}else if(op.equals("*")){
tf.setText(cal.opMultiply(tf.getText()));
ifOp = true;
}else if(op.equals("/")){
tf.setText(cal.opDivide(tf.getText()));
ifOp = true;
}else if(op.equals("=")){
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
}
}
}
}
private void operate(String x){
double x1 = stringToDouble(x); //把字符串转换成字符的函数
double y = stringToDouble(result);
switch (op){                  //op的初始值为0
case 0:
result = x;
break;
case 1:
result = String.valueOf(y+x1);
break;
case 2:
result = String.valueOf(y-x1);
break;
case 3:
result = String.valueOf(y*x1);
break;
case 4:
if(x1!=0){
result = String.valueOf(y/x1);
}
else{
result = "The divisor can't be zero!";
}
break;
}
}
public String opAdd(String x){
operate(x);
op = add;
return result;
}
我截取了这三段程序提个问题  例如在计算器上要算3+5
当在计算器上按3的时候就显示3当按+的时候就执行setOperator_ActionListener 大家可以沿着函数看
在opAdd函数中只执行了一遍operate(x)就我理解执行完这里result就是3再执行下去result也还是3啊怎么会是8呢?应该有两个operate才对啊?????
我不知道说清楚没有,希望大家看看到底怎么出的结果啊。程序中其它部分与计算无关就不写出来了
搜索更多相关主题的帖子: java getSource 计算 
2009-08-15 12:14
zcc8607
Rank: 2
等 级:论坛游民
帖 子:21
专家分:19
注 册:2008-5-19
收藏
得分:15 
确实没说清楚 起码加些标点啊
2009-08-19 17:18
michael_cc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:25
专家分:105
注 册:2009-8-24
收藏
得分:15 
e.getSource();这个函数是得到你点击按钮对象的引用,通过这个引用来调用方法来得到该按钮上的标签。
2009-08-24 17:25
快速回复:看一个小程序但是不懂,请大家指教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015344 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved