Foros del Web » Programación para mayores de 30 ;) » Java »

Problema con ejercicio en java

Estas en el tema de Problema con ejercicio en java en el foro de Java en Foros del Web. Hola amigos, hace tiempo q leo el foro y hoy me he decidido postear... Resulta que me estoy iniciando en el mundo de java 2 ...
  #1 (permalink)  
Antiguo 04/07/2008, 10:21
 
Fecha de Ingreso: julio-2008
Mensajes: 2
Antigüedad: 15 años, 10 meses
Puntos: 0
Pregunta Problema con ejercicio en java

Hola amigos, hace tiempo q leo el foro y hoy me he decidido postear...
Resulta que me estoy iniciando en el mundo de java 2 y nuestro profesor nos ha puesto un ejercicio.

Vale, diréis... El profesor te tiene que ayudar etc... pero a él le gusta q nos busquemos la vida y yo ya estoy muerto de buscar. Y esperaba que algunos de ustedes me echasen una mano.

El problema : Sólo puedo hacer un una operación con un solo operador, cuando me gustaría hacerlo con cualquiera. No sé ni explicarme.

Gracias por la ayuda.


PD: JODER HE PUESTO 2 POST REPETIDO; VAYA QUE BIEN EMPIEZO EN EL FORO :S


Cita:
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.color.*;
import java.awt.GridLayout;

public class Calculadora extends Frame implements ActionListener, WindowListener {

Button boton1,boton2,boton3,boton4,boton5,boton6,boton7,b oton8,boton9,boton0,signo_mas, signo_igual,signo_menos,signo_mul,signo_div;
TextField campo_texto;
boolean acabo_de_realizar_una_operacion;
int primer_operando;




Calculadora (){

acabo_de_realizar_una_operacion = false;
primer_operando = 0;
MenuBar mb = new MenuBar();
Menu m1 = new Menu ("Menú");
m1.add(new MenuItem ("Salir"));
mb.add(m1);
setMenuBar(mb);
campo_texto = new TextField (" ");
boton1 = new Button ("1");
setLayout(new GridLayout (5,5));
boton1.addActionListener(this);
boton2 = new Button ("2");
boton2.addActionListener(this);
boton3 = new Button ("3");
boton3.addActionListener(this);
boton4 = new Button ("4");
boton4.addActionListener(this);
boton5 = new Button ("5");
boton5.addActionListener(this);
boton6 = new Button ("6");
boton6.addActionListener(this);
boton7 = new Button ("7");
boton7.addActionListener(this);
boton8 = new Button ("8");
boton8.addActionListener(this);
boton9 = new Button ("9");
boton9.addActionListener(this);
boton0 = new Button ("0");
boton0.addActionListener(this);
signo_menos = new Button ("-");
signo_menos.addActionListener(this);
signo_mul = new Button ("*");
signo_mul.addActionListener(this);
signo_div = new Button ("/");
signo_div.addActionListener(this);
signo_mas = new Button ("+");
signo_mas.addActionListener(this);
signo_igual = new Button ("=");
signo_igual.addActionListener(this);
setLayout(new FlowLayout ());
add(campo_texto);
add(boton1);
add(boton2);
add(boton3);
add(boton4);
add(boton5);
add(boton6);
add(boton7);
add(boton8);
add(boton9);
add(boton0);
add(signo_div);
add(signo_menos);
add(signo_mul);
add(signo_igual);
add(signo_mas);
addWindowListener(this);
setVisible(true);
pack();


}
private void GridLayout(int i, int j) {
// TODO Auto-generated method stub

}
//System.out.println(tecla);


@SuppressWarnings("deprecation")
public static void main(String[] args) {

Calculadora calc = new Calculadora();
calc.setLayout(new GridLayout (6,2));
calc.setSize(300,300);
calc.setTitle("Calculadora ...");
calc.setBackground(Color.BLACK);
calc.show();
}


private static int TextField() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void windowOpened (WindowEvent arg0){

}
@Override
public void windowClosing (WindowEvent arg0 ){
System.exit(0);


}
@Override
public void windowClosed (WindowEvent arg0 ){


}

@Override
public void windowActivated (WindowEvent arg0 ){

}
@Override
public void windowDeactivated (WindowEvent arg0 ){



}



@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String tecla;

tecla = (((Button)e.getSource()).getLabel());
if (tecla.equals("1") || (tecla.equals("2") || (tecla.equals("3") || (tecla.equals("4") || (tecla.equals("5") || (tecla.equals("6") || (tecla.equals("7") || (tecla.equals("8") || (tecla.equals("9") || (tecla.equals("0")))))))))))
{
if(acabo_de_realizar_una_operacion)
{
campo_texto.setText("");
acabo_de_realizar_una_operacion=false;
}
campo_texto.setText((campo_texto.getText()+tecla). trim());
}
else
if (tecla.equals("+")|| (tecla.equals("-") || (tecla.equals("*") || (tecla.equals("/")))))
{
//A partir de aquí no se que hacer para que funcione bien

primer_operando = Integer.parseInt(campo_texto.getText().trim());
campo_texto.setText(" ");
//me_han_metido_el_primer_operndo = true;
}
else
{
campo_texto.setText(primer_operando*Integer.parseI nt((campo_texto.getText().trim()))+"");
primer_operando=Integer.parseInt(campo_texto.getTe xt().trim());

}

}



@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub

}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub

}




}


Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:30.