283 lines
7.3 KiB
Java
283 lines
7.3 KiB
Java
|
import java.awt.*;
|
||
|
import java.awt.event.*;
|
||
|
import javax.swing.*;
|
||
|
|
||
|
/*
|
||
|
* Der Calculator besitzt die basis Komponenten der Kalkulation +,-,*,%
|
||
|
* Eingabe erfolgt über die Buttons auf dem Interface oder der Tastatur, wobei die Kalkulationtasten nur über das Interface
|
||
|
* eingegeben werden können.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
|
||
|
public class Calculator
|
||
|
{
|
||
|
public static void main(String[] args)
|
||
|
{
|
||
|
CalculatorFrame frame = new CalculatorFrame();
|
||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
|
// Visible auf true
|
||
|
frame.setVisible(true);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class CalculatorFrame extends JFrame
|
||
|
{
|
||
|
public CalculatorFrame()
|
||
|
{
|
||
|
setTitle("Taschenrechner");
|
||
|
|
||
|
//Aufruf des Panels
|
||
|
CalculatorPanel panel = new CalculatorPanel();
|
||
|
panel.requestFocusInWindow();
|
||
|
add(panel); // Panel zu jFrame adden
|
||
|
pack();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class CalculatorPanel extends JPanel
|
||
|
{
|
||
|
private JButton display;
|
||
|
private JPanel panel;
|
||
|
|
||
|
private double result;
|
||
|
private String lastOperator;
|
||
|
private boolean start;
|
||
|
|
||
|
/*
|
||
|
* zusammenbauen des panels, setzten buttons und layout
|
||
|
*/
|
||
|
public CalculatorPanel()
|
||
|
{
|
||
|
setLayout(new BorderLayout());
|
||
|
|
||
|
result = 0;
|
||
|
lastOperator = "=";
|
||
|
start = true;
|
||
|
|
||
|
display = new JButton("0");
|
||
|
display.setEnabled(false);
|
||
|
|
||
|
|
||
|
//setzten des displays noch oben
|
||
|
add(display, BorderLayout.NORTH);
|
||
|
|
||
|
//Actions
|
||
|
ActionListener insert = new InsertAction();
|
||
|
ActionListener operator = new OperatorAction();
|
||
|
|
||
|
panel = new JPanel();
|
||
|
panel.setLayout(new GridLayout(4, 4)); // layout aus 4 Rows / Cols 4 Aufbau
|
||
|
|
||
|
|
||
|
//Setzten des Hintergrundes
|
||
|
setBk();
|
||
|
|
||
|
//Spalte 1
|
||
|
addButton("7", insert);
|
||
|
addButton("8", insert);
|
||
|
addButton("9", insert);
|
||
|
addButton(":", operator);
|
||
|
|
||
|
//Spalte 2
|
||
|
addButton("4", insert);
|
||
|
addButton("5", insert);
|
||
|
addButton("6", insert);
|
||
|
addButton("*", operator);
|
||
|
|
||
|
//Spalte 3
|
||
|
addButton("1", insert);
|
||
|
addButton("2", insert);
|
||
|
addButton("3", insert);
|
||
|
addButton("-", operator);
|
||
|
|
||
|
//Spalte 4
|
||
|
addButton("0", insert);
|
||
|
addButton(".", insert);
|
||
|
addButton("=", operator);
|
||
|
addButton("+", operator);
|
||
|
|
||
|
add(panel, BorderLayout.CENTER);
|
||
|
|
||
|
// setzten des Focuses für die eingaben auf mein Swing grid
|
||
|
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).clear();
|
||
|
panel.getActionMap().clear();
|
||
|
|
||
|
//hinzufügen der Keys für swing
|
||
|
addKeys();
|
||
|
|
||
|
panel.setFocusable(true);
|
||
|
|
||
|
}
|
||
|
|
||
|
//Setzten des Hintergrundes
|
||
|
private void setBk()
|
||
|
{
|
||
|
setBackground(Color.white);
|
||
|
panel.setBackground(Color.BLACK);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
//#region Action-events
|
||
|
// Actionlistener um auf eingaben per click zu reagieren
|
||
|
private class InsertAction implements ActionListener
|
||
|
{
|
||
|
public void actionPerformed(ActionEvent event)
|
||
|
{
|
||
|
String input = event.getActionCommand();
|
||
|
handleInput(input);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//um auf eingaben per Tastatur zu reagieren
|
||
|
private class KeyInsertAction extends AbstractAction
|
||
|
{
|
||
|
private String key;
|
||
|
|
||
|
public KeyInsertAction(String key)
|
||
|
{
|
||
|
this.key = key;
|
||
|
}
|
||
|
|
||
|
// muss implementiert werden für die abstractAction
|
||
|
public void actionPerformed(ActionEvent e)
|
||
|
{
|
||
|
handleInput(key);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//commandListener falls es sich um einen Befehl handelt +*-...
|
||
|
private class OperatorAction implements ActionListener
|
||
|
{
|
||
|
public void actionPerformed(ActionEvent event)
|
||
|
{
|
||
|
String operator = event.getActionCommand();
|
||
|
handleCommand(operator);
|
||
|
}
|
||
|
}
|
||
|
//#endregion
|
||
|
|
||
|
|
||
|
//#region Handler
|
||
|
//Handler des imputs
|
||
|
private void handleInput(String input)
|
||
|
{
|
||
|
if (start)
|
||
|
{
|
||
|
display.setText("");
|
||
|
start = false;
|
||
|
}
|
||
|
// setzten des display text bis rechen Operator
|
||
|
display.setText(display.getText() + input);
|
||
|
}
|
||
|
|
||
|
//handler des Komandos
|
||
|
private void handleCommand(String op)
|
||
|
{
|
||
|
if (start)
|
||
|
{
|
||
|
// wenn -
|
||
|
if (op.equals("-"))
|
||
|
{
|
||
|
display.setText(op);
|
||
|
start = false;
|
||
|
}
|
||
|
else
|
||
|
lastOperator = op;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// Berechnung ausführen
|
||
|
calculate(Double.parseDouble(display.getText()));
|
||
|
lastOperator = op;
|
||
|
|
||
|
// taschenrechner wieder auf rechen modus setzten
|
||
|
start = true;
|
||
|
}
|
||
|
}
|
||
|
//#endregion
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Führt die anstehenden Berechnungen aus.
|
||
|
* @param x der mit dem vorherigen Ergebnis zu berechnende Wert */
|
||
|
public void calculate(double x)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
// Berechnung durchführen und berechnung immer neu ausführen und anzeigen
|
||
|
if (lastOperator.equals("+"))
|
||
|
result += x;
|
||
|
else if (lastOperator.equals("-"))
|
||
|
result -= x;
|
||
|
else if (lastOperator.equals("*"))
|
||
|
result *= x;
|
||
|
else if (lastOperator.equals("/"))
|
||
|
result /= x;
|
||
|
else if (lastOperator.equals("="))
|
||
|
result = x;
|
||
|
|
||
|
display.setText("" + result);
|
||
|
}
|
||
|
catch (NumberFormatException ex)
|
||
|
{
|
||
|
messageBox(ex.toString(), "Fehlerhafte Eingabe");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
//#region add-funktionen
|
||
|
|
||
|
/**
|
||
|
* hinzufügen der Keys
|
||
|
* sind nötig damit Swing auf Tastatur éingaben reagieren kann
|
||
|
* Verbindung zwischen einer Taste/Tastenkombination und einer Aktion da */
|
||
|
private void addKeys()
|
||
|
{
|
||
|
|
||
|
// KeyBindings für die Zahlen 0 bis 9
|
||
|
for (int i = 0; i < 10; i++)
|
||
|
{
|
||
|
String key = String.valueOf(i);
|
||
|
|
||
|
// keys normal 0-9 normal
|
||
|
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), key);
|
||
|
panel.getActionMap().put(key, new KeyInsertAction(key));
|
||
|
|
||
|
//Keys des numpad 0-9 einfügen
|
||
|
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("NUMPAD" + i), "NUMPAD" + i);
|
||
|
panel.getActionMap().put("NUMPAD" + i, new KeyInsertAction(key));
|
||
|
}
|
||
|
|
||
|
//oder so einfügen
|
||
|
//panel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_0, 0), "0");
|
||
|
//panel.getActionMap().put("0", new KeyInsertAction("0")); das gleiche noch fürs numpad
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Button hinzufügen
|
||
|
* @param lable => +-
|
||
|
* @param listener => ob ein InsertAction oder ein OperatorAction */
|
||
|
private void addButton(String label, ActionListener listener)
|
||
|
{
|
||
|
JButton button = new JButton(label);
|
||
|
button.addActionListener(listener);
|
||
|
button.setBackground(Color.lightGray);
|
||
|
panel.add(button);
|
||
|
}
|
||
|
|
||
|
//#endregion
|
||
|
|
||
|
|
||
|
/**
|
||
|
* message box zum anzeigen von problemen
|
||
|
*/
|
||
|
private void messageBox(String sInfo, String sTitle) {
|
||
|
JOptionPane.showMessageDialog(null, sInfo, "InfoBox: " + sTitle + " " + lastOperator, JOptionPane.INFORMATION_MESSAGE);
|
||
|
}
|
||
|
}
|