//Titel:            Testbeispiel für MDI-Fenster

// Beispiel eines MDI-Fenster

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

// Packages für Opendialog
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.*;
import java.util.Hashtable;
import java.util.Enumeration;

// Packages für Filter
import javax.swing.filechooser.*;
import java.io.File;
import java.util.Hashtable;
import java.util.Enumeration;




public class MDI04 extends JFrame  implements ActionListener  {

  // Container für die Clientfenster
  private JDesktopPane desktopPane = new JDesktopPane();

    // Variablen für die Menüs
  private JMenuBar menuBar1;   // MenüContainer
  private JMenu mainFile;      // Hauptmenü
  private JMenuItem mnNew;     // Menüeintrag
  private JMenuItem mnOpen;    // Menüeintrag
  private JMenuItem mnSave;    // Menüeintrag
  private JMenuItem mnSaveAs;    // Menüeintrag
  private JMenuItem mnClose;   // Menüeintrag

  private JMenu mainEdit;         // HauptMenü
  private JMenuItem mnCut;        // Menüeintrag
  private JMenuItem mnCopy;       // Menüeintrag
  private JMenuItem mnInsert;     // Menüeintrag

  private JMenu mainWindows;        // HauptMenü
  private JMenuItem mnPrevWindows;  // Menüeintrag
  private JMenuItem mnNextWindows;  // Menüeintrag
  private JMenuItem mnTile;         // Menüeintrag
  private JMenuItem mnCascade;      // Menüeintrag
  private JMenuItem mnTileVertical;        
  private JMenuItem mnTileHorizontal;        

  JButton bnOpen = new JButton("Öffnen");
  JButton bnNew = new JButton("Neu");


  String _sFileName="";


   // Konstruktor
  public MDI04() {
    this.setSize( 600, 400);
    setTitle( "MDI: 2. Beispiel");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  public void setGUI() {
      // BorderLayout setzen
    this.getContentPane().setLayout( new BorderLayout() );

      // Variable DesktopPane einfügen
      // in ihr werden alle Clientfenster eingefügt
    JScrollPane sc = new JScrollPane (desktopPane);
    this.getContentPane().add(sc, BorderLayout.CENTER);
    setMenus();
    setToolBars();
  }



    // setzt die Menüs
  private void setMenus() {
    menuBar1 = new JMenuBar();

      // DATEI
    mainFile = new JMenu("Datei");
    mnNew = new JMenuItem("Neu");
    mnOpen = new JMenuItem("Öffnen");
    mnSave = new JMenuItem("Speichern");
    mnSaveAs = new JMenuItem("Speichern Als");
    mnClose = new JMenuItem("Schließen");

      // Event definieren
   mnNew.addActionListener(this);
      // Short Key definieren  STRG+N
    mnNew.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
    mainFile.add(mnNew);

      // Event definieren
    mnOpen.addActionListener(this);
      // Short Key definieren  STRG+O
    mnOpen.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
     // Menüeinträge zum mainFile hinzufügen
    mainFile.add(mnOpen);

    mainFile.addSeparator();

      // Event definieren
    mnSave.addActionListener(this);
      // Short Key definieren  STRG+O
    mnSave.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
     // Menüeinträge zum mainFile hinzufügen
    mainFile.add(mnSave);

      // Event definieren
    mnSaveAs.addActionListener(this);
     // Menüeinträge zum mainFile hinzufügen
    mainFile.add(mnSaveAs);
    mainFile.addSeparator();



    // --------------------------
      // Event definieren
    mnClose.addActionListener(this);
    mainFile.add(mnClose);

      // Hauptmenü FILE in den MenüBar einfügen
    menuBar1.add(mainFile);



    // Menü EDIT
    mainEdit = new JMenu("Bearbeiten");
    mnCut = new JMenuItem("Ausschneiden");
    mnCopy = new JMenuItem("Kopieren");
    mnInsert = new JMenuItem("Einfügen");

      // Event definieren
    mnCut.addActionListener(this);
    mainEdit.add(mnCut);

    mnCopy.addActionListener(this);
    mainEdit.add(mnCopy);
    // mainFile.addSeparator();

    // --------------------------
      // Event definieren
    mnInsert.addActionListener(this);
    mainEdit.add(mnInsert);
    menuBar1.add(mainEdit);

    // ----------------
    // Menü Windows
    mainWindows = new JMenu("Fenster");
    mnPrevWindows = new JMenuItem("Vorheriges Fenster");
    mnNextWindows = new JMenuItem("Nächstes Fenster");
    mnCascade = new JMenuItem("Kaskade");
    mnCascade.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0));

