import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* This is a simple calculator for basic arithmetic operations
*
* @author yvonne
*
*/
public class Taschenrechner extends JFrame
{
/**
* Create new Frame, set visible, set close Operation
*
* @param args
*/
public static void main(String[] args)
{
TaschenrechnerFrame frame = new TaschenrechnerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class TaschenrechnerFrame extends JFrame
{
/**
* Create new Panel, set Title, add the panel to Frame, pack create it to a
* preferred size that Buttons are shown in the Frame
*/
public TaschenrechnerFrame()
{
setTitle("Calculator");
TaschenrechnerPanel panel = new TaschenrechnerPanel();
add(panel);
pack();
}
}
class TaschenrechnerPanel extends JPanel
{
JButton display;
JPanel panel;
double result;
String lastCommand;
boolean start;
/**
* Set Start as True, create new JOptionPane, Create new Button Display with
* BoderLayout add the Buttons with numbers and commands Set PanelLayout
*
*/
public TaschenrechnerPanel()
{
setLayout(new BorderLayout());
lastCommand = "=";
start = true;
JOptionPane warnung = new JOptionPane();
warnung.setLocation(500,500);
display = new JButton(" ");
display.setEnabled(false);
add(display, BorderLayout.NORTH);
ActionListener zahlen = new ZahlenAction();
ActionListener rechenzeichen = new RechenzeichenAction();
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
addButton("7", zahlen);
addButton("8", zahlen);
addButton("9", zahlen);
addButton("/", rechenzeichen);
addButton("4", zahlen);
addButton("5", zahlen);
addButton("6", zahlen);
addButton("*", rechenzeichen);
addButton("1", zahlen);
addButton("2", zahlen);
addButton("3", zahlen);
addButton("-", rechenzeichen);
addButton("0", zahlen);
addButton(".", zahlen);
addButton("=", rechenzeichen);
addButton("+", rechenzeichen);
add(panel, BorderLayout.CENTER);
}
/**
* Create new Buttons with label add ActionListener to JButtons
*
* @param label
* @param listener
*/
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class ZahlenAction implements ActionListener
{
/**
* Set the text in disply
*/
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (display.getText().contains(".") && input.contains("."))
{
JOptionPane.showMessageDialog(null, "Ungültige Eingabe", "Warnung", JOptionPane.ERROR_MESSAGE);
return;
}
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}
private class RechenzeichenAction implements ActionListener
{
/**
* if the fist input was a - set a negativ number when the first command
* was a other command they show an warning box is access the method
* calculate
*/
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (start)
{
if (command.equals("-"))
{
display.setText(command);
start = false;
} else
{
lastCommand = command;
JOptionPane.showMessageDialog(null, "Ungültige Eingabe", "Warnung", JOptionPane.ERROR_MESSAGE);
return;
}
} else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}
/**
* this methode act the calculations with chosen commands and set the text
*
* @param x
*/
public void calculate(double x)
{
if (lastCommand.equals("+"))
result += x;
else if (lastCommand.equals("-"))
result -= x;
else if (lastCommand.equals("*"))
result *= x;
else if (lastCommand.equals("/"))
result /= x;
else if (lastCommand.equals("="))
result = x;
display.setText("" + result);
}
}