Untitled

 avatar
unknown
java
2 years ago
5.9 kB
2
Indexable
package cal;

import com.sun.source.tree.BreakTree;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Cal extends JFrame implements ActionListener {

    static JButton b1, b2, b3, b4, b5, b6, b7, b8, b9;
    static JButton b11, b12, b13, b14, b15, b16;
    static JTextField t1;
    static char op = 'a';
    static int number1, number2;

    Cal() {
        b1 = new JButton("1");
        this.add(b1);
        b2 = new JButton("2");
        this.add(b2);
        b3 = new JButton("3");
        this.add(b3);
        b4 = new JButton("4");
        this.add(b4);
        b5 = new JButton("5");
        this.add(b5);
        b6 = new JButton("6");
        this.add(b6);
        b7 = new JButton("7");
        this.add(b7);
        b8 = new JButton("8");
        this.add(b8);
        b9 = new JButton("9");
        this.add(b9);
        b11 = new JButton("+");
        this.add(b11);
        b12 = new JButton("-");
        this.add(b12);
        b13 = new JButton("*");
        this.add(b13);
        b14 = new JButton("/");
        this.add(b14);
        b15 = new JButton("Clean");
        this.add(b15);
        b16=new JButton("=");
        this.add(b16);
        t1 = new JTextField();
        t1.setText("");
        this.add(t1);
        t1.setEditable(false);
        this.setLayout(null);
        t1.setBounds(0, 0, 350, 80);
        b1.setBounds(0, 85, 100, 100);
        b2.setBounds(110, 85, 100, 100);
        b3.setBounds(220, 85, 100, 100);
        b4.setBounds(0, 190, 100, 100);
        b5.setBounds(110, 190, 100, 100);
        b6.setBounds(220, 190, 100, 100);
        b7.setBounds(0, 300, 100, 100);
        b8.setBounds(110, 300, 100, 100);
        b9.setBounds(220, 300, 100, 100);
        b11.setBounds(0, 405, 50, 50);
        b12.setBounds(60, 405, 50, 50);
        b13.setBounds(120, 405, 50, 50);
        b14.setBounds(180, 405, 50, 50);
        b15.setBounds(240, 405, 90, 50);
        b16.setBounds(0, 455, 50, 50);
        
        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);
        b11.addActionListener(this);
        b12.addActionListener(this);
        b13.addActionListener(this);
        b14.addActionListener(this);
        b15.addActionListener(this);
        b16.addActionListener(this);
        
        this.setTitle("cal");
        this.setBounds(200,200,350, 570);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new Cal();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1) {
            t1.setText(t1.getText() + "1");
        }
        if (e.getSource() == b2) {
            t1.setText(t1.getText() + "2");
        }
        if (e.getSource() == b3) {
            t1.setText(t1.getText() + "3");
        }
        if (e.getSource() == b4) {
            t1.setText(t1.getText() + "4");
        }
        if (e.getSource() == b5) {
            t1.setText(t1.getText() + "5");
        }
        if (e.getSource() == b6) {
            t1.setText(t1.getText() + "6");
        }
        if (e.getSource() == b7) {
            t1.setText(t1.getText() + "7");
        }
        if (e.getSource() == b8) {
            t1.setText(t1.getText() + "8");
        }
        if (e.getSource() == b9) {
            t1.setText(t1.getText() + "9");
        }
        if (e.getSource() == b11) {
             if(t1.getText()!=""){
            op = '+';
            number1=Integer.parseInt(t1.getText());
                        t1.setText("");

        }}
        if (e.getSource() == b12) {
             if(t1.getText()!=""){
            op = '-';
            number1=Integer.parseInt(t1.getText());
                        t1.setText("");

        }}
        if (e.getSource() == b13) {
             if(t1.getText()!=""){
            op = '/';
            number1=Integer.parseInt(t1.getText());
        t1.setText("");}}
        if (e.getSource() == b14) {
             if(t1.getText()!=""){
            op = '*';
            number1=Integer.parseInt(t1.getText());
            t1.setText("");
        }}
        if (e.getSource() == b15) {
            
            t1.setText("");
            number1 = 0;
            number2 = 0;
            op = 'a';
        }
        if (e.getSource()==b16){
            if(t1.getText()!=""){
        number2 = Integer.parseInt(t1.getText());
        if(number1!=0&&number2!=0){
            calculator(number1, number2);
            t1.setText("");
            number1 = 0;
            number2 = 0;
            op = 'a';}
            }
        }
    }

    public static void calculator(int n1, int n2) {
        switch (op) {
            case '+':
                JOptionPane.showMessageDialog(null, "result = " + (n1 + n2));
                break;
            case '-':
                JOptionPane.showMessageDialog(null, "result = " + (n1 - n2));
                break;
            case '*':
                JOptionPane.showMessageDialog(null, "result = " + (n1 * n2));
                break;
            case '/':
                JOptionPane.showMessageDialog(null, "result = " + (n1 / n2));
                break;
            default:
                JOptionPane.showMessageDialog(null, "no operation");

        }
    }

}
Editor is loading...