Soal 2
Soal 2
*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
frame.add(textField);
// Membuat tombol
String[] buttonLabels = {
"1", "2", "3", "+",
"4", "5", "6", "-",
"7", "8", "9", "*",
"C", "0", "=", "/"
};
x += buttonWidth;
if (x >= 250) {
x = 10;
y += buttonHeight;
}
}
if (command.equals("C")) {
textField.setText("");
} else if (command.equals("=")) {
try {
String expression = textField.getText();
double result = evaluateExpression(expression);
textField.setText(Double.toString(result));
} catch (Exception ex) {
textField.setText("Error");
}
} else {
textField.setText(textField.getText() + command);
}
}
};
// Menampilkan frame
frame.setVisible(true);
}