Windows for computing in Task 2 of week 10 on java

Source: Internet
Author: User

[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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.