Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
9.0 kB
3
Indexable
Never
import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.util.regex.Pattern;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class calculator {


    public static void main(String [] args){

    
        JFrame jframe = new JFrame();
        jframe.setTitle("Calculator");
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setSize(345, 330);
        jframe.setResizable(false);
        
        JPanel jpanel = new JPanel();
        jpanel.setLayout(null);
        jframe.setContentPane(jpanel);

        JTextField jt = new JTextField(10);
        jt.setBounds(20, 20, 250, 50);
        jt.setFont(new Font("Dialog", Font.BOLD, 20));
        jt.setEditable(false);
        jt.setHorizontalAlignment(JTextField.RIGHT);
        jpanel.add(jt);

        //Switch
        JRadioButton on = new JRadioButton("On");
        on.setBounds(270, 25, 50, 20);
        jpanel.add(on);

        JRadioButton off = new JRadioButton("Off");
        off.setBounds(270, 50, 50, 20);
        jpanel.add(off);

        //numbers
        JButton num1 = new JButton("1");
        num1.setBounds(20, 80, 50, 40);
        jpanel.add(num1);

        JButton num2 = new JButton("2");
        num2.setBounds(80, 80, 50, 40);
        jpanel.add(num2);

        JButton num3 = new JButton("3");
        num3.setBounds(140, 80, 50, 40);
        jpanel.add(num3);

        JButton num4 = new JButton("4");
        num4.setBounds(20, 130, 50, 40);
        jpanel.add(num4);

        JButton num5 = new JButton("5");
        num5.setBounds(80, 130, 50, 40);
        jpanel.add(num5);

        JButton num6 = new JButton("6");
        num6.setBounds(140, 130, 50, 40);
        jpanel.add(num6);

        JButton num7 = new JButton("7");
        num7.setBounds(20, 180, 50, 40);
        jpanel.add(num7);

        JButton num8 = new JButton("8");
        num8.setBounds(80, 180, 50, 40);
        jpanel.add(num8);

        JButton num9 = new JButton("9");
        num9.setBounds(140, 180, 50, 40);
        jpanel.add(num9);

        JButton num0 = new JButton("0");
        num0.setBounds(200, 180, 50, 40);
        jpanel.add(num0);

        //operations
        JButton plus = new JButton("+");
        plus.setBounds(200, 80, 50, 40);
        jpanel.add(plus);

        JButton min = new JButton("-");
        min.setBounds(260, 80, 50, 40);
        jpanel.add(min);

        JButton div = new JButton("/");
        div.setBounds(200, 130, 50, 40);
        jpanel.add(div);

        JButton mul = new JButton("*");
        mul.setBounds(260, 130, 50, 40);
        jpanel.add(mul);

        JButton clear = new JButton("C");
        clear.setBounds(260, 180, 50, 40);
        jpanel.add(clear);

        JButton eq = new JButton("=");
        eq.setBounds(20, 230, 290, 40);
        jpanel.add(eq);

        //ACTIONS

        num1.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                int num = Integer.parseInt(jt.getText());
                if(num==0){
                  jt.setText("1");
                }else{
                  jt.setText(jt.getText()+"1")
            }
        });
        num2.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
              int num = Integer.parseInt(jt.getText());
               if(num==0){
                  jt.setText("2");
               }else{
                 jt.setText(jt.getText()+"2")
                }
            }
        });
        num3.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
              int num = Integer.parseInt(jt.getText());
                if(num==0){
                  jt.setText("3");
                }else{
                  jt.setText(jt.getText()+"3")
            }
            }
        });
        num4.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                int num = Integer.parseInt(jt.getText());
                if(num==0){

                  jt.setText("4");
                }else{
                  jt.setText(jt.getText()+"4")
            }
            }
        });
        num5.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                int num = Integer.parseInt(jt.getText());
                if(num==0){
                  jt.setText("5");
                }else{
                  jt.setText(jt.getText()+"5")
            }
            }
        });
        num6.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                int num = Integer.parseInt(jt.getText());
                if(num==0){
                  jt.setText("6");
                }else{
                  jt.setText(
                    jt.getText()+"6")
            }
            }
        });
        num7.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                int num = Integer.parseInt(jt.getText());
                if(num==0){
                  jt.setText("7");
                }else{
                  jt.setText(jt.getText()+"7")
            }
            }
        });
        num8.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                int num = Integer.parseInt(jt.getText());
                if(num==0){
                  jt.setText("8");
                }else{jt.setText(jt.getText()+"8")
            }
            }
        });
        num9.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                int num = Integer.parseInt(jt.getText());
                if(num==0){
                  jt.setText("9");
                }else{jt.setText(jt.getText()+"9")
            }
            }
        });
        num0.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                int num = Integer.parseInt(jt.getText());
                if(num==0){
                  jt.setText("0");
                }else{jt.setText(jt.getText()+"0")
            }
        });
        clear.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                jt.setText("");
                first = 0;
                sec = 0;
            }
        });
        plus.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                first = Double.parseDouble(jt.getText());
                op = "+";
            }
        });
        min.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                first = Double.parseDouble(jt.getText());
                op = "-";
            }
        });
        div.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                first = Double.parseDouble(jt.getText());
                op = "/";
            }
        });
        mul.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                first = Double.parseDouble(jt.getText());
                op = "*";
            }
        });
        eq.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
              sec = Double.parseDouble(jt.getText());
              
              if(op.equals("+"){
                first = first + sec;
                jt.setText(String.valueOf(first));
              }else if(op.equals("-"){
                first = first - sec;
                jt.setText(String.valueOf(first));
              }else if(op.equals("/"){
                first = first / sec;
                jt.setText(String.valueOf(first));
              }else if(op.equals("*"){
                first = first * sec;
                jt.setText(String.valueOf(first));
              }
                
            }
        });

        //set frame true
        jframe.setVisible(true);
    }
    public double first = 0;
    public double sec = 0;
    public string op = "";
    
}