    mnTile = new JMenuItem("Aufteilen");
    mnTile.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0));
    mnTileVertical = new JMenuItem("Aufteilen Vertikal");
    mnTileVertical.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0));

    mnTileHorizontal = new JMenuItem("Aufteilen Horizontal");
    mnTileHorizontal.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0));


    mnPrevWindows.addActionListener(this);
    mnNextWindows.addActionListener(this);
    mnCascade.addActionListener(this);
    mnTile.addActionListener(this);
    mnTileVertical.addActionListener(this);
    mnTileHorizontal.addActionListener(this);

      // MenüEinträge definieren
    mainWindows.add(mnPrevWindows);
    mainWindows.add(mnNextWindows);
    mainWindows.addSeparator();
    mainWindows.add(mnCascade);
    mainWindows.add(mnTile);
    mainWindows.add(mnTileVertical);
    mainWindows.add(mnTileHorizontal);
    mnTileHorizontal.setActionCommand("TileHorizontal");
    menuBar1.add(mainWindows);

    this.setJMenuBar(menuBar1);

    for (int i = 0; i < menuBar1.getComponentCount(); i++) {
      JMenu main = (JMenu) menuBar1.getComponent(i);
      main.setFont(new Font("Arial", Font.BOLD, 18));
      for (int j = 0; j < main.getItemCount(); j++) {
        Component c = main.getItem(j);
        if (c instanceof JMenuItem) {
          JMenuItem mn = (JMenuItem) c;
          mn.setFont(new Font("Arial", Font.BOLD, 18));
        }
      }
    }

  } // setMenus


  private void setToolBars() {
    JToolBar toolBar = new JToolBar();

    toolBar.add(bnOpen);
    bnOpen.addActionListener(this);

    bnNew.addActionListener(this);
    toolBar.add(bnNew);
    this.getContentPane().add(toolBar, BorderLayout.NORTH);
    bnOpen.setFont(new Font("Arial", Font.BOLD, 18));
    bnNew.setFont(new Font("Arial", Font.BOLD, 18));
  } // setToolBars


  public void actionPerformed(ActionEvent e) {
       if (  (e.getSource()==mnNew) || (e.getSource()==bnNew) ) {
            MyInternalFrame frame = new MyInternalFrame(null, desktopPane.getAllFrames().length+1);
            int pos = desktopPane.getAllFrames().length+1;
            frame.setBounds(pos*10, pos*10, 450, 300);
            desktopPane.add(frame);
            desktopPane.moveToFront(frame);
        }
       if (  (e.getSource()==mnOpen)  ||  (e.getSource()==bnOpen) ) {
          mnOpen_Click();
       }
       if (e.getSource()==mnSave) {
          mnSave_Click();
       }
       if (e.getSource()==mnSaveAs) {
          mnSaveAs_Click();
       }
       if (e.getSource()==mnClose) {
          mnClose_Click();
       }
       if (e.getSource()==mnCut) {
         mnCut_Click();
       }
       if (e.getSource()==mnCopy) {
         mnCopy_Click();
       }
       if (e.getSource()==mnInsert) {
          mnInsert_Click();
       }


       if (e.getSource() == mnPrevWindows ) {
          WindowLayout.mnPrevWindows(desktopPane);
       }
       if (e.getSource() == mnNextWindows ) {
         WindowLayout.mnNextWindows(desktopPane);
       }


      if (e.getSource() == mnCascade) {
         WindowLayout.mnCascade(desktopPane);
      }
      if (e.getSource() == mnTile) {
          WindowLayout.mnTile(desktopPane);
      }
      if (e.getSource() == mnTileVertical) {
         WindowLayout.mnTileVertical(desktopPane);    
      }
      if (e.getSource() == mnTileHorizontal) {
         WindowLayout.mnTileHorizontal(desktopPane);
      }

    }  // actionPerformed

  



     // Beispiel zum Oeffnen EINER Datei
  void mnOpen_Click() {
   JFileChooser jFileChooser1 = new JFileChooser();
   String sFileName;

     // Filter definieren
   MyFileFilter javaFilter, txtFilter;

       // Eine Maske
    javaFilter = new MyFileFilter(
              "java", "Java-Texte");
       // Zwei Masken
	txtFilter = new MyFileFilter(
  new String[] {"dat", "txt"}, "Texte");

     // setzen der Optionen
   jFileChooser1.setMultiSelectionEnabled(false);
   jFileChooser1.setCurrentDirectory( 
    new File(  (String) System.getProperties().get("user.dir")  )   );
   jFileChooser1.addChoosableFileFilter(
          javaFilter);
   jFileChooser1.addChoosableFileFilter(txtFilter);

      // Aufruf des Dialogfensters
    if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) {
        // holen des Dateinamens
      sFileName = jFileChooser1.getSelectedFile().getPath();
        // Erzeugen des Clientframes
      MyInternalFrame frame = new MyInternalFrame( sFileName, desktopPane.getAllFrames().length+1 );
        // Grenzen
      int pos = desktopPane.getAllFrames().length+1;
      frame.setBounds(pos*10, pos*10, 450, 300);
      desktopPane.add(frame);

      // Neu
      desktopPane.moveToFront(frame);

    }
  }  // mnOpen


     // Beispiel zum Oeffnen MEHRERE Datei
  void mnOpen2_Click() {
   JFileChooser jFileChooser1;
   String sFileName;
   File [] files;  // Array für

   jFileChooser1 = new JFileChooser();
   jFileChooser1.setMultiSelectionEnabled(true);

   jFileChooser1.setCurrentDirectory(  new File(  (String) System.getProperties().get("user.dir")  )   );

     // Okay gedrückt ?
    if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) {
      files = jFileChooser1.getSelectedFiles();

      int k;
      k = files.length;
      for (int i=0; i<files.length; i++) {
        sFileName = files[i].getPath();
        MyInternalFrame frame = new MyInternalFrame( sFileName, desktopPane.getAllFrames().length+1 );
        int pos = desktopPane.getAllFrames().length+1;
        frame.setBounds(pos*10, pos*10, 450, 300);
        desktopPane.add(frame);
        desktopPane.moveToFront(frame);
      }
    }
  }  // mnOpen

  void mnSave_Click() {
    if (_sFileName.length()==0)
      mnSaveAs_Click();
    else
      SaveToFile();
  }

  void mnSaveAs_Click() {
   JFileChooser jFileChooser1 = new JFileChooser();
   String sFileName;

   MyFileFilter dbsFilter;

   dbsFilter = new MyFileFilter( new String[] {"std"}, "Studenten-Datenbank");

   jFileChooser1.setCurrentDirectory(  new File(  (String) System.getProperties().get("user.dir")  )   );
   jFileChooser1.addChoosableFileFilter(dbsFilter);

	// Ok-Schalter gedrückt ?
    if (JFileChooser.APPROVE_OPTION == jFileChooser1.showSaveDialog(this)) {
      _sFileName = jFileChooser1.getSelectedFile().getPath();
      SaveToFile();
    }
  }

  public void SaveToFile() {
    // suche aktive Fenster
    // speichert eine Datei
   MyInternalFrame frame;
   JInternalFrame[] ff;  // Array of all internal Clientwindow
   ff = desktopPane.getAllFrames();
   if (ff.length>0) {
     frame = (MyInternalFrame) ff[0];
     System.out.println( frame.getTitle() );
   }
  }  // SaveToFile


   // wird aufgerufen beim Schließen des Hauptfensters
  void mnClose_Click() {
     this.dispose(); //     System.exit(0);
  }


   // -----------------
     // Event definieren, CODE
     // Hier sollte das aktuelle Clientfensters aufgerufen werden
  void mnCut_Click() {
    System.out.println("Menüeintrag Cut");
  }

     // Event definieren, CODE
     // Hier sollte das aktuelle Clientfensters aufgerufen werden
  void mnCopy_Click() {
    System.out.println("Menüeintrag Copy");
  }

     // Event definieren, CODE
     // Hier sollte das aktuelle Clientfensters aufgerufen werden
  void mnInsert_Click() {
    System.out.println("Menüeintrag Insert");
  }



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





