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

// vier Threads, aber Aufruf RUN

public class Thread04 extends JFrame {

  JButton BnStart = new JButton("Start");
  JLabel label;

  //Frame konstruieren
  public Thread04() {
    setSize(500, 320);
    setLocation(100,10);
    setTitle("TestListe4");

    label = new JLabel();
    label.setFont( new Font("Arial", Font.BOLD,32));
    label.setText( "0" );

    this.getContentPane().add(label, BorderLayout.CENTER);

    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    JPanel pn = new JPanel();
    pn.setLayout( new FlowLayout() );

    pn.add(BnStart);
    this.getContentPane().add(pn, BorderLayout.SOUTH);
    BnStart.setFont( new Font("Arial", Font.BOLD,32));

    BnStart.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnStart_Click();
      }
    });
    setVisible(true);
  }

    private void  BnStart_Click() {
     long summe;
      myThread f1,f2, f3,f4;
      f1 = new myThread(1, label);
      f2 = new myThread(2, label);
      f3 = new myThread(3, label);
      f4 = new myThread(4, label);
      label.setText("0");
      f1.run();
      f2.run();
      f3.run();
      f4.run();
    } // BnStart_Click


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


}



class myThread extends Thread {

  long _nr=0;
  JLabel _mylabel;

  public myThread (long nr, JLabel label) {
     _mylabel = label;
     _nr = nr;
  }


  // lokale Summe
  public void run() {
    String s;
    long summe;
    for (int i=1; i<=10; i++) {
      s = _mylabel.getText();
      summe = Long.valueOf(s).longValue();
      summe = summe + _nr;
      _mylabel.setText( Long.toString(summe)  );
      System.out.println("Nr: "+_nr+"  Summe: "+summe);
      delay(40);
    }
  }

  void delay(long time) {
    try {
        Thread.sleep(time);
      }
      catch (InterruptedException e) {
      }
  }
}


