|
编写一个一个应用程序,在应用程序中,而两个文本框,
一个输入数字,然后分解每个位数字,另一个文本框中会显示数字的平均值- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.util.*;
- public class p1{
- public static void main(String args[]){
- Mywindow win=new Mywindow();
-
- }
- }
- class Mywindow extends JFrame implements ActionListener{
- JTextField text1,text2;
- Mywindow(){
- text1=new JTextField(15);
- text2=new JTextField(15);
- text1.addActionListener(this);
- setLayout(new FlowLayout());
- add(text1);
- add(text2);
- setBounds(100,100,150,150);
- setVisible(true);
- validate();
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
-
- }
- public void actionPerformed(ActionEvent e){
- StringTokenizer fenxi;
- double sum=0,n,i,ave;
-
- String s=text1.getText();
- fenxi=new StringTokenizer(s);
- for(i=0;fenxi.hasMoreTokens();i++){
- String str=fenxi.nextToken();
- n=Double.parseDouble(str);
- sum+=n;
- }
- ave=(Double)(sum/i);
- text2.setText("和为:"+sum+"平均值为:"+ave);
-
- }
- }
复制代码 |
|