import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.RoundingMode;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
//@SuppressWarnings("serial")

public class guiTaschenrechner extends JFrame {
	private JLabel labelOperand1;
	private JLabel labelOperand2;

	private JTextField fieldOperand1;
	private JTextField fieldOperand2;
	private JTextField fieldErgebnis;

	private JButton buttonAddition;
	private JButton buttonSubtraktion;
	private JButton buttonMultiplikation;
	private JButton buttonDivision;
	
	private Color bg;
	private Color schrift;
		
	public guiTaschenrechner(String Titel) {
	
		setTitle(Titel);	
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(360,160);
		setResizable(false);
		setLocationRelativeTo(null);
		
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		//labelOperand1
		labelOperand1 = new JLabel("Zahl 1");
//		labelOperand1.setMaximumSize(new Dimension(50,30));
//		labelOperand1.setMinimumSize(new Dimension(50,30));
		c.weightx = 0.5;
		c.ipady = 5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridy = 0;
		c.gridx = 0;
		add(labelOperand1, c);
		
		//fieldOperand1
		fieldOperand1 = new JTextField(4);
//		fieldOperand1.setMaximumSize(new Dimension(60,30));
//		fieldOperand1.setMinimumSize(new Dimension(60,30));		
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridy = 0;
		c.gridx = 1;
		add(fieldOperand1, c);
		
		//labelOperand2
		labelOperand2 = new JLabel("Zahl 2");
//		labelOperand2.setMaximumSize(new Dimension(50,30));
//		labelOperand2.setMinimumSize(new Dimension(50,30));
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridy = 0;
		c.gridx = 2;
		add(labelOperand2, c);
		
		//fieldOperand2
		fieldOperand2 = new JTextField(4);
//		fieldOperand2.setMaximumSize(new Dimension(60,30));
//		fieldOperand2.setMinimumSize(new Dimension(60,30));
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridy = 0;
		c.gridx = 3;
		add(fieldOperand2, c);
	
		buttonAddition = new JButton("+");
//		buttonAddition.setMaximumSize(new Dimension(50,50));
//		buttonAddition.setMinimumSize(new Dimension(50,50));
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 20;
		c.gridy = 1;
		c.gridx = 0;
		add(buttonAddition, c);
				
		buttonSubtraktion = new JButton("-");
//		buttonSubtraktion.setMaximumSize(new Dimension(50,50));
//		buttonSubtraktion.setMinimumSize(new Dimension(50,50));
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridy = 1;
		c.gridx = 1;
		add(buttonSubtraktion, c);
		
		buttonMultiplikation = new JButton("*");
//		buttonMultiplikation.setMaximumSize(new Dimension(50,50));
//		buttonMultiplikation.setMinimumSize(new Dimension(50,50));
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridy = 1;
		c.gridx = 2;
		add(buttonMultiplikation, c);
		
		buttonDivision = new JButton("/");
//		buttonDivision.setMaximumSize(new Dimension(50,50));
//		buttonDivision.setMinimumSize(new Dimension(50,50));
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridy = 1;
		c.gridx = 3;
		add(buttonDivision, c);
		
		//fieldErgebnis	
		fieldErgebnis = new JTextField(4);
//		fieldErgebnis.setMaximumSize(new Dimension(100,30));
//		fieldErgebnis.setMinimumSize(new Dimension(100,30));
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 5;
		c.gridy = 2;
		c.gridx = 1;
		c.gridwidth = 2;
		add(fieldErgebnis, c);
		
		fieldErgebnis.setEnabled(false);//setzt das Feld inaktiv damit niemand das Ergebnis löschen kann 
			bg = new Color(220,220,220);
		fieldErgebnis.setBackground(bg);
		schrift = new Color(50,50,50);
		fieldErgebnis.setForeground(schrift); //das hier ist die Farbe wenn das Feld aktiv ist 
		fieldErgebnis.setDisabledTextColor(schrift);//das hier ist die Farbe wenn es inaktiv ist
		
		setVisible(true);
	
		buttonAddition.addActionListener(new ActionListener() 
			{
			public void actionPerformed(ActionEvent e) {
				String op1s = fieldOperand1.getText();
				double op1 = 0;
				try {
		    		op1 = Double.parseDouble(op1s);
		    	}
		    	catch (NumberFormatException e1)
		    	{
		    		JOptionPane.showMessageDialog(guiTaschenrechner.this, "Im Feld 1 ist keine Zahl", "Fehler", JOptionPane.ERROR_MESSAGE);
		    		//abbrechen
		    	}
				String op2s = fieldOperand2.getText();
				double op2 = 0;
				try {
		    		op2 = Double.parseDouble(op2s);
		    	}
		    	catch (NumberFormatException e2)
		    	{
		    		JOptionPane.showMessageDialog(guiTaschenrechner.this, "Im Feld 2 ist keine Zahl", "Fehler", JOptionPane.ERROR_MESSAGE);
		    		//abbrechen
		    	}
								
				double ergebnis = op1+op2;
				NumberFormat nf = NumberFormat.getNumberInstance();
				nf.setMaximumFractionDigits(12);
				nf.setRoundingMode(RoundingMode.HALF_UP);
				String rounded = nf.format(ergebnis);
				fieldErgebnis.setText(rounded);
										
				
			}
		});
			
		buttonSubtraktion.addActionListener(new ActionListener() 
				{
			public void actionPerformed(ActionEvent e) {
				String op1s = fieldOperand1.getText();
				double op1 = 0;
				try {
		    		op1 = Double.parseDouble(op1s);
		    	}
		    	catch (NumberFormatException e1)
		    	{
		    		JOptionPane.showMessageDialog(guiTaschenrechner.this, "Im Feld 1 ist keine Zahl", "Fehler", JOptionPane.ERROR_MESSAGE);
		    		//abbrechen
		    	}
				String op2s = fieldOperand2.getText();
				double op2 = 0;
				try {
		    		op2 = Double.parseDouble(op2s);
		    	}
		    	catch (NumberFormatException e2)
		    	{
		    		JOptionPane.showMessageDialog(guiTaschenrechner.this, "Im Feld 2 ist keine Zahl", "Fehler", JOptionPane.ERROR_MESSAGE);
		    		//abbrechen
		    	}
			
			
				double ergebnis = op1-op2;
				NumberFormat nf = NumberFormat.getNumberInstance();
				nf.setMaximumFractionDigits(12);
				nf.setRoundingMode(RoundingMode.HALF_UP);
				String rounded = nf.format(ergebnis);
				fieldErgebnis.setText(rounded);
			
			}
		}); 
		
					
		buttonMultiplikation.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) {
				String op1s = fieldOperand1.getText();
				double op1 = 0;
				try {	
		    		op1 = Double.parseDouble(op1s);
		    	}
		    	catch (NumberFormatException e1)
		    	{
		    		JOptionPane.showMessageDialog(guiTaschenrechner.this, "Im Feld 1 ist keine Zahl", "Fehler", JOptionPane.ERROR_MESSAGE);
		    		//abbrechen
		    	}
				double op2 = 0;
				try {String op2s = fieldOperand2.getText();
		    		op2 = Double.parseDouble(op2s);
		    	}
		    	catch (NumberFormatException e2)
		    	{
		    		JOptionPane.showMessageDialog(guiTaschenrechner.this, "Im Feld 2 ist keine Zahl", "Fehler", JOptionPane.ERROR_MESSAGE);
		    		//abbrechen
		    	}
				
				double ergebnis = op1*op2;
				NumberFormat nf = NumberFormat.getNumberInstance();
				nf.setMaximumFractionDigits(12);
				nf.setRoundingMode(RoundingMode.HALF_UP);
				String rounded = nf.format(ergebnis);
				fieldErgebnis.setText(rounded);
	
			}
		});
		
		
		buttonDivision.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) {
				String op1s = fieldOperand1.getText();
				double op1 = 0;
				try {
		    		op1 = Double.parseDouble(op1s);
		    	}
		    	catch (NumberFormatException e1)
		    	{
		    		JOptionPane.showMessageDialog(guiTaschenrechner.this, "Im Feld 1 ist keine Zahl", "Fehler", JOptionPane.ERROR_MESSAGE);
		    		//abbrechen
		    	}
				double op2 = 0;
				try {String op2s = fieldOperand2.getText();
		    		op2 = Double.parseDouble(op2s);
		    	}
		    	catch (NumberFormatException e2)
		    	{
		    		JOptionPane.showMessageDialog(guiTaschenrechner.this, "Im Feld 2 ist keine Zahl", "Fehler", JOptionPane.ERROR_MESSAGE);
		    		//abbrechen
		    	}
				double ergebnis = op1/op2;
				NumberFormat nf = NumberFormat.getNumberInstance();
				nf.setMaximumFractionDigits(12);
				nf.setRoundingMode(RoundingMode.HALF_UP);
				String rounded = nf.format(ergebnis);
				fieldErgebnis.setText(rounded);
					
			}
		});		
		
		
	}
}

