import java.awt.*;
import java.util.*;
import java.awt.event.*;
class FrameWithTextArea extends Frame{
TextArea textarea1=new TextArea("please enter some numbers."),
textarea2=new TextArea("the rusult is here.");
FlowLayout flow1=new FlowLayout();
int sum1=0,n=0;
FrameWithTextArea(String s){
super(s);
setLayout(flow1);
textarea1.addTextListener(new TextListener(){
public void textValueChanged(TextEvent e1){
String str1=textarea1.getText();
StringTokenizer fenxi1=new StringTokenizer(str1,",'/n' ");
try{
sum1=0;
n=fenxi1.countTokens();
int[] numbers=new int[n];
for(int i=0;i<n;i++){
int temp=Integer.parseInt(fenxi1.nextToken());
numbers[i]=temp;
sum1=sum1+numbers[i];
}
textarea2.setText(Integer.toString(sum1));
}
catch(NumberFormatException e2){
textarea2.setText("NumberFormatException e2 found");
}
}
});
add(textarea1);
add(textarea2);
setBounds(200,200,600,480);
setVisible(true);
validate();
}
}
public class p211_4{
public static void main(String args[]){
FrameWithTextArea fwta1=new FrameWithTextArea("A Fram with TextArea");
}
}<PIXTEL_MMI_EBOOK_2005>2 </PIXTEL_MMI_EBOOK_2005>
java程序设计基础教程第二版: p211_4
最新推荐文章于 2024-03-03 18:14:40 发布