You are on page 1of 11

Santiago Valverde Ing.

Electrónica 13661-9
Programación II 07/07/2020 Ciclo 4

Calculadora código con herencia


public class cal extends javax.swing.JFrame {
int a,b,c,m;
/** Creates new form cal */
public cal() {
initComponents();
}

/** This method is called from within the constructor to


* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


dato1 = new javax.swing.JTextField();
dato2 = new javax.swing.JTextField();
resultado = new javax.swing.JLabel();
suma = new javax.swing.JButton();
resta = new javax.swing.JButton();
multiplicacion = new javax.swing.JButton();
division = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
borrar = new javax.swing.JButton();
memoria = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N


jLabel1.setForeground(new java.awt.Color(51, 51, 255));
jLabel1.setText("calculadora");

resultado.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N


resultado.setForeground(new java.awt.Color(0, 0, 255));

suma.setText("+");
suma.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sumaActionPerformed(evt);
}
});

resta.setText("-");
resta.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
restaActionPerformed(evt);
}
});

multiplicacion.setText("*");
multiplicacion.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
multiplicacionActionPerformed(evt);
}
});

division.setText("/");
division.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
divisionActionPerformed(evt);
}
});

jLabel3.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N


jLabel3.setForeground(new java.awt.Color(51, 0, 255));
jLabel3.setText("resultado");

borrar.setText("c");
borrar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
borrarActionPerformed(evt);
}
});

memoria.setText("m");
memoria.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
memoriaActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE,
133, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(105, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jLabel3))
.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILI
NG)
.addComponent(resultado,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
.addComponent(dato1,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
.addComponent(dato2,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE))
.addGap(8, 8, 8)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(memoria,
javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
.addComponent(borrar, 0, 0, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(suma,
javax.swing.GroupLayout.PREFERRED_SIZE, 48,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(resta,
javax.swing.GroupLayout.PREFERRED_SIZE, 48,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(multiplicacion,
javax.swing.GroupLayout.PREFERRED_SIZE, 50,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(division,
javax.swing.GroupLayout.PREFERRED_SIZE, 47,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addGroup(layout.createSequentialGroup()
.addComponent(dato1,
javax.swing.GroupLayout.PREFERRED_SIZE, 27,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(dato2,
javax.swing.GroupLayout.PREFERRED_SIZE, 25,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(memoria))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addGap(9, 9, 9)
.addComponent(resultado,
javax.swing.GroupLayout.PREFERRED_SIZE, 47,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(borrar))
.addGap(33, 33, 33)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(suma)
.addComponent(resta)
.addComponent(multiplicacion)
.addComponent(division))
.addContainerGap(27, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void sumaActionPerformed(java.awt.event.ActionEvent evt) {


a = Integer.parseInt(dato1.getText());
b = Integer.parseInt(dato2.getText());
c = a+b;
resultado.setText(Integer.toString(c));
m = a+b;
resultado.setText(Integer.toString(m));

// TODO add your handling code here:


}

private void restaActionPerformed(java.awt.event.ActionEvent evt) {


a = Integer.parseInt(dato1.getText());
b = Integer.parseInt(dato2.getText());
c= a-b;
resultado.setText(Integer.toString(c));
m = a-b;
resultado.setText(Integer.toString(m));

// TODO add your handling code here:


}

private void multiplicacionActionPerformed(java.awt.event.ActionEvent evt) {

a = Integer.parseInt(dato1.getText());
b = Integer.parseInt(dato2.getText());
c = a*b;
resultado.setText(Integer.toString(c));
m = a*b;
resultado.setText(Integer.toString(m));
// TODO add your handling code here:
}

private void divisionActionPerformed(java.awt.event.ActionEvent evt) {


a = Integer.parseInt(dato1.getText());
b = Integer.parseInt(dato2.getText());
c = a/b;
resultado.setText(Integer.toString(c));
m = a/b;
resultado.setText(Integer.toString(m));

// TODO add your handling code here:


}
private void borrarActionPerformed(java.awt.event.ActionEvent evt) {
dato1.setText(null);
dato2.setText(null);
resultado.setText(null);
// TODO add your handling code here:
}

private void memoriaActionPerformed(java.awt.event.ActionEvent evt) {


resultado.setText(Integer.toString(a));
resultado.setText(Integer.toString(b));
resultado.setText(Integer.toString(c));
resultado.setText(Integer.toString(a+b));
resultado.setText(Integer.toString(a-b));
resultado.setText(Integer.toString(a*b));
resultado.setText(Integer.toString(a/b));
resultado.setText(Integer.toString(m));
dato1.setText(Integer.toString(a));
dato2.setText(Integer.toString(b));

// TODO add your handling code here:


}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new cal().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton borrar;
private javax.swing.JTextField dato1;
private javax.swing.JTextField dato2;
private javax.swing.JButton division;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JButton memoria;
private javax.swing.JButton multiplicacion;
private javax.swing.JButton resta;
private javax.swing.JLabel resultado;
private javax.swing.JButton suma;
// End of variables declaration

}
Generate Code
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


dato1 = new javax.swing.JTextField();
dato2 = new javax.swing.JTextField();
resultado = new javax.swing.JLabel();
suma = new javax.swing.JButton();
resta = new javax.swing.JButton();
multiplicacion = new javax.swing.JButton();
division = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
borrar = new javax.swing.JButton();
memoria = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N


jLabel1.setForeground(new java.awt.Color(51, 51, 255));
jLabel1.setText("calculadora");

resultado.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N


resultado.setForeground(new java.awt.Color(0, 0, 255));
Ejecución

You might also like