package calculadoranova;
/**
*
* @author externo
*/
public class JFCalc extends javax.swing.JFrame {
char operacao;
String temp, numeroantes;
double num1, num2, resultado;
/** Creates new form JFCalc */
public JFCalc() {
initComponents();
}
private void escolheOperacao(char op) throws NumberFormatException {
operacao = op;
num1 = Double.parseDouble(jTVisor.getText());
jTVisor.setText("");
// 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jTVisor = new javax.swing.JTextField();
jBbackspace = new javax.swing.JButton();
jB7 = new javax.swing.JButton();
jB4 = new javax.swing.JButton();
jB1 = new javax.swing.JButton();
jB0 = new javax.swing.JButton();
jBclear = new javax.swing.JButton();
jBcontrol = new javax.swing.JButton();
jBmudarsinal = new javax.swing.JButton();
jB8 = new javax.swing.JButton();
jB5 = new javax.swing.JButton();
jB2 = new javax.swing.JButton();
jBponto = new javax.swing.JButton();
jB9 = new javax.swing.JButton();
jB6 = new javax.swing.JButton();
jBigual = new javax.swing.JButton();
jB3 = new javax.swing.JButton();
jBdivisao = new javax.swing.JButton();
jBmultiplicacao = new javax.swing.JButton();
jBsubtracao = new javax.swing.JButton();
jBadicao = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jBbackspace.setText("Bsp");
jBbackspace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBbackspaceActionPerformed(evt);
}
});
jB7.setText("7");
jB7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB7ActionPerformed(evt);
}
});
jB4.setText("4");
jB4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB4ActionPerformed(evt);
}
});
jB1.setText("1");
jB1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB1ActionPerformed(evt);
}
});
jB0.setText("0");
jB0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB0ActionPerformed(evt);
}
});
jBclear.setText("CE");
jBclear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBclearActionPerformed(evt);
}
});
jBcontrol.setText("Ctr");
jBcontrol.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBcontrolActionPerformed(evt);
}
});
jBmudarsinal.setText("+/-");
jBmudarsinal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBmudarsinalActionPerformed(evt);
}
});
jB8.setText("8");
jB8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB8ActionPerformed(evt);
}
});
jB5.setText("5");
jB5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB5ActionPerformed(evt);
}
});
jB2.setText("2");
jB2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB2ActionPerformed(evt);
}
});
jBponto.setText(".");
jBponto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBpontoActionPerformed(evt);
}
});
jB9.setText("9");
jB9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB9ActionPerformed(evt);
}
});
jB6.setText("6");
jB6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB6ActionPerformed(evt);
}
});
jBigual.setText("=");
jBigual.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBigualActionPerformed(evt);
}
});
jB3.setText("3");
jB3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB3ActionPerformed(evt);
}
});
jBdivisao.setText("/");
jBdivisao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBdivisaoActionPerformed(evt);
}
});
jBmultiplicacao.setText("*");
jBmultiplicacao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBmultiplicacaoActionPerformed(evt);
}
});
jBsubtracao.setText("-");
jBsubtracao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBsubtracaoActionPerformed(evt);
}
});
jBadicao.setText("+");
jBadicao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBadicaoActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTVisor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBbackspace, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
.addComponent(jB7, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
.addComponent(jB4, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
.addComponent(jB1, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
.addComponent(jB0, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jBclear, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBcontrol, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBmudarsinal, javax.swing.GroupLayout.PREFERRED_SIZE, 50, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jB5, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
.addComponent(jB8, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
.addComponent(jB2, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
.addComponent(jBponto, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBigual, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
.addComponent(jB6, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
.addComponent(jB3, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
.addComponent(jB9, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jBsubtracao, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
.addComponent(jBmultiplicacao, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
.addComponent(jBdivisao, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
.addComponent(jBadicao, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))))))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTVisor, 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(jBbackspace)
.addComponent(jBclear)
.addComponent(jBcontrol)
.addComponent(jBmudarsinal))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB7)
.addComponent(jB8)
.addComponent(jB9)
.addComponent(jBdivisao))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB4)
.addComponent(jB5)
.addComponent(jB6)
.addComponent(jBmultiplicacao))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB1)
.addComponent(jB2)
.addComponent(jB3)
.addComponent(jBsubtracao))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB0)
.addComponent(jBponto)
.addComponent(jBigual)
.addComponent(jBadicao))
.addContainerGap(20, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(37, 37, 37))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void jB1ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("1");
// TODO add your handling code here:
}
private void jBpontoActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado(".");
// TODO add your handling code here:
}
private void jB0ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("0");
// TODO add your handling code here:
}
private void jB2ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("2");
// 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 jBadicaoActionPerformed(java.awt.event.ActionEvent evt) {
escolheOperacao('+');
// TODO add your handling code here:
}
private void jBigualActionPerformed(java.awt.event.ActionEvent evt) {
num2=Double.parseDouble(jTVisor.getText());
switch(operacao){
case '+':
resultado=num1+num2;
break;
case '-':
resultado=num1-num2;
break;
case '*':
resultado=num1*num2;
break;
case '/':
resultado=num1/num2;
break;
}
jTVisor.setText(String.valueOf(resultado));
// TODO add your handling code here:
}
private void jBsubtracaoActionPerformed(java.awt.event.ActionEvent evt) {
escolheOperacao('-');
// TODO add your handling code here:
}
private void jBmultiplicacaoActionPerformed(java.awt.event.ActionEvent evt) {
escolheOperacao('*');
// TODO add your handling code here:
}
private void jBdivisaoActionPerformed(java.awt.event.ActionEvent evt) {
escolheOperacao('/');
// TODO add your handling code here:
}
private void jBclearActionPerformed(java.awt.event.ActionEvent evt) {
num2=0;
jTVisor.setText(String.valueOf(num1));
// TODO add your handling code here:
}
private void jBcontrolActionPerformed(java.awt.event.ActionEvent evt) {
num1=0;
num2=0;
jTVisor.setText("");
// TODO add your handling code here:
}
private void jBbackspaceActionPerformed(java.awt.event.ActionEvent evt) {
temp=numeroantes;
jTVisor.setText(temp);
// TODO add your handling code here:
}
private void jBmudarsinalActionPerformed(java.awt.event.ActionEvent evt) {
if(temp.startsWith("-")){
temp=temp.replace('-', '+');
}else if (temp.startsWith("+")){
temp=temp.replace('+', '-');
}else {
temp='-'+temp;
}
// TODO add your handling code here:
jTVisor.setText(temp);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFCalc().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 jBadicao;
private javax.swing.JButton jBbackspace;
private javax.swing.JButton jBclear;
private javax.swing.JButton jBcontrol;
private javax.swing.JButton jBdivisao;
private javax.swing.JButton jBigual;
private javax.swing.JButton jBmudarsinal;
private javax.swing.JButton jBmultiplicacao;
private javax.swing.JButton jBponto;
private javax.swing.JButton jBsubtracao;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTVisor;
// End of variables declaration
private void numeroDigitado(String numero) {
temp = jTVisor.getText();
numeroantes=temp;
temp = temp + numero;
jTVisor.setText(temp);
// TODO add your handling code here:
}
}