Ver Mensaje Individual
  #4 (permalink)  
Antiguo 25/07/2008, 00:14
Avatar de HackmanC
HackmanC
 
Fecha de Ingreso: enero-2008
Ubicación: Guatemala
Mensajes: 1.817
Antigüedad: 16 años, 3 meses
Puntos: 260
Sonrisa Respuesta: Error en calculadora java

Hola,
Bien gracias !

Posiblemente lo puedas resolver de la siguiente forma:

Código:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;

public class CALC extends JFrame implements ActionListener {
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bmas,bmenos,bpor,bdiv,bc;
JTextField tf;
public static void main (String argv[] ) {
CALC app=new CALC();
app.setSize(300,500);
app.setVisible(true);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
}
//CONSTRUCTOR
public CALC(){
setLayout(new GridLayout(5,3,5,5));
JPanel p1,p2;
Icon uno,dos,tres,cuatro,cinco,seis,siete,ocho,nueve,cero,suma,resta,multi,division,igual,C;//declaro los icon
setLayout(new BorderLayout());
p1=new JPanel(new GridLayout(1,1,10,10));//panel donde se ubicara el area en blanco
p2=new JPanel(new GridLayout(5,1,5,5)); //panel donde se ubicaran los botones
uno=new ImageIcon("uno.jpg");//creo el icon
dos=new ImageIcon("dos.jpg");//creo el icon
tres=new ImageIcon("tres.jpg");//creo el icon
cuatro=new ImageIcon("cuatro.jpg");//creo el icon
cinco=new ImageIcon("cinco.jpg");//creo el icon
seis=new ImageIcon("seis.jpg");//creo el icon
siete=new ImageIcon("siete.jpg");//creo el icon
ocho=new ImageIcon("ocho.jpg");//creo el icon
nueve=new ImageIcon("nueve.jpg");//creo el icon
cero=new ImageIcon("cero.jpg"); //creo el icon
suma=new ImageIcon("suma.jpg");//creo el icon
resta=new ImageIcon("resta.jpg");//creo el icon
multi=new ImageIcon("multi.jpg");//creo el icon
division=new ImageIcon("division.jpg");//creo el icon
igual=new ImageIcon("igual.jpg");//creo el icon
C=new ImageIcon("c.jpg");//creo el icon
b1 = new JButton(uno);//el boton lo relaciono con el icon
b2 = new JButton(dos);//el boton lo relaciono con el icon
b3 = new JButton(tres);//el boton lo relaciono con el icon
b4 = new JButton(cuatro);//el boton lo relaciono con el icon
b5 = new JButton(cinco);//el boton lo relaciono con el icon
b6 = new JButton(seis);//el boton lo relaciono con el icon
b7 = new JButton(siete);//el boton lo relaciono con el icon
b8 = new JButton(ocho);//el boton lo relaciono con el icon
b9 = new JButton(nueve);//el boton lo relaciono con el icon
b0 = new JButton(cero);//el boton lo relaciono con el icon
b1.setActionCommand("1");
b2.setActionCommand("2");
b3.setActionCommand("3");
b4.setActionCommand("4");
b5.setActionCommand("5");
b6.setActionCommand("6");
b7.setActionCommand("7");
b8.setActionCommand("8");
b9.setActionCommand("9");
b0.setActionCommand("0");
bmas = new JButton(suma);//el boton lo relaciono con el icon
bmenos = new JButton(resta);//el boton lo relaciono con el icon
bpor = new JButton(multi);//el boton lo relaciono con el icon
bdiv = new JButton(division);//el boton lo relaciono con el icon
bc = new JButton(C);
tf= new JTextField ();
p1.add(tf);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bmas);
p2.add(b0);
p2.add(bmenos);
p2.add(bpor);
p2.add(bdiv);
p2.add(bc);
add(p1,BorderLayout.NORTH);//ubico el panel en la parte norte del borderlayout
add(p2,BorderLayout.CENTER);//ubico el panel en la parte centro del borderlayout
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
bmenos.addActionListener(this);
bmas.addActionListener(this);
bpor.addActionListener(this);
bdiv.addActionListener(this);
bc.addActionListener(this);
}

public void actionPerformed(ActionEvent ae){
int res,a,b,total;
char op;

tf.setText(ae.getActionCommand());
//if (ae.getSource()==b1){
//res= 1;
//aux=Integer.parseInt(t1.getText());
//}

}

}
El JButton tiene asociado un Action por defecto, uno de los constructores de JButton acepta un Action como parámetro, pero se puede utilizar solo el ActionCommand, puedes expandir la funcionalidad implementando un Action.

Saludos,

Última edición por HackmanC; 25/07/2008 a las 00:27 Razón: cleanup