calculadora en jbuilder
Publicado por Nea! (2 intervenciones) el 10/09/2007 23:18:24
Hola a todos..!!!
mi problema es el siguiente me dejaron como trabajo hacer una calculadora como la de windows y lo q quiero hecer es prograamr mis botones para q sumen, resten ,multi.... constantemente y no solo sumen 2 valores.... el boton sumar funciona perfectamente pero... los otros... tienen unos errores bien raros...!!
y ademas quieren q la calculadora pueda ejacutar operaciones desd el teclado y se supone q ocupo eventos como keypressed, keytyped o keystroke.... no se
el problema es q no sse como utilizarlo ....!!! :S .... :@ jejeje
he aqui el codigo de mi calculadora..!!!
package ejemplo1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.TitledBorder;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyListener;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author Lastenia Morell
* @version 1.0
*/
public class otrowinmain extends JFrame {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JTextField txtvalor = new JTextField();
JButton btn1 = new JButton();
JButton btn2 = new JButton();...
public otrowinmain() {
try
{
jbInit();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
//probando no tengo idea alguna de como utilizar esto....!!! por eso solo esta como etiqueta!!! :S.....!!!!!!!!!!!!!
/*public class Jtexfield extends JFrame implements KeyListener {
private String linea1 = "", linea2 = "";
private String linea3 = "";
// lista.addKeyListener(this);
public void keyPressed(KeyEvent e) {
linea1 = "Tecla presionada: " + e.getKeyText(e.getKeyCode());
seteaLineas2y3(e);
}
public void keyReleased(KeyEvent e) {
linea1 = "Tecla soltada: " + e.getKeyText(e.getKeyCode());
seteaLineas2y3(e);
}
public void keyTyped(KeyEvent e) {
linea1 = "Tecla tipeada: " + e.getKeyChar();
seteaLineas2y3(e);
}
private void seteaLineas2y3(KeyEvent e) {
linea2 = "Esta tecla, " + ( e.isActionKey() ? "" : "no " ) + "es una tecla de acción";
String temp = e.getKeyModifiersText( e.getModifiers() );
linea3 = "Modifica tecla presionada: " + ( temp.equals( "" ) ? "no" : temp );
txtvalor.setText(linea1 + "\n" + linea2 + "\n" + linea3 + "\n" );
}
/*public void keyTyped(KeyEvent e) {
displayInfo(e, "KEY TYPED: ");
}
public void keyPressed(KeyEvent e) {
displayInfo(e, "KEY PRESSED: ");
}
public void keyReleased(KeyEvent e) {
displayInfo(e, "KEY RELEASED: ");
}*/
/* protected void displayInfo(KeyEvent e, String s) {
char c = e.getKeyChar();
int keyCode = e.getKeyCode();
int modifiers = e.getModifiers();
}
}*/
public void btnac_actionPerformed(ActionEvent e) {//boton de limpiar
txtvalor.setText("");
numero = "";
acumulado = 0;
}
public void btn1_actionPerformed(ActionEvent e) {
numero+= 1;
txtvalor.setText(numero);
}
public void btnmulti_actionPerformed(ActionEvent e) {
acumulado= acumulado* Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
//numero = txtvalor.getText();
acumulado = Integer.parseInt(numero);
boton =3;
numero = "";
}
public void btnsuma_actionPerformed(ActionEvent e) {
acumulado += Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
numero = txtvalor.getText();
acumulado = Integer.parseInt(numero);
boton = 1;
numero = "";
}
public void btndividir_actionPerformed(ActionEvent e) {
numero=txtvalor.getText();
acumulado /= Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
boton = 4;
acumulado = Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
//Mantiene el valor acumulado en pantalla
numero = "";
}
public void btnigual_actionPerformed(ActionEvent e) {
if (boton == 0) {
txtvalor.setText("0");
acumulado =0;
numero = "";
}
if (boton == 1) {
acumulado += Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
acumulado = 0;
numero = "";
}
if (boton == 2) {
acumulado -= Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
acumulado = 0;
numero = "";
}
if (boton == 3) {
acumulado *= Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
acumulado = 0;
numero = "";
}
if (boton == 4) {
acumulado /= Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
acumulado = 0;
numero = "";
}
}
class otrowinmain_btndividir_actionAdapter implements
ActionListener {
private otrowinmain adaptee;
otrowinmain_btndividir_actionAdapter(otrowinmain adaptee) {
this.adaptee = adaptee;
}
....
public void btn2_actionPerformed(ActionEvent e) {
numero += 2;
txtvalor.setText(numero);
}
public void btn3_actionPerformed(ActionEvent e) {
numero += 3;
txtvalor.setText(numero);
}
public void btn4_actionPerformed(ActionEvent e) {
numero += 4;
txtvalor.setText(numero);
}
public void btn6_actionPerformed(ActionEvent e) {
numero += 6;
txtvalor.setText(numero);
}
public void btn7_actionPerformed(ActionEvent e) {
numero += 7;
txtvalor.setText(numero);
}
public void btn8_actionPerformed(ActionEvent e) {
numero += 8;
txtvalor.setText(numero);
}
public void btn9_actionPerformed(ActionEvent e) {
numero += 9;
txtvalor.setText(numero);
}
public void btn0_actionPerformed(ActionEvent e) {
numero += 0;
txtvalor.setText(numero);
}
public void btnresta_actionPerformed(ActionEvent e) {
acumulado= acumulado-(Integer.parseInt(numero));
txtvalor.setText(Integer.toString(acumulado));
acumulado = Integer.parseInt(numero);
numero = txtvalor.getText();
boton = 2;
numero = "";
}
public void jMenuItem1_actionPerformed(ActionEvent e) {
this.dispose();
}
public void jButton2_actionPerformed(ActionEvent e) {
numero += 5;
txtvalor.setText(numero);
}
public void txtvalor_actionPerformed(ActionEvent e) {
}
public void txtvalor_keyPressed(KeyEvent e) {
??????
}
}
...
}
please someone help me.....!!!!!!!!!!
Atte Lastenia Morell
mi problema es el siguiente me dejaron como trabajo hacer una calculadora como la de windows y lo q quiero hecer es prograamr mis botones para q sumen, resten ,multi.... constantemente y no solo sumen 2 valores.... el boton sumar funciona perfectamente pero... los otros... tienen unos errores bien raros...!!
y ademas quieren q la calculadora pueda ejacutar operaciones desd el teclado y se supone q ocupo eventos como keypressed, keytyped o keystroke.... no se
el problema es q no sse como utilizarlo ....!!! :S .... :@ jejeje
he aqui el codigo de mi calculadora..!!!
package ejemplo1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.TitledBorder;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyListener;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author Lastenia Morell
* @version 1.0
*/
public class otrowinmain extends JFrame {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JTextField txtvalor = new JTextField();
JButton btn1 = new JButton();
JButton btn2 = new JButton();...
public otrowinmain() {
try
{
jbInit();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
//probando no tengo idea alguna de como utilizar esto....!!! por eso solo esta como etiqueta!!! :S.....!!!!!!!!!!!!!
/*public class Jtexfield extends JFrame implements KeyListener {
private String linea1 = "", linea2 = "";
private String linea3 = "";
// lista.addKeyListener(this);
public void keyPressed(KeyEvent e) {
linea1 = "Tecla presionada: " + e.getKeyText(e.getKeyCode());
seteaLineas2y3(e);
}
public void keyReleased(KeyEvent e) {
linea1 = "Tecla soltada: " + e.getKeyText(e.getKeyCode());
seteaLineas2y3(e);
}
public void keyTyped(KeyEvent e) {
linea1 = "Tecla tipeada: " + e.getKeyChar();
seteaLineas2y3(e);
}
private void seteaLineas2y3(KeyEvent e) {
linea2 = "Esta tecla, " + ( e.isActionKey() ? "" : "no " ) + "es una tecla de acción";
String temp = e.getKeyModifiersText( e.getModifiers() );
linea3 = "Modifica tecla presionada: " + ( temp.equals( "" ) ? "no" : temp );
txtvalor.setText(linea1 + "\n" + linea2 + "\n" + linea3 + "\n" );
}
/*public void keyTyped(KeyEvent e) {
displayInfo(e, "KEY TYPED: ");
}
public void keyPressed(KeyEvent e) {
displayInfo(e, "KEY PRESSED: ");
}
public void keyReleased(KeyEvent e) {
displayInfo(e, "KEY RELEASED: ");
}*/
/* protected void displayInfo(KeyEvent e, String s) {
char c = e.getKeyChar();
int keyCode = e.getKeyCode();
int modifiers = e.getModifiers();
}
}*/
public void btnac_actionPerformed(ActionEvent e) {//boton de limpiar
txtvalor.setText("");
numero = "";
acumulado = 0;
}
public void btn1_actionPerformed(ActionEvent e) {
numero+= 1;
txtvalor.setText(numero);
}
public void btnmulti_actionPerformed(ActionEvent e) {
acumulado= acumulado* Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
//numero = txtvalor.getText();
acumulado = Integer.parseInt(numero);
boton =3;
numero = "";
}
public void btnsuma_actionPerformed(ActionEvent e) {
acumulado += Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
numero = txtvalor.getText();
acumulado = Integer.parseInt(numero);
boton = 1;
numero = "";
}
public void btndividir_actionPerformed(ActionEvent e) {
numero=txtvalor.getText();
acumulado /= Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
boton = 4;
acumulado = Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
//Mantiene el valor acumulado en pantalla
numero = "";
}
public void btnigual_actionPerformed(ActionEvent e) {
if (boton == 0) {
txtvalor.setText("0");
acumulado =0;
numero = "";
}
if (boton == 1) {
acumulado += Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
acumulado = 0;
numero = "";
}
if (boton == 2) {
acumulado -= Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
acumulado = 0;
numero = "";
}
if (boton == 3) {
acumulado *= Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
acumulado = 0;
numero = "";
}
if (boton == 4) {
acumulado /= Integer.parseInt(numero);
txtvalor.setText(Integer.toString(acumulado));
acumulado = 0;
numero = "";
}
}
class otrowinmain_btndividir_actionAdapter implements
ActionListener {
private otrowinmain adaptee;
otrowinmain_btndividir_actionAdapter(otrowinmain adaptee) {
this.adaptee = adaptee;
}
....
public void btn2_actionPerformed(ActionEvent e) {
numero += 2;
txtvalor.setText(numero);
}
public void btn3_actionPerformed(ActionEvent e) {
numero += 3;
txtvalor.setText(numero);
}
public void btn4_actionPerformed(ActionEvent e) {
numero += 4;
txtvalor.setText(numero);
}
public void btn6_actionPerformed(ActionEvent e) {
numero += 6;
txtvalor.setText(numero);
}
public void btn7_actionPerformed(ActionEvent e) {
numero += 7;
txtvalor.setText(numero);
}
public void btn8_actionPerformed(ActionEvent e) {
numero += 8;
txtvalor.setText(numero);
}
public void btn9_actionPerformed(ActionEvent e) {
numero += 9;
txtvalor.setText(numero);
}
public void btn0_actionPerformed(ActionEvent e) {
numero += 0;
txtvalor.setText(numero);
}
public void btnresta_actionPerformed(ActionEvent e) {
acumulado= acumulado-(Integer.parseInt(numero));
txtvalor.setText(Integer.toString(acumulado));
acumulado = Integer.parseInt(numero);
numero = txtvalor.getText();
boton = 2;
numero = "";
}
public void jMenuItem1_actionPerformed(ActionEvent e) {
this.dispose();
}
public void jButton2_actionPerformed(ActionEvent e) {
numero += 5;
txtvalor.setText(numero);
}
public void txtvalor_actionPerformed(ActionEvent e) {
}
public void txtvalor_keyPressed(KeyEvent e) {
??????
}
}
...
}
please someone help me.....!!!!!!!!!!
Atte Lastenia Morell
Valora esta pregunta


0