0% found this document useful (0 votes)
18 views2 pages

Soal 2

This Java program creates a simple calculator application with a graphical user interface. It displays a text field for input and output and buttons for numbers and operators. When buttons are clicked, the corresponding values are appended to the text field. The '=' button evaluates the expression and displays the result.

Uploaded by

Siti Lutfiyah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Soal 2

This Java program creates a simple calculator application with a graphical user interface. It displays a text field for input and output and buttons for numbers and operators. When buttons are clicked, the corresponding values are appended to the text field. The '=' button evaluates the expression and displays the result.

Uploaded by

Siti Lutfiyah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import javax.swing.

*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleCalculator {


public static void main(String[] args) {
// Membuat frame utama
JFrame frame = new JFrame("Kalkulator Sederhana");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
frame.setLayout(null);

// Membuat field teks untuk menampilkan input dan hasil


JTextField textField = new JTextField();
textField.setBounds(10, 10, 265, 40);

frame.add(textField);

// Membuat tombol
String[] buttonLabels = {
"1", "2", "3", "+",
"4", "5", "6", "-",
"7", "8", "9", "*",
"C", "0", "=", "/"
};

JButton[] buttons = new JButton[buttonLabels.length];


int x = 10, y = 60, buttonWidth = 60, buttonHeight = 60;

for (int i = 0; i < buttonLabels.length; i++) {


buttons[i] = new JButton(buttonLabels[i]);
buttons[i].setBounds(x, y, buttonWidth, buttonHeight);
frame.add(buttons[i]);

x += buttonWidth;

if (x >= 250) {
x = 10;
y += buttonHeight;
}
}

// Membuat event handler untuk tombol


ActionListener buttonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command = ((JButton) e.getSource()).getText();

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);
}
}
};

for (JButton button : buttons) {


button.addActionListener(buttonListener);
}

// Menampilkan frame
frame.setVisible(true);
}

// Metode untuk mengevaluasi ekspresi matematika


private static double evaluateExpression(String expression) {
// Anda dapat menggunakan pustaka matematika seperti JEP atau javax.script
// untuk mengevaluasi ekspresi matematika yang lebih kompleks.
try {
return (double) new
ScriptEngineManager().getEngineByName("JavaScript").eval(expression);
} catch (Exception e) {
throw new IllegalArgumentException("Ekspresi tidak valid");
}
}
}

You might also like