Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- class Calculator extends JFrame implements ActionListener{
- JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b_add,b_sub,b_multi,b_div,b_clear,b_del,b_dot,b_equals;
- JTextField text;
- static double variable = 0, result = 0;
- static String sResult = "";
- static int operationNumber = -1;
- Calculator(){
- setLayout(null);
- setResizable(false);
- setSize(350,550);
- text = new JTextField(10);
- text.setEditable(false);
- b0 = new JButton("0");
- b1 = new JButton("1");
- b2 = new JButton("2");
- b3 = new JButton("3");
- b4 = new JButton("4");
- b5 = new JButton("5");
- b6 = new JButton("6");
- b7 = new JButton("7");
- b8 = new JButton("8");
- b9 = new JButton("9");
- b_equals = new JButton("=");
- b_dot = new JButton(".");
- b_add = new JButton("+");
- b_sub = new JButton("-");
- b_multi = new JButton("*");
- b_div = new JButton("/");
- b_del = new JButton("del");
- b_clear = new JButton("clear");
- text.setBounds(20,20,290,70);
- b7.setBounds(30,110,60,60);
- b8.setBounds(100,110,60,60);
- b9.setBounds(170,110,60,60);
- b_div.setBounds(240,110,60,60);
- b4.setBounds(30,190,60,60);
- b5.setBounds(100,190,60,60);
- b6.setBounds(170,190,60,60);
- b_multi.setBounds(240,190,60,60);
- b1.setBounds(30,270,60,60);
- b2.setBounds(100,270,60,60);
- b3.setBounds(170,270,60,60);
- b_sub.setBounds(240,270,60,60);
- b_dot.setBounds(30,350,60,60);
- b0.setBounds(100,350,60,60);
- b_equals.setBounds(170,350,60,60);
- b_add.setBounds(240,350,60,60);
- b_clear.setBounds(30,430,130,60);
- b_del.setBounds(170,430,130,60);
- add(text);
- add(b0);
- add(b1);
- add(b2);
- add(b3);
- add(b4);
- add(b5);
- add(b6);
- add(b7);
- add(b8);
- add(b9);
- add(b_dot);
- add(b_add);
- add(b_sub);
- add(b_multi);
- add(b_div);
- add(b_del);
- add(b_clear);
- add(b_equals);
- setVisible(true);
- b0.addActionListener(this);
- b1.addActionListener(this);
- b2.addActionListener(this);
- b3.addActionListener(this);
- b4.addActionListener(this);
- b5.addActionListener(this);
- b6.addActionListener(this);
- b7.addActionListener(this);
- b8.addActionListener(this);
- b9.addActionListener(this);
- b_equals.addActionListener(this);
- b_dot.addActionListener(this);
- b_add.addActionListener(this);
- b_sub.addActionListener(this);
- b_multi.addActionListener(this);
- b_div.addActionListener(this);
- b_del.addActionListener(this);
- b_clear.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e){
- try {
- if (e.getSource() == b0)
- text.setText(text.getText().concat("0"));
- if (e.getSource() == b1)
- text.setText(text.getText().concat("1"));
- if (e.getSource() == b2)
- text.setText(text.getText().concat("2"));
- if (e.getSource() == b3)
- text.setText(text.getText().concat("3"));
- if (e.getSource() == b4)
- text.setText(text.getText().concat("4"));
- if (e.getSource() == b5)
- text.setText(text.getText().concat("5"));
- if (e.getSource() == b6)
- text.setText(text.getText().concat("6"));
- if (e.getSource() == b7)
- text.setText(text.getText().concat("7"));
- if (e.getSource() == b8)
- text.setText(text.getText().concat("8"));
- if (e.getSource() == b9)
- text.setText(text.getText().concat("9"));
- if (e.getSource() == b_dot)
- text.setText(text.getText().concat("."));
- else if (e.getSource() == b_add) {
- operationNumber = 1;
- variable = Double.parseDouble(text.getText());
- clearTextBox();
- } else if (e.getSource() == b_sub) {
- operationNumber = 2;
- variable = Double.parseDouble(text.getText());
- clearTextBox();
- } else if (e.getSource() == b_multi) {
- operationNumber = 3;
- variable = Double.parseDouble(text.getText());
- clearTextBox();
- } else if (e.getSource() == b_div) {
- operationNumber = 4;
- variable = Double.parseDouble(text.getText());
- clearTextBox();
- } else if (e.getSource() == b_del) {
- if (text.getText().length() != 0) {
- String temp = text.getText();
- clearTextBox();
- for (int i = 0; i < temp.length() - 1; i++)
- text.setText(text.getText() + temp.charAt(i));
- }
- } else if (e.getSource() == b_clear) {
- clearTextBox();
- } else if (e.getSource() == b_equals) {
- if (operationNumber == 1) {
- result = variable + Double.parseDouble(text.getText());
- }
- if (operationNumber == 2) {
- result = variable - Double.parseDouble(text.getText());
- }
- if (operationNumber == 3) {
- result = variable * Double.parseDouble(text.getText());
- }
- if (operationNumber == 4) {
- result = variable / Double.parseDouble(text.getText());
- }
- text.setText("" + result);
- }
- }
- catch (Exception err){}
- }
- void clearTextBox() {
- text.setText("");
- }
- }
- public class Main{
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable(){
- public void run(){
- new Calculator();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement