/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Cal.java
*
* Created on 24-feb-2011, 13:02:17
*/
package calculadorablog;
/**
*
* @author MAURICIO HERNANDEZ
*/
public class Cal extends javax.swing.JFrame {
/** Creates new form Cal */
char operacion;
String temp;
double num1, num2, resultado;
private String numeroantes;
public Cal() {
initComponents();
}
private void EscogerOperacion(char op) throws NumberFormatException {
operacion = op;
num1 = Double.parseDouble(jTResultado.getText());
jTResultado.setText("");
// TODO add your handling code here:
}
private void NumeroDigitado(String numero) {
temp = jTResultado.getText();
numeroantes = temp;
temp = temp + numero;
jTResultado.setText(temp);
// TODO add your handling code here:
}
/** 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")
//
private void initComponents() {
jButton10 = new javax.swing.JButton();
jButton24 = new javax.swing.JButton();
jTResultado = new javax.swing.JTextField();
jRHex = new javax.swing.JRadioButton();
jRDec = new javax.swing.JRadioButton();
jROct = new javax.swing.JRadioButton();
jRBin = new javax.swing.JRadioButton();
jBA = new javax.swing.JButton();
jBB = new javax.swing.JButton();
jBC = new javax.swing.JButton();
jBD = new javax.swing.JButton();
jBE = new javax.swing.JButton();
jBF = new javax.swing.JButton();
jB7 = new javax.swing.JButton();
jB8 = new javax.swing.JButton();
jB9 = new javax.swing.JButton();
jBDividir = new javax.swing.JButton();
jB4 = new javax.swing.JButton();
jB5 = new javax.swing.JButton();
jB6 = new javax.swing.JButton();
jBMultiplicar = new javax.swing.JButton();
jB1 = new javax.swing.JButton();
jB2 = new javax.swing.JButton();
jB3 = new javax.swing.JButton();
jBRestar = new javax.swing.JButton();
jB0 = new javax.swing.JButton();
jBpunto = new javax.swing.JButton();
jBSumar = new javax.swing.JButton();
jBIgual = new javax.swing.JButton();
jBCE = new javax.swing.JButton();
jBClear = new javax.swing.JButton();
jBreturn = new javax.swing.JButton();
jButton10.setText("jButton10");
jButton24.setText("jButton24");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTResultado.setFont(new java.awt.Font("Tahoma", 0, 36));
jTResultado.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jRHex.setText("Hex");
jRHex.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRHexActionPerformed(evt);
}
});
jRDec.setText("Dec");
jROct.setText("Oct");
jRBin.setText("Bin");
jBA.setBackground(new java.awt.Color(204, 204, 255));
jBA.setText("A");
jBA.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBAActionPerformed(evt);
}
});
jBB.setBackground(new java.awt.Color(204, 204, 255));
jBB.setText("B");
jBC.setBackground(new java.awt.Color(204, 204, 255));
jBC.setText("C");
jBD.setBackground(new java.awt.Color(204, 204, 255));
jBD.setText("D");
jBE.setBackground(new java.awt.Color(204, 204, 255));
jBE.setText("E");
jBF.setBackground(new java.awt.Color(204, 204, 255));
jBF.setText("F");
jB7.setBackground(new java.awt.Color(204, 204, 255));
jB7.setText("7");
jB7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB7ActionPerformed(evt);
}
});
jB8.setBackground(new java.awt.Color(204, 204, 255));
jB8.setText("8");
jB8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB8ActionPerformed(evt);
}
});
jB9.setBackground(new java.awt.Color(204, 204, 255));
jB9.setText("9");
jB9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB9ActionPerformed(evt);
}
});
jBDividir.setBackground(new java.awt.Color(204, 204, 255));
jBDividir.setText("/");
jBDividir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBDividirActionPerformed(evt);
}
});
jB4.setBackground(new java.awt.Color(204, 204, 255));
jB4.setText("4");
jB4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB4ActionPerformed(evt);
}
});
jB5.setBackground(new java.awt.Color(204, 204, 255));
jB5.setText("5");
jB5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB5ActionPerformed(evt);
}
});
jB6.setBackground(new java.awt.Color(204, 204, 255));
jB6.setText("6");
jB6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB6ActionPerformed(evt);
}
});
jBMultiplicar.setBackground(new java.awt.Color(204, 204, 255));
jBMultiplicar.setText("*");
jBMultiplicar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBMultiplicarActionPerformed(evt);
}
});
jB1.setBackground(new java.awt.Color(204, 204, 255));
jB1.setText("1");
jB1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB1ActionPerformed(evt);
}
});
jB2.setBackground(new java.awt.Color(204, 204, 255));
jB2.setText("2");
jB2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB2ActionPerformed(evt);
}
});
jB3.setBackground(new java.awt.Color(204, 204, 255));
jB3.setText("3");
jB3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB3ActionPerformed(evt);
}
});
jBRestar.setBackground(new java.awt.Color(204, 204, 255));
jBRestar.setText("-");
jBRestar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBRestarActionPerformed(evt);
}
});
jB0.setBackground(new java.awt.Color(204, 204, 255));
jB0.setText("0");
jB0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB0ActionPerformed(evt);
}
});
jBpunto.setBackground(new java.awt.Color(204, 204, 255));
jBpunto.setText(".");
jBSumar.setBackground(new java.awt.Color(204, 204, 255));
jBSumar.setText("+");
jBSumar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBSumarActionPerformed(evt);
}
});
jBIgual.setBackground(new java.awt.Color(204, 204, 255));
jBIgual.setText("=");
jBIgual.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBIgualActionPerformed(evt);
}
});
jBCE.setBackground(new java.awt.Color(204, 204, 255));
jBCE.setText("CE");
jBCE.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBCEActionPerformed(evt);
}
});
jBClear.setBackground(new java.awt.Color(204, 204, 255));
jBClear.setText("C");
jBreturn.setBackground(new java.awt.Color(204, 204, 255));
jBreturn.setText("Re");
jBreturn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBreturnActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTResultado, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jROct)
.addComponent(jRDec)
.addComponent(jRBin)
.addComponent(jRHex))
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jBD, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBC, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBB, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBA, javax.swing.GroupLayout.Alignment.LEADING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jB0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBpunto))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jB1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB3))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jB4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB6))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jB7)
.addComponent(jBCE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jB8)
.addComponent(jBClear))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jB9)
.addComponent(jBreturn)))))
.addComponent(jBF))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBSumar)
.addComponent(jBRestar, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
.addComponent(jBMultiplicar)
.addComponent(jBDividir, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
.addComponent(jBIgual))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jB1, jB2, jB3, jB4, jB5, jB6, jB7, jB8, jB9, jBA, jBB, jBC, jBCE, jBClear, jBD, jBE, jBF, jBIgual, jBMultiplicar, jBRestar, jBSumar, jBpunto, jBreturn});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jTResultado, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(15, 15, 15)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(jRHex)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRDec)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jROct)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRBin))
.addGroup(layout.createSequentialGroup()
.addComponent(jBA)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jBB)
.addComponent(jBCE)
.addComponent(jBClear)
.addComponent(jBreturn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBC)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBD)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBF))
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jBDividir)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBMultiplicar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBRestar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBSumar))
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB7)
.addComponent(jB8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jB9))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB4)
.addComponent(jB5)
.addComponent(jB6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB1)
.addComponent(jB2)
.addComponent(jB3))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB0)
.addComponent(jBIgual)
.addComponent(jBpunto))))
.addContainerGap())
);
pack();
}//
private void jBAActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jBCEActionPerformed(java.awt.event.ActionEvent evt) {
num2 = 0;
jTResultado.setText(String.valueOf(num1));
// TODO add your handling code here:
}
private void jB0ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("0");
}
private void jRHexActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jB2ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("2");
// TODO add your handling code here:
}
private void jB1ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("1");
// TODO add your handling code here:
}
private void jB3ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("3");
// TODO add your handling code here:
}
private void jB4ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("4");
// TODO add your handling code here:
}
private void jB5ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("5");
// TODO add your handling code here:
}
private void jB6ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("6");
// TODO add your handling code here:
}
private void jB7ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("7");
// TODO add your handling code here:
}
private void jB8ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("8");
// TODO add your handling code here:
}
private void jB9ActionPerformed(java.awt.event.ActionEvent evt) {
NumeroDigitado("9");
// TODO add your handling code here:
}
private void jBSumarActionPerformed(java.awt.event.ActionEvent evt) {
EscogerOperacion('+');
}
private void jBIgualActionPerformed(java.awt.event.ActionEvent evt) {
num2 = Double.parseDouble(jTResultado.getText());
switch (operacion){
case '+':
resultado = num1 + num2;
break;
case '-':
resultado = num1 - num2;
break;
case '*':
resultado = num1 * num2;
break;
case '/':
resultado = num1 / num2;
break;
}
jTResultado.setText(String.valueOf(resultado));
// TODO add your handling code here:
}
private void jBRestarActionPerformed(java.awt.event.ActionEvent evt) {
EscogerOperacion('-');
// TODO add your handling code here:
}
private void jBMultiplicarActionPerformed(java.awt.event.ActionEvent evt) {
EscogerOperacion('*'); // TODO add your handling code here:
}
private void jBDividirActionPerformed(java.awt.event.ActionEvent evt) {
EscogerOperacion('/'); // TODO add your handling code here:
}
private void jBreturnActionPerformed(java.awt.event.ActionEvent evt) {
temp = numeroantes;
jTResultado.setText(temp);
// TODO add your handling code here:
}
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 jB0;
private javax.swing.JButton jB1;
private javax.swing.JButton jB2;
private javax.swing.JButton jB3;
private javax.swing.JButton jB4;
private javax.swing.JButton jB5;
private javax.swing.JButton jB6;
private javax.swing.JButton jB7;
private javax.swing.JButton jB8;
private javax.swing.JButton jB9;
private javax.swing.JButton jBA;
private javax.swing.JButton jBB;
private javax.swing.JButton jBC;
private javax.swing.JButton jBCE;
private javax.swing.JButton jBClear;
private javax.swing.JButton jBD;
private javax.swing.JButton jBDividir;
private javax.swing.JButton jBE;
private javax.swing.JButton jBF;
private javax.swing.JButton jBIgual;
private javax.swing.JButton jBMultiplicar;
private javax.swing.JButton jBRestar;
private javax.swing.JButton jBSumar;
private javax.swing.JButton jBpunto;
private javax.swing.JButton jBreturn;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton24;
private javax.swing.JRadioButton jRBin;
private javax.swing.JRadioButton jRDec;
private javax.swing.JRadioButton jRHex;
private javax.swing.JRadioButton jROct;
private javax.swing.JTextField jTResultado;
// End of variables declaration
}