// Clientfenster
class MyInternalFrame extends JInternalFrame {
  private String _sFileName;
  private String _Caption;
  private int _nr;


  public MyInternalFrame(String sFileName, int nr) {
    _sFileName = sFileName;
    if (sFileName==null)
      _Caption = "noname";
    else
      _Caption = sFileName;
    _nr = nr;
    setGUI();
    setVisible(true);
  }

  public void setGUI() {
    this.setClosable(true);
    this.setIconifiable(true);
    this.setMaximizable(true);
    this.setResizable(true);
    this.getContentPane().setLayout( new BorderLayout() );
    this.setTitle(_nr+": "+_Caption);
  }

}  // MyInternalFrame




// Klasse für die Filter eines FileChoosers
// muss nicht geändert werden
class MyFileFilter extends FileFilter {

    private static String TYPE_UNKNOWN = "Type Unknown";
    private static String HIDDEN_FILE = "Hidden File";

    private Hashtable filters = null;
    private String description = null;
    private String fullDescription = null;
    private boolean useExtensionsInDescription = true;

    /**
     * Creates a file filter. If no filters are added, then all
     * files are accepted.
     *
     * @see #addExtension
     */
    public MyFileFilter() {
     	this((String) null, (String) null);
    }

    /**
     * Creates a file filter that accepts files with the given extension.
     * Example: new ExampleFileFilter("jpg");
     *
     * @see #addExtension
     */
    public MyFileFilter(String extension) {
     	this(extension,null);
    }

