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

public class invokelater2 extends JFrame {

  JButton BnStart = new JButton("Start");
  JButton BnESC = new JButton("Abbruch");
  JProgressBar progress = new JProgressBar();

    JLabel label2 = new JLabel("Datei");

    //Frame konstruieren
  public invokelater2() {
    setSize(500, 320);
    setLocation(100,10);
    setTitle("Thread20, Swing und Threads");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    this.getContentPane().setLayout( new GridBagLayout() );

    JLabel label1 = new JLabel("Swing und Threads");
    label1.setFont( new Font("Arial", Font.BOLD,24)  );

    label2.setFont( new Font("Arial", Font.BOLD,24)  );
    
    this.getContentPane().add(label1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(30,20,0,0), 0, 0));

    this.getContentPane().add(label2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(30,20,0,0), 0, 0));

    this.getContentPane().add(progress,
        new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0
        ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
         new Insets(30,10,0,10), 00, 30));


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

    pn.add(BnStart);
    pn.add(BnESC);
    
    this.getContentPane().add(pn,
        new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0
        ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
         new Insets(30,10,0,10), 0, 0));

    BnStart.setFont( new Font("Arial", Font.BOLD,24));

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


    BnESC.setFont( new Font("Arial", Font.BOLD,24));

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

    setVisible(true);
  }

  private void BnESC_Click() {
     System.exit(0);
  }

  private void  BnStart_Click() {
     long summe;
    myThread f1;

    f1 = new myThread(100000, progress, label2);
    f1.start();

  } // Start




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


}



class myThread extends Thread {

  long _anz=0;
  JProgressBar _progress;
  
  JLabel _label;

  public myThread (long anz, JProgressBar progress, JLabel label) {
     _anz = anz;
     _progress = progress;
     _label = label;
  }


  public void run() {
    int time;
    double d;
    int k;
    FileOutputStream fdest;  // File Handler
    String sDest = "d:\\1.bin";
    
    try {
      fdest = new FileOutputStream(sDest);
      DataOutputStream dout = new DataOutputStream(fdest);
      for (int i=0; i<=_anz; i++) {
          time = (int) (Math.random()*30);
          //_anz=0;
          d = (i*100.0)/_anz;
          k = (int) d;
          dout.writeInt(time);
          _progress.setValue(k);
          _label.setText( Integer.toString(time)  );
          Thread.sleep(time);
      }
      fdest.close();
    }
    catch (InterruptedException e) {
        System.out.println("InterruptedException"); 
    }
    
    catch (IOException io)  { 
        System.out.println("IOException"); 
    }      
  }



}

