java高手解释下

import java.util.*;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Example5_9 extends Applet implements ActionListener{
JTextArea textA; JTextField textF; JButton b1,b2;
public void init(){
setSize(250,150);
textA = new JTextArea("",5,10); textA.setBackground(Color.cyan);
textF = new JTextField("",10); textF.setBackground(Color.red); textF.setEditable(false);
b1 =new JButton("求和"); b2 =new JButton("重新开始");
add(textA);add(textF);add(b1);add(b2);
}
public void actionPerFormed(ActionEvent e){
if(e.getSource() == b1){
Sring s = textA.getText();StringTokenizer tokens = new StringTokenizer(s);
int n = tokens.countTokens(),sum = 0, i;//这行代码的意思
for(i=0;i<=n-1;i++){
String temp = tokens.nextToken();
sum += Integer.parseInt(temp);
}
textF.setText("" +sum);
}
else if (e.getSource() == b2){
textA.setText(null);
textF.setText(null);
}
}
}

这是一种代码简写,分开就是:
int n=tokens.countTokens();
int sum=0;
int i;
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-23
b1 =new JButton("求和"); b2 =new JButton("重新开始");
这两个按钮没有添加listener,点击后应该没有什么响应。。。
。所以加个监听事件就可以了。
b1.addActionListener(this)追问

没看到,呵呵。int n = tokens.countTokens(),sum = 0, i;//这行代码的意思?

追答

b1.addActionListener(this)

第2个回答  2011-11-23
public StringTokenizer(String str)
为指定字符串构造一个 string tokenizer。tokenizer 使用默认的分隔符集 " \t\n\r\f",即:空白字符、制表符、换行符、回车符和换页符。分隔符字符本身不作为标记。
例如将
“123 456 789”
分成三个字符串
“123”“456”“789”
相似回答