    /**
     * Creates a file filter that accepts the given file type.
     * Example: new ExampleFileFilter("jpg", "JPEG Image Images");
     *
     * Note that the "." before the extension is not needed. If
     * provided, it will be ignored.
     *
     * @see #addExtension
     */
    public MyFileFilter(String extension, String description) {
     	this(new String[] {extension}, description);
    }

    /**
     * Creates a file filter from the given string array.
     * Example: new ExampleFileFilter(String {"gif", "jpg"});
     *
     * Note that the "." before the extension is not needed adn
     * will be ignored.
     *
     * @see #addExtension
     */
    public MyFileFilter(String[] filters) {
     	this(filters, null);
    }

    /**
     * Creates a file filter from the given string array and description.
     * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
     *
     * Note that the "." before the extension is not needed and will be ignored.
     *
     * @see #addExtension
     */
    public MyFileFilter(String[] filters, String description) {
      this.filters = new Hashtable(filters.length);
      for (int i = 0; i < filters.length; i++) {
          // add filters one by one
          addExtension(filters[i]);
      }
      setDescription(description);
    }

    /**
     * Return true if this file should be shown in the directory pane,
     * false if it shouldn't.
     *
     * Files that begin with "." are ignored.
     *
     * @see #getExtension
     * @see FileFilter#accepts
     */
    public boolean accept(File f) {
      if(f != null) {
          if(f.isDirectory()) {
        return true;
          }
          String extension = getExtension(f);
          if(extension != null &&
          filters.get(getExtension(f))
                  != null) {
        return true;
          };
      }
      return false;
    }

    /**
     * Return the extension portion of the file's name .
     *
     * @see #getExtension
     * @see FileFilter#accept
     */
     public String getExtension(File f) {
        if(f != null) {
            String filename = f.getName();
            int i = filename.lastIndexOf('.');
            if(i>0 && i<filename.length()-1) {
          return filename.substring(i+1).toLowerCase();
            };
        }
        return null;
    }

    /**
     * Adds a filetype "dot" extension to filter against.
     *
     * For example: the following code will create a filter that filters
     * out all files except those that end in ".jpg" and ".tif":
     *
     *   ExampleFileFilter filter = new ExampleFileFilter();
     *   filter.addExtension("jpg");
     *   filter.addExtension("tif");
     *
     * Note that the "." before the extension is not needed and will be ignored.
     */
    public void addExtension(String extension) {
      if(filters == null) {
          filters = new Hashtable(5);
      }
      filters.put(extension.toLowerCase(), this);
      fullDescription = null;
    }


    /**
     * Returns the human readable description of this filter. For
     * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
     *
     * @see setDescription
     * @see setExtensionListInDescription
     * @see isExtensionListInDescription
     * @see FileFilter#getDescription
     */
    public String getDescription() {
      if(fullDescription == null) {
          if(description == null || isExtensionListInDescription()) {
        if(description != null) {
            fullDescription = description;
        }
        fullDescription += " (";
        // build the description from the extension list
        Enumeration extensions = filters.keys();
        if(extensions != null) {
            fullDescription += "." + (String) extensions.nextElement();
            while (extensions.hasMoreElements()) {
          fullDescription += ", " + (String) extensions.nextElement();
            }
        }
        fullDescription += ")";
          } else {
        fullDescription = description;
          }
      }
      return fullDescription;
    }

