Hallo,
ich bin gerade dran Java zu lernen und habe versucht einen Taschenrechner zu erstellen. Wenn ich den Code durchführe, öffnet es mir wie gewünscht den Taschenrechner. Sobald ich jedoch eine Zahl eingebe erscheint im Textfeld "0.0". Kann mir da jemand weiterhelfen? Hier der Code:
package trGui;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CalculatorGUI implements ActionListener {
private JFrame frame;
private JTextField textField = new JTextField();
private JPanel panel;
private JButton[] numberButtons = new JButton[10];
private JButton[] functionButtons = new JButton[7];
private JButton addButton, subButton, mulButton, divButton, clearButton, equalsButton, comma;
// Variablen Deklarieren
private double firstNumber;
private double secondNumber;
private char operator;
private double result;
CalculatorGUI() {
//frame erstellen
frame = new JFrame("Taschenrechner");
frame.setSize(350, 450);
frame.setLayout(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 0; i < 10; i++) {
numberButtons = new JButton(String.valueOf(i));
}
// Textfeld formatieren
textField.setBounds(40, 25, 270, 40);
textField.setEditable(false);
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
clearButton = new JButton("c");
equalsButton = new JButton("=");
comma = new JButton(".");
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = clearButton;
functionButtons[5] = equalsButton;
functionButtons[6] = comma;
for (int i = 0; i < 7; i++) {
functionButtons.addActionListener(this);
functionButtons.setFocusable(false);
}
for (int i = 0; i < 10; i++) {
numberButtons = new JButton(String.valueOf(i));
numberButtons.addActionListener(this);
numberButtons.setFocusable(false);
}
//Number Buttons formatieren
clearButton.setBounds(40, 360, 130, 40);
equalsButton.setBounds(180, 360, 130, 40);
// Textfeld formatieren
textField.setBounds(40, 25, 270, 40);
textField.setEditable(false);
//Panel für die Function und Number Buttons
panel = new JPanel();
panel.setBounds(40, 80, 270, 270);
panel.setLayout(new GridLayout(4,4,10,10));
//Reihe1
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(addButton);
//Reihe2
panel.add(numberButtons[6]);
panel.add(numberButtons[5]);
panel.add(numberButtons[4]);
panel.add(subButton);
//Reihe3
panel.add(numberButtons[3]);
panel.add(numberButtons[2]);
panel.add(numberButtons[1]);
panel.add(mulButton);
//Reihe4
panel.add(numberButtons[0]);
panel.add(comma);
panel.add(divButton);
panel.setBackground(Color.gray);
addButton.setEnabled(false);
subButton.setEnabled(false);
mulButton.setEnabled(false);
divButton.setEnabled(false);
frame.add(panel);
frame.add(textField);
frame.add(clearButton);
frame.add(equalsButton);
frame.setVisible(true);
}
public static void main(String[] args) {
CalculatorGUI calc = new CalculatorGUI();
}
private double parseNumber() {
return Double.parseDouble(textField.getText());
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons) {
textField.setText(textField.getText().concat(String.valueOf(i)));
addButton.setEnabled(true);
subButton.setEnabled(true);
mulButton.setEnabled(true);
divButton.setEnabled(true);
}
}
if (e.getSource() == comma) {
if (!textField.getText().contains(".")) {
textField.setText(textField.getText().concat("."));
}
}
if (e.getSource() == addButton) {
firstNumber = parseNumber();
operator = '+';
textField.setText("");
}
if (e.getSource() == subButton) {
firstNumber = parseNumber();
operator = '-';
textField.setText("");
}
if (e.getSource() == mulButton) {
firstNumber = parseNumber();
operator = '*';
textField.setText("");
}
if (e.getSource() == divButton) {
numberButtons[0].setEnabled(false);
firstNumber = parseNumber();
operator = '+';
textField.setText("");
}
if (e.getSource() == clearButton) {
firstNumber = 0.0;
secondNumber = 0.0;
textField.setText("");
addButton.setEnabled(false);
subButton.setEnabled(false);
mulButton.setEnabled(false);
divButton.setEnabled(false);
}
if (e.getSource() == equalsButton) {
secondNumber = parseNumber();
switch(operator) {
case '+':
result = firstNumber + secondNumber;
break;
case '-':
result = firstNumber - secondNumber;
break;
case '*':
result = firstNumber * secondNumber;
break;
case '/':
result = firstNumber / secondNumber;
break;
}
}
textField.setText(String.valueOf(result));
}
}
ich bin gerade dran Java zu lernen und habe versucht einen Taschenrechner zu erstellen. Wenn ich den Code durchführe, öffnet es mir wie gewünscht den Taschenrechner. Sobald ich jedoch eine Zahl eingebe erscheint im Textfeld "0.0". Kann mir da jemand weiterhelfen? Hier der Code:
package trGui;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CalculatorGUI implements ActionListener {
private JFrame frame;
private JTextField textField = new JTextField();
private JPanel panel;
private JButton[] numberButtons = new JButton[10];
private JButton[] functionButtons = new JButton[7];
private JButton addButton, subButton, mulButton, divButton, clearButton, equalsButton, comma;
// Variablen Deklarieren
private double firstNumber;
private double secondNumber;
private char operator;
private double result;
CalculatorGUI() {
//frame erstellen
frame = new JFrame("Taschenrechner");
frame.setSize(350, 450);
frame.setLayout(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 0; i < 10; i++) {
numberButtons = new JButton(String.valueOf(i));
}
// Textfeld formatieren
textField.setBounds(40, 25, 270, 40);
textField.setEditable(false);
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
clearButton = new JButton("c");
equalsButton = new JButton("=");
comma = new JButton(".");
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = clearButton;
functionButtons[5] = equalsButton;
functionButtons[6] = comma;
for (int i = 0; i < 7; i++) {
functionButtons.addActionListener(this);
functionButtons.setFocusable(false);
}
for (int i = 0; i < 10; i++) {
numberButtons = new JButton(String.valueOf(i));
numberButtons.addActionListener(this);
numberButtons.setFocusable(false);
}
//Number Buttons formatieren
clearButton.setBounds(40, 360, 130, 40);
equalsButton.setBounds(180, 360, 130, 40);
// Textfeld formatieren
textField.setBounds(40, 25, 270, 40);
textField.setEditable(false);
//Panel für die Function und Number Buttons
panel = new JPanel();
panel.setBounds(40, 80, 270, 270);
panel.setLayout(new GridLayout(4,4,10,10));
//Reihe1
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(addButton);
//Reihe2
panel.add(numberButtons[6]);
panel.add(numberButtons[5]);
panel.add(numberButtons[4]);
panel.add(subButton);
//Reihe3
panel.add(numberButtons[3]);
panel.add(numberButtons[2]);
panel.add(numberButtons[1]);
panel.add(mulButton);
//Reihe4
panel.add(numberButtons[0]);
panel.add(comma);
panel.add(divButton);
panel.setBackground(Color.gray);
addButton.setEnabled(false);
subButton.setEnabled(false);
mulButton.setEnabled(false);
divButton.setEnabled(false);
frame.add(panel);
frame.add(textField);
frame.add(clearButton);
frame.add(equalsButton);
frame.setVisible(true);
}
public static void main(String[] args) {
CalculatorGUI calc = new CalculatorGUI();
}
private double parseNumber() {
return Double.parseDouble(textField.getText());
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons) {
textField.setText(textField.getText().concat(String.valueOf(i)));
addButton.setEnabled(true);
subButton.setEnabled(true);
mulButton.setEnabled(true);
divButton.setEnabled(true);
}
}
if (e.getSource() == comma) {
if (!textField.getText().contains(".")) {
textField.setText(textField.getText().concat("."));
}
}
if (e.getSource() == addButton) {
firstNumber = parseNumber();
operator = '+';
textField.setText("");
}
if (e.getSource() == subButton) {
firstNumber = parseNumber();
operator = '-';
textField.setText("");
}
if (e.getSource() == mulButton) {
firstNumber = parseNumber();
operator = '*';
textField.setText("");
}
if (e.getSource() == divButton) {
numberButtons[0].setEnabled(false);
firstNumber = parseNumber();
operator = '+';
textField.setText("");
}
if (e.getSource() == clearButton) {
firstNumber = 0.0;
secondNumber = 0.0;
textField.setText("");
addButton.setEnabled(false);
subButton.setEnabled(false);
mulButton.setEnabled(false);
divButton.setEnabled(false);
}
if (e.getSource() == equalsButton) {
secondNumber = parseNumber();
switch(operator) {
case '+':
result = firstNumber + secondNumber;
break;
case '-':
result = firstNumber - secondNumber;
break;
case '*':
result = firstNumber * secondNumber;
break;
case '/':
result = firstNumber / secondNumber;
break;
}
}
textField.setText(String.valueOf(result));
}
}