Calculadora Codigo
Calculadora Codigo
Calculadora Codigo
package clasesfuentes;
import java.awt.Choice;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
/**
* @author Personal
*/
int numero1,numero2;
int resultado=0;
int operacion=0;
public CalculadoraGui(){
super("MI Calculadora");
setLayout(null);
ch1.addItem("Suma");
ch1.addItem("Resta");
ch1.addItem("Multipicacion");
ch1.addItem("Division");
this.l1.setBounds(100,15,90,23);
this.add(l1);
this.l2.setBounds(10,55,50,23);
this.add(l2);
this.add(t1);
this.l3.setBounds(10,95,50,23);
this.add(l3);
this.t2.setBounds(100,95,90,23);
this.add(t2);
this.l4.setBounds(10,135,90 ,23);
this.add(l4);
this.ch1.setBounds(100,135,90,23);
this.add(ch1);
this.l5.setBounds(10,175,90,23);
this.add(l5);
this.t3.setBounds(100,175,90,23);
this.add(t3);
this.b1.setBounds(10,215,170,23);
this.b1.addActionListener(this);
this.add(b1);
this.t3.setEditable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(240,290);
this.setResizable(false);
this.setVisible(true);
if(arg0.getSource()==b1){
operacion = ch1.getSelectedIndex();
numero1 = Integer.parseInt(t1.getText());
numero2 = Integer.parseInt(t2.getText());
resultado = cal.calcular();
t3.setText(""+resultado);
t1.requestFocus();
resultado=0;
}
}
}
Cálculos
package clasesfuentes;
/**
* @author joseomar
*/
int opera;
int num1;
int num2;
this.num1 = n1;
this.num2 = n2;
this.opera = op;
int calcular(){
int res = 0;
if(opera == 0){
if(opera == 1){
if(opera == 2){
if(opera == 3){
if(num2>0){
return(res);
}
Calcular
package guis;
/**
* @author UT
*/
/**
*/
public Calcular() {
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
*/
@SuppressWarnings("unchecked")
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Callcular");
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Calculadora",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 16))); //
NOI18N
jLabel1.setText("Número 1:");
jLabel2.setText("Número 2:");
jLabel3.setText("Operación:");
btncalcular.setText("Calcular");
btncalcular.addActionListener(new java.awt.event.ActionListener() {
btncalcularActionPerformed(evt);
});
jLabel4.setText("Resultado:");
txtresultado.setEditable(false);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addComponent(txtresultado))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(txtnumero1)
.addComponent(txtnumero2)
.addComponent(btncalcular, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(58, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtnumero1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtnumero2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(choperacion, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(btncalcular)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtresultado, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(46, Short.MAX_VALUE))
);
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(63, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(64, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
int n1 = Integer.parseInt(txtnumero1.getText());
int n2 = Integer.parseInt(txtnumero2.getText());
int resultado = 0;
if(operacion == 0){
resultado = n1 + n2;
if(operacion == 1){
resultado = n1 - n2;
if(operacion == 2){
resultado = n1 * n2;
if(operacion == 3){
resultado = n1 / n2;
txtresultado.setText(""+resultado);
/**
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
*/
try {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
java.util.logging.Logger.getLogger(Calcular.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(Calcular.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(Calcular.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
new Calcular().setVisible(true);
});
}
Valaidar
/*
*/
package guis;
/**
* @author UT
*/
/**
*/
public Validar() {
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
*/
@SuppressWarnings("unchecked")
setTitle("Validar");
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
);
pack();
}// </editor-fold>
/**
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
*/
try {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
java.util.logging.Logger.getLogger(Validar.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(Validar.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(Validar.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(Validar.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
new Validar().setVisible(true);
});