//Titel:            Testbeispiel für ein Menü
//Version:          2.1
//Copyright:        HS-Harz, FB AI, 2007, 2012
//Autor:            Dipl.-Inf. Dipl.-Ing. Michael Wilhelm
//Firma:



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


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



public class menu2 extends JFrame  {

    // Menü Variablen
  private JMenuBar menuBar1;
  private JMenu MainFile;
  private JMenuItem MnNew;
  private JMenuItem MnOpen;
  private JMenuItem MnClose;

  private JToggleButton BnArial;
  private JToggleButton BnTimesRoman;
  private JToggleButton BnCourier;
  private JToggleButton BnVerdana;

  JTextArea editor = new JTextArea("Dies ist ein Text im Editor\nJiIller Mit zwei Zeilen");

  // Konstruktor
  public menu2() {
    setGUI();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

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

    JScrollPane sc = new JScrollPane(editor);
    this.getContentPane().add(sc, BorderLayout.CENTER );

    this.setSize( 800, 600);
    this.setVisible(true);

    setMenus();

    setToolBars();
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


  }  // setGUI


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

      // DATEI
    MainFile = new JMenu("Datei");
    MnNew = new JMenuItem("Neu");
    MnOpen = new JMenuItem("Öffnen");

    MnClose = new JMenuItem("Schließen");


    MnNew.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnNew_click();
      }
    });
    MnNew.setAccelerator( 
          KeyStroke.getKeyStroke(KeyEvent.VK_N, 
          ActionEvent.CTRL_MASK));
    MainFile.add(MnNew);


    MnOpen.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnOpen_actionPerformed(e);
      }
    });
    MnOpen.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
    MainFile.add(MnOpen);

    menuBar1.add(MainFile);

    MainFile.addSeparator();

    // --------------------------
    MnClose.addActionListener(new ActionListener()  {

      public void actionPerformed(ActionEvent e) {
        MnClose_actionPerformed(e);
      }
    });
    MainFile.add(MnClose);

    this.setJMenuBar(menuBar1);
  } // setMenus


    // ButtonImage
  private ImageIcon loadIcon(String name, String description) {
   	// String path = "images" + name;
   	String path = name;  // Hier eventuell den path eintragen
    return new ImageIcon(path, description);
  }
  

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

    JButton BnOpen = new JButton("Öffnen");
    BnOpen.setFont( new Font("Arial", Font.BOLD,14)  );
    BnOpen.setIcon( loadIcon("open.gif","öffnen") );

    JButton BnNew = new JButton("Neu");
    BnNew.setIcon( loadIcon("new.gif","neu") );
    BnNew.setFont( new Font("Arial", Font.BOLD,14)  );

    JButton BnSave = new JButton("Speichern");
    BnSave.setFont( new Font("Arial", Font.BOLD,14)  );
    BnSave.setIcon( loadIcon("save.gif","save") );


    toolBar.add(BnOpen);
    BnOpen.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        MnOpen_actionPerformed(e);  // !!!!!!
      }
    });

    toolBar.addSeparator();

    toolBar.add(BnNew);
    BnNew.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        MnNew_click();  // Aufruf der gemeinsamen Methode
      }
    });

    toolBar.addSeparator();

    toolBar.add(BnSave);
    BnSave.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        MnSave_click();
      }
    });


    BnArial = new JToggleButton("Arial",true);
    BnArial.setFont( new Font("Arial", Font.BOLD,14)  );
    editor.setFont( new Font("Arial", Font.BOLD,18)  );

    BnTimesRoman = new JToggleButton("Times New Roman");
    BnTimesRoman.setFont( new Font("Times New Roman", Font.BOLD,14)  );

    BnCourier = new JToggleButton("Courier New");
    BnCourier.setFont( new Font("Courier New", Font.BOLD,14)  );

    BnVerdana = new JToggleButton("Verdana");
    BnVerdana.setFont( new Font("Verdana", Font.BOLD,14)  );

   ButtonGroup group = new ButtonGroup();
    group.add(BnArial);
    group.add(BnTimesRoman);
    group.add(BnCourier);
   group.add(BnVerdana);

    toolBar.addSeparator();
    toolBar.add(BnArial);
    toolBar.add(BnTimesRoman);
    toolBar.add(BnCourier);
    toolBar.add(BnVerdana);

    BnArial.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnFonts_click(e);
      }
    });
    BnTimesRoman.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnFonts_click(e);
      }
    });
    BnCourier.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnFonts_click(e);
      }
    });
    BnVerdana.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnFonts_click(e);
      }
    });


    this.getContentPane().add(toolBar,
          BorderLayout.NORTH);
  } // setToolBars


  void MnClose_actionPerformed(ActionEvent e) {
    System.exit(0);
  }

  void BnFonts_click(ActionEvent e) {
    //BnArial.setSelected(false);
    //BnTimesRoman.setSelected(false);
    //BnCourier.setSelected(false);
    if (e.getSource() == BnArial ) {
      BnArial.setSelected(true);
      editor.setFont( new Font("Arial", Font.BOLD,36)  );
    }
    if (e.getSource() == BnTimesRoman ) {
      BnTimesRoman.setSelected(true);
      editor.setFont( new Font("Times New Roman", Font.BOLD,36)  );
    }
    if (e.getSource() == BnCourier    ) {
      BnCourier.setSelected(true);
      editor.setFont( new Font("Courier New", Font.BOLD,36)  );
    }
    if (e.getSource() == BnVerdana    ) {
      BnVerdana.setSelected(true);
      editor.setFont( new Font("Verdana", Font.BOLD,36)  );
    }
    editor.updateUI();
  }


    // erzeugt ein neues Fenster Frame1   DEFAULT-Einstellung
  void MnNew_click() {
   System.out.println("in New");
  }


  void MnOpen_actionPerformed(ActionEvent e) {
   System.out.println("in Open");
  }  // MnOpen


  void MnSave_click() {
    System.out.println("in Save");
  }


    // MAIN
  static public void main(String[] args) {
    menu2 desktop = new menu2();
    desktop.setVisible(true);
  }

}









