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

// Beispiel mit zwei Thread als JFrame
// Summen werden aber parallel ausgeführt

// Thread-Objekte sind in den Unterframes


public class Thread11a extends JFrame {

  JButton BnStart = new JButton("Start");

  //Frame konstruieren
  public Thread11a() {
    setSize(400, 200);
    setTitle("Thread12");
    setLocation(150,200);

    JLabel label1 = new JLabel("Main-Fenster");
    label1.setFont( new Font("Arial",0,36) );


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

    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();
      }
    });

  }

  private void  BnStart_Click() {
  // Thread variablen definieren
    myThread f1,f2;
    // Threads erzeugen
    f1 = new myThread(1);
    f2 = new myThread(2);
    // Frames anzeigen
    f1.setVisible(true);
    f2.setVisible(true);
    // Threads werden im Konstruktor gestartet
  } // 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) {
    Thread11a frame = new Thread11a();
    frame.setVisible(true);
  }


}



// Testklasse
class myThread extends JFrame implements Runnable {

  JLabel _label;

  public myThread (int nr) {
    _label = new JLabel();
    _label.setFont( new Font("Arial", Font.BOLD,32));
    _label.setText( "0" );

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

    setSize(200,100);
    switch (nr) {
      case 1: setLocation(100,400);
              break;
      case 2: setLocation(400,400);
              break;
      default: setLocation(300,300);
    }
    Thread cpu;
    cpu = new Thread(this);  // Thread wird erzeugt
    cpu.start();             // Thread wird gestartet
  } // create



  // Thread-Methode
  public void run() {
    for (int i=0; i<=100; i++) {
      _label.setText( Integer.toString(i) );
        try {
           Thread.sleep(50);
        } catch (InterruptedException e) {
        }
    }
  }


}  // myThread

