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


import javax.swing.border.*;

// 2. Beispiel mit TextField und Schaltern
//    Radiobutton
//    CheckBox



public class GUI_02 extends JFrame {

  private JLabel jLabel1 = new JLabel();
  private JTextField TName = new JTextField();

  private JButton BnOk = new JButton();
  private JButton BnESC = new JButton();

     private int Pruefung;
     private int Note;
     private boolean WP;


  JRadioButton [] rb_links;
  JRadioButton [] rb_rechts;

  JCheckBox cbBox_Student;
  JCheckBox cbBox_WP;

  public GUI_02() {
    setGUI();
    setLocation(300,300);
    setSize(600, 500);
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    // Nach der Initialisierung muss die Klick-Methode aufgerufen werden
    Check1_click();
  }

  private void setGUI() {
    setTitle("2. Beispiel GUI, checkbox aktiviert Elemente");
    this.getContentPane().setLayout( new GridBagLayout() );

    jLabel1.setText("Name");
    TName.setText("");

    jLabel1.setFont( new Font("Arial", Font.BOLD,18)  );
    TName.setFont( new Font("Arial", Font.BOLD,18)  );


    this.getContentPane().add(jLabel1,
         new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(0,25,40,0), 0, 0));

    this.getContentPane().add(TName,
        new GridBagConstraints(1, 0, 2, 1, 10.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(0,25,40,25), 0, 0));


    // -----------------------------------
    cbBox_Student = new JCheckBox("Student der HS Harz", false);
    cbBox_Student.setFont( new Font("Arial", Font.BOLD,18)  );

    this.getContentPane().add(cbBox_Student,
           new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(10,20,30,0), 0, 0));

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

  // -------------------

    rb_links = new JRadioButton[4];
    rb_links[0] = new JRadioButton("Schriftlich", false);
    rb_links[1] = new JRadioButton("Mündlich", true);
    rb_links[2] = new JRadioButton("Entwurfsarbeit", false);
    rb_links[3] = new JRadioButton("Sitzschein", false);

    ButtonGroup group1 = new ButtonGroup();
    for (int i=0; i<rb_links.length; i++) {
       group1.add( rb_links[i] );
       rb_links[i].setFont( new Font("Arial", Font.BOLD,18)  );
        rb_links[i].addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
          rb_links_click(e);
        }
        });
    }

    // Ein Panel für links
    JLabel Label1 = new JLabel("Prüfungsform");
    Label1.setFont( new Font("Arial", Font.BOLD,18)  );
    JPanel panel1 = new JPanel();
    panel1.setLayout ( new GridLayout(4,1) ); // 5 Zeilen , 2 Spalte
    for (int i=0; i<rb_links.length; i++) {
       panel1.add( rb_links[i] );
    }
    TitledBorder tb1 = new TitledBorder(
        LineBorder.createBlackLineBorder(),
           "Prüfungsform");
    panel1.setBorder(tb1);
    tb1.setTitleColor( Color.red );
    tb1.setTitleFont( new Font("Arial", Font.BOLD,18)  );
    // panel1.add( label2 );

    this.getContentPane().add(panel1,
           new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(10,20,0,0), 0, 0));
    // --------------------

    rb_rechts = new JRadioButton[3];
    rb_rechts[0] = new JRadioButton("sehr gut", false);
    rb_rechts[1] = new JRadioButton("gut", false);
    rb_rechts[2] = new JRadioButton("Befriedigend", true);

    // ------------------------


    ButtonGroup group2 = new ButtonGroup();
    for (int i=0; i<rb_rechts.length; i++) {
       group2.add( rb_rechts[i] );
       rb_rechts[i].setFont( new Font("Arial", Font.BOLD,18)  );
        rb_rechts[i].addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
          rb_rechts_click(e);
        }
        });
    }

    // JLabel Label2 = new JLabel("Gewünschte Note");
    // Label2.setFont( new Font("Arial", Font.BOLD,18)  );
    JPanel panel2 = new JPanel();
    panel2.setLayout ( new GridLayout(4,1) ); // 5 Zeilen , 2 Spalte
    // panel2.add( Label2 );
    for (int i=0; i<rb_rechts.length; i++) {
       panel2.add( rb_rechts[i] );
    }
    TitledBorder tb2 = new TitledBorder(
        LineBorder.createBlackLineBorder(),
           "Gewünschte Note");
    panel2.setBorder(tb2);
    tb2.setTitleColor( Color.red );
    tb2.setTitleFont( new Font("Arial", Font.BOLD,18)  );

    this.getContentPane().add(panel2,
           new GridBagConstraints(2, 2, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(10,20,0,30), 0, 0));
    // ------------


    cbBox_WP = new JCheckBox("Wiederholungsprüfung", false);
    cbBox_WP.setFont( new Font("Arial", Font.BOLD,18)  );

    this.getContentPane().add(cbBox_WP,
           new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(40,20,0,0), 0, 0));

    cbBox_WP.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Check2_click(e);
      }
    });
    // -------------------

    BnOk.setText("Ok");
    BnESC.setText("Abbruch");
    BnOk.setFont( new Font("Arial", Font.BOLD,18)  );
    BnESC.setFont( new Font("Arial", Font.BOLD,18)  );

    BnOk.setPreferredSize( BnESC.getPreferredSize() );


    JPanel pn = new JPanel();
    pn.setLayout( new FlowLayout() );
    pn.add(BnOk);
    pn.add(BnESC);
