Java Calculator [duplicate]

-4

I have a creation block here pfvr help me, I would like to put a "AC" button to reset the result can anyone give me an idea?    My code looks like this:

private JButton numero0;
private JButton numero1;
private JButton numero2;
private JButton numero3;
private JButton numero4;
private JButton numero5;
private JButton numero6;
private JButton numero7;
private JButton numero8;
private JButton numero9;
private JButton somar;
private JButton diminuir;
private JButton igual;
private JButton ac;
private JButton multiplicar;
private JButton dividir;
private JTextField display;
private int leitura;
private int memoria;
private char operacao;

public Calculadora() {
    this.setTitle("Exemplo Botão Somar");
    this.setBounds(0, 0, 267, 235);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.getContentPane().setLayout(null);

    leitura = 0;
    memoria = 0;

    dividir = new JButton();
    dividir.setText("/");
    dividir.setBounds(195, 220, 45, 45);
    this.add(dividir);

    multiplicar = new JButton();
    multiplicar.setText("*");
    multiplicar.setBounds(195, 170, 45, 45);
    this.add(multiplicar);

    diminuir = new JButton();
    diminuir.setText("-");
    diminuir.setBounds(195, 120, 45, 45);
    this.add(diminuir);

    somar = new JButton();
    somar.setText("+");
    somar.setBounds(195, 70, 45, 45);
    this.add(somar);

    igual = new JButton();
    igual.setText("=");
    igual.setBounds(20, 270, 200, 45);
    this.add(igual);

    ac = new JButton();
    ac.setText("ac");
    ac.setBounds(140, 220, 45, 45);
    this.add(ac);

    numero1 = new JButton();
    numero1.setText("1");
    numero1.setBounds(20, 70, 45, 45);
    this.add(numero1);

    numero2 = new JButton();
    numero2.setText("2");
    numero2.setBounds(80, 70, 45, 45);
    this.add(numero2);

    numero3 = new JButton();
    numero3.setText("3");
    numero3.setBounds(140, 70, 45, 45);
    this.add(numero3);

    numero4 = new JButton();
    numero4.setText("4");
    numero4.setBounds(20, 120, 45, 45);
    this.add(numero4);

    numero5 = new JButton();
    numero5.setText("5");
    numero5.setBounds(80, 120, 45, 45);
    this.add(numero5);

    numero6 = new JButton();
    numero6.setText("6");
    numero6.setBounds(140, 120, 45, 45);
    this.add(numero6);

    numero7 = new JButton();
    numero7.setText("7");
    numero7.setBounds(20, 170, 45, 45);
    this.add(numero7);

    numero8 = new JButton();
    numero8.setText("8");
    numero8.setBounds(80, 170, 45, 45);
    this.add(numero8);

    numero9 = new JButton();
    numero9.setText("9");
    numero9.setBounds(140, 170, 45, 45);
    this.add(numero9);

    numero0 = new JButton();
    numero0.setText("0");
    numero0.setBounds(20, 220, 105, 45);
    this.add(numero0);

    display = new JTextField();
    display.setBounds(25, 25, 200, 30);
    this.add(display);

    numero0.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 0;
            display.setText(display.getText() + "0");
        }
    });

    numero1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 1;
            display.setText(display.getText() + "1");
        }
    });

    numero2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 2;
            display.setText(display.getText() + "2");
        }
    });

    numero3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 3;
            display.setText(display.getText() + "3");
        }
    });

    numero4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 4;
            display.setText(display.getText() + "4");
        }
    });

    numero5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 5;
            display.setText(display.getText() + "5");
        }
    });

    numero6.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 6;
            display.setText(display.getText() + "6");
        }
    });

    numero7.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 7;
            display.setText(display.getText() + "7");
        }
    });

    numero8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 8;
            display.setText(display.getText() + "8");
        }
    });

    numero9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 9;
            display.setText(display.getText() + "9");
        }
    });

    somar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            operacao = '+';
            memoria += leitura;
            leitura = 0;
            display.setText("");
        }
    });

    diminuir.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            operacao = '-';
            memoria += leitura;
            leitura = 0;
            display.setText("");
        }
    });

    dividir.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            operacao = '/';
            memoria += leitura;
            leitura = 0;
            display.setText("");
        }
    });

    multiplicar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            operacao = '*';
            memoria += leitura;
            leitura = 0;
            display.setText("");
        }
    });

    igual.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            switch (operacao) {
                case '+': {
                    memoria += leitura;
                    break;
                }
                case '-': {
                    memoria -= leitura;
                    break;
                }
                case '/': {
                    memoria /= leitura;
                    break;
                }
                case '*': {
                    memoria *= leitura;
                    break;
                }
            }
            leitura = 0;
            display.setText("" + memoria);
        }
    });
}

public static void main(String[] args) {
    Calculadora exemplo = new Calculadora();
    exemplo.setVisible(true);
}
    
asked by anonymous 12.09.2016 / 20:07

1 answer

4

As for the ac button, I think you just have to delete display when it is clicked:

ac.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        display.setText("");
    }
});
    
12.09.2016 / 20:22