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

// Ein Dialog mit zwei Labels, die hochgezählt werden

public class Thread03a extends JFrame {


  //Frame konstruieren
  public Thread03a() {
     JLabel label1;
     JLabel label2;

    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    setSize(400, 320);
    this.setLocation(100, 100);
    setTitle("Thread Thread03");

    label1 = new JLabel("1. Label");
    label2 = new JLabel("2. Label");
    label1.setFont( new Font("Arial", Font.BOLD,32));
    label2.setFont( new Font("Arial", Font.BOLD,32));


    this.getContentPane().add(label1, BorderLayout.NORTH);
    this.getContentPane().add(label2, BorderLayout.SOUTH);

    setVisible(true);

    MyThread f1,f2;

    f1 = new MyThread(label1);
    f2 = new MyThread(label2);
    f1.start();
    f2.start();
  } // create




  //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }


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


}


class MyThread extends Thread {

  JLabel _myLabel;

  // Übergabe "globales JLabel"
  public MyThread (JLabel label) {
     _myLabel = label;
     _myLabel.setText( "Thread-Label" );
  }


   // Thread Methode
  public void run() {
    for (int i=1; i<=100; i++) {
      _myLabel.setText( Integer.toString(i) );
      try {
        Thread.sleep( (int) (Math.random()*150) );
      }
      catch (InterruptedException e) {
      }
    }
  }


}