//    pn.setBackground(Color.green);
    this.getContentPane().add(pn,
           new GridBagConstraints(0, 4, 3, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(30,0,0,0), 0, 0));

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



  }

  void rb_links_click(ActionEvent e) {
    JRadioButton rb = (JRadioButton) e.getSource();
    System.out.println("Geklickt: "+rb.getText() );
    // Check Sitzschein
    if (rb_links[3].isSelected() ) {
      for (int i=0; i<rb_rechts.length; i++) {
         rb_rechts[i].setEnabled( false );
      }
    }
    else {
      for (int i=0; i<rb_rechts.length; i++) {
         rb_rechts[i].setEnabled( true );
      }
    }

  }

  void rb_rechts_click(ActionEvent e) {
    JRadioButton rb = (JRadioButton) e.getSource();
    System.out.println("Geklickt: "+rb.getText() );
  }

   void Check1_click() {
    // JCheckBox chb = (JCheckBox) e.getSource();
    System.out.println("Geklickt: "+cbBox_Student.getText()+
      "\r\n     "+"Status: "+cbBox_Student.isSelected() );

    boolean bOkay = cbBox_Student.isSelected();
    cbBox_WP.setEnabled( bOkay );
    // hier getrennt, da eventuell mehrere Optionen
    for (int i=0; i<rb_links.length; i++) {
       rb_links[i].setEnabled( bOkay );
    }
    // hier getrennt, da eventuell mehrere Optionen
    for (int i=0; i<rb_rechts.length; i++) {
       rb_rechts[i].setEnabled( bOkay );
    }

   }

   void Check2_click(ActionEvent e) {
    JCheckBox chb = (JCheckBox) e.getSource();
    System.out.println("Geklickt: "+chb.getText()+
      "\r\n     "+"Status: "+chb.isSelected() );
   }

  private void BnOk_Click() {
    int i;
    // Check Input, Leerzeichen an den Enden werden gelöscht
    String sRet = TName.getText().trim();
    if ( sRet.length()==0) {
      basis.Message("Eingabe","Bitte tragen Sie einen Wert ein");
      return;
    }

    //
    if ( cbBox_Student.isSelected() ) {
     Pruefung=-1;
     for (i=0; i<rb_links.length; i++) {
        Pruefung = i;
     }
     if (Pruefung == -1) {
        basis.Message("Fehler in der Auswahl Prüfungsfrom","Fehler");
        return;
     }
     // Check Sitzschein
     if (Pruefung != 3) {
       Note=-1;
       // Eventuell detaillierter 1,0 1,3  1,7  etc.
       for (i=0; i<rb_rechts.length; i++) {
          Note = i;
       }
       if (Note == -1) {
          basis.Message("Fehler in der Auswahl Prüfungsfrom","Fehler");
          return;
       }
     }
     else {
      Note=-1;
     }
      WP = cbBox_WP.isSelected();

    }
    else {
      basis.Message("Studieren Sie an der HS Harz","Hinweis");
    }



    // nun Aktion mit dem Text
  }

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

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

}


class basis {
    /**
    * description: Zeigt eine Nachricht an   
    * @see Message, Yes-No, Yes-No-Esc, Ok-Esc
    *
    * @version     1.0  07.07.2002
    * @author      Michael Wilhelm HS Harz, FB AI
    * @param String sTitle, String sMessage
    * @return keinen
    * @exception keine
    */
  public static void Message( String sTitle, String sMessage) {
    JOptionPane.showConfirmDialog(null,sMessage,sTitle,
      JOptionPane.DEFAULT_OPTION,
      JOptionPane.INFORMATION_MESSAGE);  // Symbol
    /*  Optionen der Schalter:
         DEFAULT_OPTION    Ok
         YES_NO_OPTION
         YES_NO_CANCEL_OPTION
         OK_CANCEL_OPTION
     */
     /*  Symbole:
      ERROR_MESSAGE
      INFORMATION_MESSAGE
      WARNING_MESSAGE
      QUESTION_MESSAGE
      PLAIN_MESSAGE
     */

  } // Message


}
