import java.awt.Font;
import javax.swing.*;
import java.awt.event.*;
public class Calculator {
public static void main(String[] args) {
new App();
}
}
class App implements ActionListener {
JFrame window;
JButton button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8, button_9, button_0,
button_add, button_minus, button_multiply, button_divide, button_equal, button_clear, button_00,
button_decimal;
JTextField textfield;
static double a = 0;
static double b = 0;
static double ans = 0;
static int operator = 0;
App() {
createWindow();
createTextField();
createButton_7();
createButton_8();
createButton_9();
createButton_divide();
createButton_4();
createButton_5();
createButton_6();
createButton_multiply();
createButton_1();
createButton_2();
createButton_3();
createButton_minus();
createButton_0();
createButton_00();
createButton_decimal();
createButton_add();
createButton_clear();
createButton_equal();
window.setVisible(true);
}
public void createWindow() {
window = new JFrame();
window.setSize(400, 370);
window.setLocationRelativeTo(null);
window.setLayout(null);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void createTextField() {
textfield = new JTextField();
textfield.setBounds(10, 10, 370, 50);
textfield.setFont(new Font("SansSerif", Font.BOLD, 30));
window.add(textfield);
}
public void createButton_7() {
button_7 = new JButton();
button_7.setText("7");
button_7.setBounds(10, 70, 70, 40);
button_7.addActionListener(this);
window.add(button_7);
}
public void createButton_8() {
button_8 = new JButton();
button_8.setText("8");
button_8.setBounds(100, 70, 70, 40);
button_8.addActionListener(this);
window.add(button_8);
}
public void createButton_9() {
button_9 = new JButton();
button_9.setText("9");
button_9.setBounds(190, 70, 70, 40);
button_9.addActionListener(this);
window.add(button_9);
}
public void createButton_divide() {
button_divide = new JButton();
button_divide.setText("/");
button_divide.setBounds(300, 70, 70, 40);
button_divide.addActionListener(this);
window.add(button_divide);
}
public void createButton_4() {
button_4 = new JButton();
button_4.setText("4");
button_4.setBounds(10, 120, 70, 40);
button_4.addActionListener(this);
window.add(button_4);
}
public void createButton_5() {
button_5 = new JButton();
button_5.setText("5");
button_5.setBounds(100, 120, 70, 40);
button_5.addActionListener(this);
window.add(button_5);
}
public void createButton_6() {
button_6 = new JButton();
button_6.setText("6");
button_6.setBounds(190, 120, 70, 40);
button_6.addActionListener(this);
window.add(button_6);
}
public void createButton_multiply() {
button_multiply = new JButton();
button_multiply.setText("*");
button_multiply.setBounds(300, 120, 70, 40);
button_multiply.addActionListener(this);
window.add(button_multiply);
}
public void createButton_1() {
button_1 = new JButton();
button_1.setText("1");
button_1.setBounds(10, 170, 70, 40);
button_1.addActionListener(this);
window.add(button_1);
}
public void createButton_2() {
button_2 = new JButton();
button_2.setText("2");
button_2.setBounds(100, 170, 70, 40);
button_2.addActionListener(this);
window.add(button_2);
}
public void createButton_3() {
button_3 = new JButton();
button_3.setText("3");
button_3.setBounds(190, 170, 70, 40);
button_3.addActionListener(this);
window.add(button_3);
}
public void createButton_minus() {
button_minus = new JButton();
button_minus.setText("-");
button_minus.setBounds(300, 170, 70, 40);
button_minus.addActionListener(this);
window.add(button_minus);
}
public void createButton_0() {
button_0 = new JButton();
button_0.setText("0");
button_0.setBounds(10, 220, 70, 40);
button_0.addActionListener(this);
window.add(button_0);
}
public void createButton_00() {
button_00 = new JButton();
button_00.setText("00");
button_00.setBounds(100, 220, 70, 40);
button_00.addActionListener(this);
window.add(button_00);
}
public void createButton_decimal() {
button_decimal = new JButton();
button_decimal.setText(".");
button_decimal.setBounds(190, 220, 70, 40);
button_decimal.addActionListener(this);
window.add(button_decimal);
}
public void createButton_add() {
button_add = new JButton();
button_add.setText("+");
button_add.setBounds(300, 220, 70, 90);
button_add.addActionListener(this);
window.add(button_add);
}
public void createButton_clear() {
button_clear = new JButton();
button_clear.setText("Clear");
button_clear.setBounds(10, 270, 160, 40);
button_clear.addActionListener(this);
window.add(button_clear);
}
public void createButton_equal() {
button_equal = new JButton();
button_equal.setText("=");
button_equal.setBounds(190, 270, 70, 40);
button_equal.addActionListener(this);
window.add(button_equal);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_0) {
textfield.setText(textfield.getText().concat("0"));
} else if (e.getSource() == button_1) {
textfield.setText(textfield.getText().concat("1"));
} else if (e.getSource() == button_2) {
textfield.setText(textfield.getText().concat("2"));
} else if (e.getSource() == button_3) {
textfield.setText(textfield.getText().concat("3"));
} else if (e.getSource() == button_4) {
textfield.setText(textfield.getText().concat("4"));
} else if (e.getSource() == button_5) {
textfield.setText(textfield.getText().concat("5"));
} else if (e.getSource() == button_6) {
textfield.setText(textfield.getText().concat("6"));
} else if (e.getSource() == button_7) {
textfield.setText(textfield.getText().concat("7"));
} else if (e.getSource() == button_8) {
textfield.setText(textfield.getText().concat("8"));
} else if (e.getSource() == button_9) {
textfield.setText(textfield.getText().concat("9"));
} else if (e.getSource() == button_decimal) {
textfield.setText(textfield.getText().concat("."));
} else if (e.getSource() == button_00) {
textfield.setText(textfield.getText().concat("00"));
}
if (e.getSource() == button_add) {
a = Float.parseFloat(textfield.getText());
textfield.setText("");
operator = 1;
} else if (e.getSource() == button_minus) {
a = Float.parseFloat(textfield.getText());
textfield.setText("");
operator = 2;
} else if (e.getSource() == button_multiply) {
a = Float.parseFloat(textfield.getText());
textfield.setText("");
operator = 3;
} else if (e.getSource() == button_divide) {
a = Float.parseFloat(textfield.getText());
textfield.setText("");
operator = 4;
} else if (e.getSource() == button_equal) {
switch (operator) {
case 1:
ans = a + Float.parseFloat(textfield.getText());
textfield.setText("" + ans);
break;
case 2:
ans = a - Float.parseFloat(textfield.getText());
textfield.setText("" + ans);
break;
case 3:
ans = a * Float.parseFloat(textfield.getText());
textfield.setText("" + ans);
break;
case 4:
ans = a / Float.parseFloat(textfield.getText());
textfield.setText("" + ans);
break;
}
a = 0;
b = 0;
ans = 0;
} else if (e.getSource() == button_clear) {
textfield.setText("");
a = 0;
b = 0;
ans = 0;
}
}
}
0 Comments