Buscar
Social
Ofertas laborales ES

Foro sobre Java SE > Calculadora no muestra resultado

Hola mi consulta es que estoy programando una calculadora en JAVA FX y no me muestra el resultado. Les adjunto el codigo asi me dicen en donde me equivoque

GRACIAS

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package proyectocalc2;

/**
*
* @author victor
*/
public class CALCU extends javax.swing.JFrame {
String Memoria1;
String Signo;
String Memoria2;
private String memoria1;
public CALCU() {
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() {

TxtPantalla = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();
jButton18 = new javax.swing.JButton();
jButton19 = new javax.swing.JButton();
jButton20 = new javax.swing.JButton();
jButton21 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(23, 23, 245));

jButton2.setBackground(new java.awt.Color(0, 0, 0));
jButton2.setForeground(new java.awt.Color(255, 0, 0));
jButton2.setText("4");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setBackground(new java.awt.Color(0, 0, 0));
jButton3.setForeground(new java.awt.Color(255, 0, 0));
jButton3.setText("1");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton4.setBackground(new java.awt.Color(0, 0, 0));
jButton4.setForeground(new java.awt.Color(255, 0, 0));
jButton4.setText("7");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jButton5.setBackground(new java.awt.Color(0, 0, 0));
jButton5.setForeground(new java.awt.Color(255, 0, 0));
jButton5.setText("5");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

jButton6.setBackground(new java.awt.Color(0, 0, 0));
jButton6.setForeground(new java.awt.Color(255, 0, 0));
jButton6.setText("2");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});

jButton7.setBackground(new java.awt.Color(0, 0, 0));
jButton7.setForeground(new java.awt.Color(255, 0, 0));
jButton7.setText("8");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});

jButton8.setBackground(new java.awt.Color(0, 0, 0));
jButton8.setForeground(new java.awt.Color(255, 0, 0));
jButton8.setText("6");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});

jButton9.setBackground(new java.awt.Color(0, 0, 0));
jButton9.setForeground(new java.awt.Color(255, 0, 0));
jButton9.setText("3");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});

jButton10.setBackground(new java.awt.Color(0, 0, 0));
jButton10.setForeground(new java.awt.Color(255, 0, 0));
jButton10.setText("9");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});

jButton11.setBackground(new java.awt.Color(0, 0, 0));
jButton11.setForeground(new java.awt.Color(255, 0, 0));
jButton11.setText("0");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});

jButton12.setBackground(new java.awt.Color(0, 0, 0));
jButton12.setForeground(new java.awt.Color(255, 0, 0));
jButton12.setText("1/x");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});

jButton13.setBackground(new java.awt.Color(0, 0, 0));
jButton13.setForeground(new java.awt.Color(255, 0, 0));
jButton13.setText(".");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});

jButton14.setBackground(new java.awt.Color(0, 0, 0));
jButton14.setForeground(new java.awt.Color(255, 0, 0));
jButton14.setText("+");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton14ActionPerformed(evt);
}
});

jButton15.setBackground(new java.awt.Color(0, 0, 0));
jButton15.setForeground(new java.awt.Color(255, 0, 0));
jButton15.setText("-");
jButton15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton15ActionPerformed(evt);
}
});

jButton16.setBackground(new java.awt.Color(0, 0, 0));
jButton16.setForeground(new java.awt.Color(255, 0, 0));
jButton16.setText("*");
jButton16.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton16ActionPerformed(evt);
}
});

jButton17.setBackground(new java.awt.Color(0, 0, 0));
jButton17.setForeground(new java.awt.Color(255, 0, 0));
jButton17.setText("/");
jButton17.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton17ActionPerformed(evt);
}
});

jButton18.setBackground(new java.awt.Color(0, 0, 0));
jButton18.setForeground(new java.awt.Color(255, 0, 0));
jButton18.setText("CE");
jButton18.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton18ActionPerformed(evt);
}
});

jButton19.setBackground(new java.awt.Color(0, 0, 0));
jButton19.setForeground(new java.awt.Color(255, 0, 0));
jButton19.setText("C");
jButton19.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton19ActionPerformed(evt);
}
});

jButton20.setBackground(new java.awt.Color(0, 0, 0));
jButton20.setForeground(new java.awt.Color(255, 0, 0));
jButton20.setText("+/-");
jButton20.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton20ActionPerformed(evt);
}
});

jButton21.setBackground(new java.awt.Color(0, 0, 0));
jButton21.setForeground(new java.awt.Color(255, 0, 0));
jButton21.setText("=");
jButton21.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton21ActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(55, 55, 55)
.addComponent(TxtPantalla, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton20, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton18, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton19, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton21, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton19, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton18, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton20, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton21, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(TxtPantalla, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(12, Short.MAX_VALUE))
);

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

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TxtPantalla.setText(TxtPantalla.getText()+"1");
}

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TxtPantalla.setText(TxtPantalla.getText()+"2");
}

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TxtPantalla.setText(TxtPantalla.getText()+"3");
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TxtPantalla.setText(TxtPantalla.getText()+"4");
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TxtPantalla.setText(TxtPantalla.getText()+"5");
}

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TxtPantalla.setText(TxtPantalla.getText()+"6");
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TxtPantalla.setText(TxtPantalla.getText()+"7");
}

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TxtPantalla.setText(TxtPantalla.getText()+"8");
}

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TxtPantalla.setText(TxtPantalla.getText()+"9");
}

private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TxtPantalla.setText(TxtPantalla.getText()+"0");
}

private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String cadena;
cadena = TxtPantalla.getText();

