//Titel:            Dialog-Fenster
//Version:
//Copyright:		Copyright (c) 2010
//Autor:
//Organisation:
//Beschreibung:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class summen_thread_test1 extends JFrame {

  JButton BnOk = new JButton("Test1");
  JButton BnEsc = new JButton("Abbruch");


  private JTextArea memo;

  private long summe;


  public summen_thread_test1() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {
    setSize(400, 430);
    setTitle("JFrame als Dialog");

    BnOk.setFont( new Font("Arial", Font.BOLD,18)  );
    BnEsc.setFont( new Font("Arial", Font.BOLD,18)  );
    BnOk.setPreferredSize( BnEsc.getPreferredSize() );

    this.getContentPane().setLayout( new BorderLayout() );
    memo = new JTextArea();
    memo.setFont( new Font("Arial", Font.BOLD,24)  );
    this.getContentPane().add( new JScrollPane(memo), BorderLayout.CENTER);

	JPanel pnButton = new JPanel();
	//pnButton.setBackground(Color.red);
	pnButton.setLayout( new FlowLayout() );
	pnButton.add(BnOk);
	pnButton.add(BnEsc);

    this.getContentPane().add( pnButton, BorderLayout.SOUTH );

          BnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnOk_Click();
            }
          });


          BnEsc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnEsc_Click();
            }
          });


  } // setGUI

    private void BnOk_Click() {
      summe=0;
      System.out.println("Test1");
      myThread[] ths = new myThread[10];
      for (int i=0; i<ths.length; i++) {
        ths[i] = new myThread(i+1,100000);
      }
      for (int i=0; i<ths.length; i++) {
        ths[i].start();
      }
      try {
	      for (int i=0; i<ths.length; i++) {
    	    ths[i].join();
	      }
      }
      catch (InterruptedException e) {
      }

      memo.setText("Summe: "+Long.toString(summe) );
    }

    private void BnEsc_Click() {
        setVisible(false);
        System.exit(0);
    }


  public static void main(String[] args) {
    summen_thread_test1 frame = new summen_thread_test1();
    frame.setVisible(true);
 }

   class myThread extends Thread {
     int _n;
     int _nr;
   
     public myThread (int nr, int n) {
        _n = n;
        _nr = nr;
     }
   
   
      // Thread Methode
     public void run() {
       for (int i=1; i<=_n; i++) {
         summe=summe+1; // _nr;
       }
     }
   
   
   }
   
   

}  // summen_thread_test1



   /*
         try {
           Thread.sleep( (int) (Math.random()*150) );
         }
         catch (InterruptedException e) {
         }
   */