    /**
     * Sets the human readable description of this filter. For
     * example: filter.setDescription("Gif and JPG Images");
     *
     * @see setDescription
     * @see setExtensionListInDescription
     * @see isExtensionListInDescription
     */
    public void setDescription(String description) {
   	  this.description = description;
    	fullDescription = null;
    }

    /**
     * Determines whether the extension list (.jpg, .gif, etc) should
     * show up in the human readable description.
     *
     * Only relevent if a description was provided in the constructor
     * or using setDescription();
     *
     * @see getDescription
     * @see setDescription
     * @see isExtensionListInDescription
     */
    public void setExtensionListInDescription(boolean b) {
      useExtensionsInDescription = b;
      fullDescription = null;
    }

    /**
     * Returns whether the extension list (.jpg, .gif, etc) should
     * show up in the human readable description.
     *
     * Only relevent if a description was provided in the constructor
     * or using setDescription();
     *
     * @see getDescription
     * @see setDescription
     * @see setExtensionListInDescription
     */
    public boolean isExtensionListInDescription() {
     	return useExtensionsInDescription;
    }
}  // MyFilter



class WindowLayout {

  
    public static void mnPrevWindows(JDesktopPane desktopPane) {     
      JInternalFrame[] frames = desktopPane.getAllFrames();
      if(frames.length > 1)
     { 
         desktopPane.selectFrame(false);
     }
   }
	
   public static void mnNextWindows(JDesktopPane desktopPane) {
       JInternalFrame[] frames = desktopPane.getAllFrames();
        if(frames.length > 1)
       {
          desktopPane.selectFrame(true);
       }
   }


 private static  void setCascadeFrame(JDesktopPane desktopPane) {
   final int w = 250;
   final int h = 150;
   JInternalFrame frame;     // the actual Clientwindow
   JInternalFrame[] ff;  // Array of all internal Clientwindow
   ff = desktopPane.getAllFrames();
   for (int pos = 0; pos<ff.length; pos++) {
      frame = (JInternalFrame)ff[pos];  // typecasting
      frame.setBounds(
     (pos*50 % (desktopPane.getWidth()-w) ),
     (pos*50 % (desktopPane.getHeight()-h) ), 
         w, h);
   } // for
  }  // setCascadeFrame()

   // -----------------
  public static void mnCascade(JDesktopPane desktopPane) {
      int i, j, p, Abstand, w, h, n;
      JInternalFrame f;     // the actual Clientwindow
      JInternalFrame[] ff;  // Array of all internal Clientwindow
      ff = desktopPane.getAllFrames();
      n = ff.length;  // Count of Frames
      // calc the distance of the frames, depends on the count of the frames
      if (n > 0) {
           if (n<=3)
             Abstand = 80;
           else {
             if (n>6)
               Abstand = 25;
             else
               Abstand = 40;
           }
           // calc the width and height of each frame
           w = desktopPane.getWidth() - (n-1)*Abstand;
           h = desktopPane.getHeight() - (n-1)*Abstand;
           if ( (w<80) || (h<80) || (n>20) ) {
             setCascadeFrame(desktopPane);
           }
           else {
             if (w<=0) w = 100;
             if (h<=0) h = 50;
             // the top frame has the index 0 !
             // index i is for the position
             // index j ist for the frames, the last is the top-frame !
             for (i = 0, j=n-1; i <n; i++,j--) {
                 f = (JInternalFrame)ff[j];  // typecasting
                 p = i*Abstand;
                 f.setLocation(p, p);        // position
                 f.setSize(w,h);             // width height
             }
           }
      }
  }   // MnCascade_click() {