if (cadena.length() <= 0)
{
TxtPantalla.setText("0.");
}
else
{
if (!existePunto(TxtPantalla.getText()))
{
TxtPantalla.setText(TxtPantalla.getText()+".");
}
}

}

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String cadena;
cadena = TxtPantalla.getText();
Double num;
if (cadena.length()>0)
{
num=1/(Double.parseDouble(cadena));
TxtPantalla.setText(num.toString());
}

}

private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Double num;
String cadena;
cadena = TxtPantalla.getText();
if (cadena.length()>0)
{
num = (Double.parseDouble (cadena))*(-1);
TxtPantalla.setText(num.toString());
}
}

private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String cadena;
cadena = TxtPantalla.getText();
if (cadena.length() > 0)
{
cadena = cadena.substring(0, cadena.length()-1);
TxtPantalla.setText(cadena);
}
}

private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TxtPantalla.setText("");
}

private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (!TxtPantalla.getText().equals(""))
{
Memoria1 = TxtPantalla.getText();
Signo = "+";
TxtPantalla.setText(TxtPantalla.getText()+"+");
}
}

private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (!TxtPantalla.getText().equals(""))
{
Memoria1 = TxtPantalla.getText();
Signo = "-";
TxtPantalla.setText(TxtPantalla.getText()+"-");
}
}

private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (!TxtPantalla.getText().equals(""))
{
Memoria1 = TxtPantalla.getText();
Signo = "*";
TxtPantalla.setText(TxtPantalla.getText()+"*");
}
}

private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (!TxtPantalla.getText().equals(""))
{
Memoria1 = TxtPantalla.getText();
Signo = "/";
TxtPantalla.setText(TxtPantalla.getText()+"/");
}
}

private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String resultado;
Memoria2 = TxtPantalla.getText();
if (!Memoria2.equals("resultado"))
{
resultado = calculadora (Memoria1,Memoria2,Signo);
TxtPantalla.setText(TxtPantalla.getText()+"resultado");
}
}

public static String calculadora (String memoria1, String memoria2, String signo)
{
Double resultado = 0.0;
String respuesta = null;
if (signo.equals("-"))
{
resultado = Double.parseDouble(memoria1) - Double.parseDouble((memoria2));
}
if (signo.equals("+"))
{
resultado = Double.parseDouble(memoria1) + Double.parseDouble((memoria2));
}
if (signo.equals("*"))
{
resultado = Double.parseDouble(memoria1) * Double.parseDouble((memoria2));
}
if (signo.equals("/"))
{
resultado = Double.parseDouble(memoria1) / Double.parseDouble((memoria2));
}
if (signo.equals("")
respuesta = resultado.toString();
return respuesta;
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<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.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(CALCU.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CALCU.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CALCU.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CALCU.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CALCU().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JTextField TxtPantalla;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton18;
private javax.swing.JButton jButton19;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton20;
private javax.swing.JButton jButton21;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
// End of variables declaration

public static boolean existePunto (String cadena)
{
boolean resultado = false;
for (int i=0; i<cadena.length(); i++)
{
if(cadena.substring(i, i+1).equals("."))
{
resultado=true;
break;
}
}
return resultado;
}


}

diciembre 16, 2016 | Unregistered Commenterfacu

Hola, he estado echandole un Ojo a tu codigo. Lo primero, comenzar por decirte, que lo que tu estas haciendo aquí, no es JAVA FX. Haces usos de componentes Swing, por tanto es una aplicación Swing y no java fx.

Ahora en cuanto a tu código y diseño, decirte que puedes tener problemas en la manera en la que lo estas haciendo, ya que si yo pongo 5+4+1 ya son 2 signos los que entran en juego y 3 operando, cosa que es muy común, hacer operaciones con mas de 2 operandos y mas de 1 signo.

Ahora la solucion:

Para solucionarlo y poder tener mas de 2 operandos y mas de 1 signo, lo mejor es evaluar la expresión matemática que el usuario ingrese en la pantalla, es decir si el usuario inserta en la pantalla 20+5-3+2*2 por ejemplo, nosotros evaluaremos esa expresion al completo.

Para ello, java dispone de una serie de objetos que nos permite evaluar expresiones matematicas. Estos son: ScriptEngineManager y ScriptEngine. Con estos objetos podemos evaluar expresiones matematicas en forma de cadena de texto.

Para tu código, el método del boton "=" debe quedar así:


private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {
ScriptEngineManager manager = new ScriptEngineManager();
//Crea un objeto ScriptEngine para evaluar expresiones matematicas con javascript
ScriptEngine motorScript = manager.getEngineByName("JavaScript");
//Lo siguiente crear la cadena con el script que vamos a ejecutar
//En este caso es el contenido del campo de texto
if(TxtPantalla.getText().length()!=0){
String script = TxtPantalla.getText();

try {

TxtPantalla.setText(script +" = " +motorScript.eval(script).toString());
} catch (ScriptException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Expresion matematica no valida.");
}
}
else{
JOptionPane.showMessageDialog(this, "Debe introducir una expresion matematica.");

}

}

Como ves, lo que hacemos, es coger la expresión completa, y evaluarla con el método eval, del objeto ScriptEngine, haciendo uso del lenguaje javascript en este caso. De esta forma, puedes evaluear expresiones con muchos operandos y varios signos. ya que de la forma en la que tu lo haces estas limitado a dos operandos y un signo.


Por otro lado, según tu lo tenias, no te funciona porque si por ejemplo yo escribo en la pantalla 5+3 y le doy al igual, tu estas intentando sumas (5) + (5+3) y para java 5+3 no es un numero y por tanto falla. Prueba la solución que te di y ya nos cuentas.

Un saludo

diciembre 18, 2016 | Registered Commenterloderain