import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.lang.NumberFormatException;

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 09.04.2010
  * @author
  */

public class SchuelerGUI extends JFrame {
  // Anfang Attribute
  private JLabel lbEnglisch = new JLabel();
  private JLabel lbDeutschnote = new JLabel();
  private JLabel lbMathematiknote = new JLabel();
  private JTextField tfEnglischnote = new JTextField();
  private JTextField tfDeutschnote = new JTextField();
  private JTextField tfMathematiknote = new JTextField();
  private JButton btDurchschnittberechnen = new JButton();
  private JLabel lbName = new JLabel();
  private JTextField tfName = new JTextField();
  private JLabel lbVorname = new JLabel();
  private JTextField tfVorname = new JTextField();
  private JLabel lbErgebnis = new JLabel();
  private JLabel lNotendurchschnitt = new JLabel();
  private JLabel lbBWL = new JLabel();
  private JTextField tfBWLnote = new JTextField();
  // Ende Attribute
  
  //Output formats
  DecimalFormat notenformat = new DecimalFormat("##.0");
  
  //Assoziation  Schueler-Objekt
  Schueler schueler1 = new Schueler();

  public SchuelerGUI(String title) {
    // Frame-Initialisierung
    super(title);
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    int frameWidth = 420;
    int frameHeight = 331;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    Container cp = getContentPane();
    cp.setLayout(null);
    // Anfang Komponenten

    lbEnglisch.setBounds(16, 80, 93, 16);
    lbEnglisch.setText("Englischnote");
    lbEnglisch.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
    cp.add(lbEnglisch);
    lbDeutschnote.setBounds(16, 112, 75, 16);
    lbDeutschnote.setText("Deutschnote");
    lbDeutschnote.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
    cp.add(lbDeutschnote);
    lbMathematiknote.setBounds(16, 144, 95, 16);
    lbMathematiknote.setText("Mathematiknote");
    lbMathematiknote.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
    cp.add(lbMathematiknote);
    tfEnglischnote.setBounds(128, 72, 121, 24);
    tfEnglischnote.setText("tfEnglischnote");
    cp.add(tfEnglischnote);
    tfDeutschnote.setBounds(128, 104, 121, 24);
    tfDeutschnote.setText("tfDeutschnote");
    cp.add(tfDeutschnote);
    tfMathematiknote.setBounds(128, 136, 121, 24);
    tfMathematiknote.setText("tfMathematiknote");
    cp.add(tfMathematiknote);
    btDurchschnittberechnen.setBounds(16, 256, 201, 33);
    btDurchschnittberechnen.setText("Durchschnitt berechnen");
    btDurchschnittberechnen.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        btDurchschnittberechnen_ActionPerformed(evt);
      }
    });
    cp.add(btDurchschnittberechnen);
    lbName.setBounds(16, 16, 37, 16);
    lbName.setText("Name");
    lbName.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
    cp.add(lbName);
    tfName.setBounds(80, 8, 177, 24);
    tfName.setText("tfName");
    cp.add(tfName);
    lbVorname.setBounds(16, 48, 55, 16);
    lbVorname.setText("Vorname");
    lbVorname.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
    cp.add(lbVorname);
    tfVorname.setBounds(80, 40, 177, 24);
    tfVorname.setText("tfVorname");
    cp.add(tfVorname);
    lbErgebnis.setBounds(258, 272, 99, 20);
    lbErgebnis.setText("<ergenbis>");
    lbErgebnis.setFont(new Font("MS Sans Serif", Font.PLAIN, 15));
    lbErgebnis.setForeground(Color.RED);
    lbErgebnis.setHorizontalAlignment(SwingConstants.RIGHT);
    cp.add(lbErgebnis);
    lNotendurchschnitt.setBounds(256, 248, 109, 16);
    lNotendurchschnitt.setText("Notendurchschnitt:");
    lNotendurchschnitt.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
    cp.add(lNotendurchschnitt);
    lbBWL.setBounds(16, 176, 62, 16);
    lbBWL.setText("BWL-Note");
    lbBWL.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
    cp.add(lbBWL);
    tfBWLnote.setBounds(128, 168, 121, 24);
    tfBWLnote.setText("tfBWLnote");
    cp.add(tfBWLnote);
    // Ende Komponenten

    setResizable(false);
    setVisible(true);
  }

  // Anfang Methoden
  public void btDurchschnittberechnen_ActionPerformed(ActionEvent evt) {
  schueler1.setName(tfName.getText());
  schueler1.setVorname(tfVorname.getText());
  schueler1.setEnglischnote(Double.parseDouble(tfEnglischnote.getText()));
  schueler1.setDeutschnote(Double.parseDouble(tfDeutschnote.getText()));
  schueler1.setMathematiknote(Double.parseDouble(tfMathematiknote.getText()));
  schueler1.setBWLnote(Double.parseDouble(tfBWLnote.getText()));
  schueler1.durchschnittBerechnen();
  this.ausgeben();
  }

  public void ausgeben(){
    lbErgebnis.setText(String.valueOf(notenformat.format(schueler1.getErgebnis())).replace(',','.'));
  }
  // Ende Methoden
  public static void main(String[] args) {
    new SchuelerGUI("SchuelerGUI");
  }
}
