62,616
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class love {
public static void main(String args[]) {
WindowFrame s = new WindowFrame("计算的窗口");
}
}
class WindowFrame extends Frame implements TextListener, ActionListener {
float aver, sum = 0;
TextArea a, b;
Button button1,button2;
WindowFrame(String s) {
super(s);
a = new TextArea(15, 16);
b = new TextArea(15, 45);
button1 = new Button("退出");
button2 = new Button("分析");
add(button1);
add(button2);
setLayout(new FlowLayout());
add(a);
add(b);
button1.addActionListener(this);
button2.addActionListener(this);
//a.addTextListener(this);
setVisible(true);
validate();
setBounds(0, 0, 345, 600);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(button2)){
try {
String s;
s = a.getText();
sum = 0;
StringTokenizer fenxi = new StringTokenizer(s, " ,'\n'");
int n = fenxi.countTokens();
String c[] = new String[n];
for (int i = 0; i <= n - 1; i++) {
String temp = fenxi.nextToken();
System.out.println(temp);
c[i] = temp;
}
for (int j = 0; j < n; j++) {
sum = sum + Float.parseFloat(c[j]);
aver = sum / n;
b.setText("你输入的数的和sum=" + sum + '\n' + "你输入的数的平均数aver=" + aver);
}
}
catch (NumberFormatException ee) {
}
}
else if (e.getSource().equals(button1)){
System.exit(0);
}
}
public void textValueChanged(TextEvent e) {
try {
String s;
s = a.getText();
StringTokenizer fenxi = new StringTokenizer(s, " ,'\n'");
int n = fenxi.countTokens();
String c[] = new String[n];
for (int i = 0; i <= n - 1; i++) {
String temp = fenxi.nextToken();
System.out.println(temp);
c[i] = temp;
}
for (int j = 0; j < n; j++) {
sum = sum + Float.parseFloat(c[j]);
aver = sum / n;
b.setText("你输入的数的和sum=" + sum + '\n' + "你输入的数的平均数aver=" + aver);
}
}
catch (NumberFormatException ee) {
}
}
}