[Java]
/*
* Start the program header annotation.
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: calculated window
* Author: Xue Guangchen
* Completion date: January 1, November 05, 2012
* Version No.: x1.0
* Description of tasks and Solutions
* Input description:
* Problem description: compile a JFrame with the title "computing window". In this window, the component layout is FlowLayout. Add two partition areas in the window. When we enter a number in a partition,
The other worker area calculates the sum of the input number and calculates the average value. That is to say, as the input changes, the other worker area constantly updates the sum and average value.
* Program output:
* End the comment in the program Header
*/
Package xue;
Import java. awt. event .*;
Import java. text. DecimalFormat;
Import java. util .*;
Import java. awt .*;
Import javax. swing. JFrame;
Public class MyJFrame extends JFrame implements TextListener, ActionListener {
TextArea text1;
TextArea text2;
Button buttonClear;
MyJFrame ()
{
SetLayout (new FlowLayout ());
SetTitle ("computing window ");
SetSize );
SetVisible (true );
Text1 = new TextArea (6, 15 );
Text2 = new TextArea (6, 15 );
ButtonClear = new Button ("clear ");
Add (text1 );
Add (text2 );
Add (buttonClear );
Text2.setEditable (false );
Text1.addTextListener (this );
ButtonClear. addActionListener (this );
SetBounds (100,100,350,160 );
SetVisible (true );
Validate ();
}
Public void actionreceivmed (ActionEvent e ){
Text1.setText (null );
}
Public void textValueChanged (TextEvent e ){
String s = text1.getText ();
StringTokenizer fenxi = new StringTokenizer (s, ", '\ N '");
Int n = fenxi. countTokens ();
String a [] = new String [n];
For (int I = 0; I <n; I ++)
{
String temp = fenxi. nextToken ();
A [I] = temp;
}
Double addResult = 0;
Double aveResult = 0;
For (int I = 0; I <n; I ++)
{
AddResult + = Double. valueOf (a [I]);
}
If (n! = 0)
{
AveResult = addResult/n;
}
Text2.setText (null );
Text2.append (n + "sum of numbers:" + addResult + "\ n ");
DecimalFormat d = new DecimalFormat ("#. 000 ");
Text2.append ("average value:" + d. format (aveResult) + "\ n ");
}
}
Running result: