Caja registradora
package Capitulo_A11;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class _6_Ejercicio_11_4_ extends JFrame implements ActionListener {
private int suma = 0;
private JButton botonSuma, botonBorrar;
private JLabel Suma;
private JTextField campo1, campoSuma;
public static void main(String[] args) {
_6_Ejercicio_11_4_ marco = new _6_Ejercicio_11_4_();
marco.setSize(250, 150);
marco.setGUI();
marco.setVisible(true);
}
public void setGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container ventana = getContentPane();
ventana.setLayout(null);
campo1 = new JTextField();
campo1.setBounds(10, 10, 220, 20);
ventana.add(campo1);
Suma = new JLabel("Suma");
Suma.setBounds(30, 30, 100, 20);
ventana.add(Suma);
campoSuma = new JTextField();
campoSuma.setBounds(10, 50, 220, 20);
ventana.add(campoSuma);
botonSuma = new JButton("Sumar");
botonSuma.setBounds(10, 80, 100, 20);
ventana.add(botonSuma);
botonSuma.addActionListener(this);
botonBorrar = new JButton("Borrar");
botonBorrar.setBounds(120, 80, 100, 20);
ventana.add(botonBorrar);
botonBorrar.addActionListener(this);
}
public void actionPerformed (ActionEvent event) {
Object origen = event.getSource();
if (origen == botonSuma) {
int a = Integer.parseInt(campo1.getText());
suma = suma + a;
}
if (origen == botonBorrar) {
campo1.setText("");
campoSuma.setText("");
suma = 0;
}
campoSuma.setText(Integer.toString(suma));
}
}