  public static void mnTile(JDesktopPane desktopPane) {
    System.out.println("Menüeintrag Tile");
      JInternalFrame[] ff;    // Array for all internal frames
      JInternalFrame f; //
      int hoehe, anz, breite, left, top;
      int indexCol, anzCol, anzRow, anzRowUnten, n, i, h, b1, b2;
      boolean bZweiBreiten;

      ff = desktopPane.getAllFrames();
        // Count > 0 ?
      if (ff.length > 0) {
          // Quadratische Aufteilung

            /*
             5:
                 -
                - -
                - -

             11:
                 - -
                - - -
                - - -
                - - -

            */

          n = ff.length;
          if (n < 1) {
              return;
          }
          if (n == 1) {
              f = (JInternalFrame)ff[0];
              try {
                  f.setMaximum(true);
              }
              catch (Exception e1) {
                  e1.printStackTrace();
              }
              return;
          }
          
          anzCol = (int)Math.round(Math.sqrt(n)); // Wurzel aus Anzahl
          // bestimme Anzahl der unteren (vollen) Zeilen
          anzRowUnten = n / anzCol;  // trunc
            // Abfrage nach Rest
          if (n > (anzRowUnten * anzCol)) {
              anzRow = anzRowUnten + 1;
              bZweiBreiten = true;
          }
          else {
              anzRow = anzRowUnten;
              bZweiBreiten = false;
          }
          //  AnzRow zeigt die Anzahl aller Zeilen

          // nun werden zwei Breiten und eine Höhe ausgerechnet
          // 1. Breite  gilt für die vollen Reihen
          // 2. Breite  gilt für oberste  Reihe, falls sie nicht voll ist
          hoehe = desktopPane.getHeight();
          h = desktopPane.getHeight() / anzRow;
          b1 = desktopPane.getWidth() / anzCol; // volle Breite
          if (bZweiBreiten)
             b2 = desktopPane.getWidth() / (n - anzCol * anzRowUnten); // untere Breite
          else
             b2 = b1;

          // Rückwärts
          anz = 0;
          left = 0;
          top = hoehe - h;
          indexCol = 0;
          for (i = ff.length - 1; i >= 0; i--) {
              f = (JInternalFrame)ff[i];
              //f.setSize(100,100);
              try {
                  f.setMaximum(false);
              }
              catch (Exception e2) {
                  e2.printStackTrace();
              }
              anz++;
              indexCol++;
              if (indexCol > anzCol) {
                  // neue Reihe anfangen
                  left = 0;
                  top -= h;
                  indexCol = 1;
              }
              // Position im Fenster setzen
              f.setLocation(left, top);
              // Breite Höhe setzen
              // Check, ob man in der obersten Zeile sich befindet ?
              if (anz > (anzRowUnten * anzCol)) {
                  f.setSize(b2, h); // width height
                  left += b2;
              }
              else {
                  f.setSize(b1, h); // width height
                  left += b1;
              }
          }
      }
  } // MnTile



  public static void mnTileHorizontal(JDesktopPane desktopPane) {
      JInternalFrame[] ff;    // Array for all internal frames
      JInternalFrame f; //
      int hoehe, breite, top;
      int i, n;
      ff = desktopPane.getAllFrames();
        // Count > 0 ?
      if (ff.length > 0) {
          n = ff.length;
          breite = desktopPane.getWidth() ;
          hoehe = desktopPane.getHeight()/ n;
          if (ff.length==1 ) {
              f = (JInternalFrame)ff[0];
              try {
                  f.setMaximum(false);
                  f.setSize(desktopPane.getWidth(), hoehe);
                  f.setLocation(0,0);
                  return;
              }
              catch (Exception e2) {
                  e2.printStackTrace();
              }            
          }
          top=0;
          for (i = ff.length - 1; i >= 0; i--) {
              f = (JInternalFrame)ff[i];
              //f.setSize(100,100);
              try {
                  f.setMaximum(false);
                  f.setSize(breite, hoehe);
                  f.setLocation(0,top);  // x/y
                  top+=hoehe;
              }
              catch (Exception e2) {
                  e2.printStackTrace();
              }
          }
      }
    
  }



  public static void mnTileVertical(JDesktopPane desktopPane) {
      JInternalFrame[] ff;    // Array for all internal frames
      JInternalFrame f; //
      int hoehe, breite, left, top;
      int i, n;
      ff = desktopPane.getAllFrames();
        // Count > 0 ?
      if (ff.length > 0) {
          n = ff.length;
          breite = desktopPane.getWidth() / n;
          hoehe = desktopPane.getHeight();
          if (ff.length==1 ) {
              f = (JInternalFrame)ff[0];
              try {
                  f.setMaximum(false);
                  f.setSize(desktopPane.getWidth(), hoehe);
                  f.setLocation(0,0);
                  return;
              }
              catch (Exception e2) {
                  e2.printStackTrace();
              }            
          }
          left=0;
          for (i = ff.length - 1; i >= 0; i--) {
              f = (JInternalFrame)ff[i];
              //f.setSize(100,100);
              try {
                  f.setMaximum(false);
                  f.setSize(breite, hoehe);
                  f.setLocation(left,0);  // x/y
                  left+=breite;
              }
              catch (Exception e2) {
                  e2.printStackTrace();
              }
          }
      }
    
  }
  
  
}
