import java.awt.*;
import java.awt.event.*;
import java.util.*;
class wenben extends Frame implements TextListener,ActionListener{
Button butExit = null;
TextArea text1 = null; //輸入文件域
TextArea text2 = null; //結果輸出的文件域
public wenben(){
butExit = new Button();
add(butExit);
butExit.addActionListener(this);
setLayout(null); //設置界面的格式
setBounds(35,35,280,250);
butExit.setBounds(50,200,180,20);
butExit.setLabel("離開");
text1 = new TextArea(" ",10,10);
text1.setBounds(40,50,100,100);
text2 = new TextArea(" ",10,10);
text2.setBounds(150,50,100,100);
add(text1);
text1.addTextListener(this);
add(text2);
text2.setEditable(false);
text2.addTextListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
System.exit(-1);
}
public void textValueChanged(TextEvent e)
{
double averge = 0; //均值
double result = 0; // 和
int i = 0;
if(e.getSource()==text1)
{
String str = text1.getText();
StringTokenizer detect = new StringTokenizer(str); //stringtokenizer很用的類,對字符的處理功能強大
int n = detect.countTokens();
double[] dou = new double[n]; //也可分配一靜態的數組,這樣不用每次分配空間,但是數組的長度的變化缺少靈活性
for(i = 0;i<n; i++)
{
String temp = detect.nextToken();
dou[i]=Double.valueOf(temp).doubleValue();
result = result+dou[i];
}
text2.setText(""+result+'\n'+(result/i));
}
}
public static void main(String[] args){
wenben wb = new wenben();
}
}
import java.awt.event.*;
import java.util.*;
class wenben extends Frame implements TextListener,ActionListener{
Button butExit = null;
TextArea text1 = null; //輸入文件域
TextArea text2 = null; //結果輸出的文件域
public wenben(){
butExit = new Button();
add(butExit);
butExit.addActionListener(this);
setLayout(null); //設置界面的格式
setBounds(35,35,280,250);
butExit.setBounds(50,200,180,20);
butExit.setLabel("離開");
text1 = new TextArea(" ",10,10);
text1.setBounds(40,50,100,100);
text2 = new TextArea(" ",10,10);
text2.setBounds(150,50,100,100);
add(text1);
text1.addTextListener(this);
add(text2);
text2.setEditable(false);
text2.addTextListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
System.exit(-1);
}
public void textValueChanged(TextEvent e)
{
double averge = 0; //均值
double result = 0; // 和
int i = 0;
if(e.getSource()==text1)
{
String str = text1.getText();
StringTokenizer detect = new StringTokenizer(str); //stringtokenizer很用的類,對字符的處理功能強大
int n = detect.countTokens();
double[] dou = new double[n]; //也可分配一靜態的數組,這樣不用每次分配空間,但是數組的長度的變化缺少靈活性
for(i = 0;i<n; i++)
{
String temp = detect.nextToken();
dou[i]=Double.valueOf(temp).doubleValue();
result = result+dou[i];
}
text2.setText(""+result+'\n'+(result/i));
}
}
public static void main(String[] args){
wenben wb = new wenben();
}
}