ayuda con swing
Publicado por pablo (6 intervenciones) el 10/12/2006 05:56:54
aqui muestro un codigo, son 4 clases, el boton "resultado" no gatilla el evento:
primera clase:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.text.NumberFormat;
public class Interfaz extends JFrame
{
private JButton realizarCarrera, resultado;
private JTextField tNombre, tVelocidad, tMontoGanado;
public static void main(String[] args)
{
new Interfaz();
}
public Interfaz()
{
this.setTitle("Carreras");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
JLabel titulo = new JLabel("Resultados de Carreras");
agregarComponentes(panel, titulo, 1, 0, 1, GridBagConstraints.CENTER);
JLabel nombre = new JLabel("Nombre");
JLabel velocidad = new JLabel("Velocidad");
JLabel monto = new JLabel("Monto Ganado");
agregarComponentes(panel, nombre, 0, 1, 1, GridBagConstraints.EAST);
agregarComponentes(panel, velocidad, 0, 2, 1, GridBagConstraints.EAST);
agregarComponentes(panel, monto, 0, 3,1, GridBagConstraints.EAST);
tNombre = new JTextField(20);
tNombre.setEditable(false);
tVelocidad = new JTextField(10);
tVelocidad.setEditable(false);
tMontoGanado = new JTextField(10);
tMontoGanado.setEditable(false);
agregarComponentes(panel, tNombre, 1, 1, 2, GridBagConstraints.WEST);
agregarComponentes(panel, tVelocidad, 1, 2, 1, GridBagConstraints.WEST);
agregarComponentes(panel, tMontoGanado, 1, 3, 1, GridBagConstraints.WEST);
ManejoEventos me = new ManejoEventos();
realizarCarrera = new JButton("Realizar Carrera");
realizarCarrera.addActionListener(me);
resultado = new JButton("Resultado");
resultado.addActionListener(me);
agregarComponentes(panel, realizarCarrera, 0, 4, 1, GridBagConstraints.WEST);
agregarComponentes(panel, resultado, 2, 4, 1, GridBagConstraints.EAST);
this.add(panel);
this.setResizable(false);
this.pack();
this.setVisible(true);
}
private void agregarComponentes(JPanel p, JComponent c, int x, int y, int z, int pos)
{
GridBagConstraints gb = new GridBagConstraints ();
gb.gridx = x;
gb.gridy = y;
gb.insets = new Insets(7,7,7,7);
gb.gridwidth = z;
gb.anchor = pos;
p.add(c, gb);
}
private class ManejoEventos implements ActionListener
{
private LinkedList<String> resultado;
private double total;
public ManejoEventos()
{
resultado = new LinkedList<String>();
total = 0;
}
public void actionPerformed(ActionEvent e)
{
JButton boton = (JButton)e.getSource();
if(boton.equals(realizarCarrera))
{
Caballo c1 = new Caballo("Los Alumnitos");
String n1 = c1.getNombre();
c1.setVelocidad();
double v1 = c1.getVelocidad();
Caballo c2 = new Caballo("El Guru");
String n2 = c2.getNombre();
c2.setVelocidad();
double v2 = c2.getVelocidad();
new Thread(c1).start();
new Thread(c2).start();
NumberFormat nf1 = NumberFormat.getNumberInstance();
nf1.setMaximumFractionDigits(3);//3 decimales para velocidad
NumberFormat nf2 = NumberFormat.getNumberInstance();
nf2.setMaximumFractionDigits(0);// ningún decimal para el total
if(v1 < v2)
{
Total.setTotal(v1);
total = Total.getTotal();
resultado.add(n1);
resultado.add(nf1.format(v1));
resultado.add(nf2.format(total));
}
else
{
Total.setTotal(v2);
total = Total.getTotal();
resultado.add(n2);
resultado.add(nf1.format(v2));
resultado.add(nf2.format(total));
}
JOptionPane.showMessageDialog(Interfaz.this, "¿Quién ganó?. Presiona en el botón resultado", "Partieron !!",
JOptionPane.INFORMATION_MESSAGE);
}
if(boton.equals(resultado))// no funciona, excepto si pongo un "else"
{
int i = 0;
while(i < resultado.size())
{
tNombre.setText(resultado.get(i));
tVelocidad.setText(resultado.get(++i));
tMontoGanado.setText(resultado.get(++i));
i++;
}
}
}
}
}
Segunda Clase:
public class Caballo implements Runnable
{
private String nombre;
private double velocidad;
public Caballo()
{
nombre = new String();
velocidad = 0;
}
public Caballo(String nombre)
{
this();
setNombre(nombre);
velocidad = 0;
}
public void setNombre(String nombre)
{
if(nombre.trim().length() !=0)
{
this.nombre = nombre.trim();
}
else
{
System.out.println("Nombre no válido");
}
}
public void run()
{
setVelocidad();
int vuelta = 0;
while(vuelta <= 5)
{
try
{
Thread.sleep( (long) (velocidad));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
vuelta ++;
}
}
public void setVelocidad()
{
try
{
velocidad = Math.random()*100;
if(velocidad == 0)
{
throw new ExcepcionVelocidad();
}
}
catch(ExcepcionVelocidad v)
{
velocidad = Math.random()*100;
}
}
public double getVelocidad()
{
return velocidad;
}
public String getNombre()
{
return nombre;
}
}
tercera clase:
public class Total
{
private static double total;
public Total()
{
total = 0;
}
public static void setTotal(double cantidad)
{
total = cantidad*100;
}
public static double getTotal()
{
return total;
}
}
cuarta clase:
public class ExcepcionVelocidad extends Exception
{
public ExcepcionVelocidad()
{
}
}
Tengo la duda en la primera clase, en Interfaz. El boton "resultado" no funciona a pesar de que registro el listener. Hice un debug con eclipse, y me di cuenta que cuando en la interfaz (básica por lo demás) presiono en el boton "resultado" no entra a la sentencia "if" de ese boton, pero si pongo "else" sí entra.
¿Qué sucede?. Gracias y perdon por postear tantas clases, pero no veo sinceramente la razón. Gracias-
primera clase:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.text.NumberFormat;
public class Interfaz extends JFrame
{
private JButton realizarCarrera, resultado;
private JTextField tNombre, tVelocidad, tMontoGanado;
public static void main(String[] args)
{
new Interfaz();
}
public Interfaz()
{
this.setTitle("Carreras");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
JLabel titulo = new JLabel("Resultados de Carreras");
agregarComponentes(panel, titulo, 1, 0, 1, GridBagConstraints.CENTER);
JLabel nombre = new JLabel("Nombre");
JLabel velocidad = new JLabel("Velocidad");
JLabel monto = new JLabel("Monto Ganado");
agregarComponentes(panel, nombre, 0, 1, 1, GridBagConstraints.EAST);
agregarComponentes(panel, velocidad, 0, 2, 1, GridBagConstraints.EAST);
agregarComponentes(panel, monto, 0, 3,1, GridBagConstraints.EAST);
tNombre = new JTextField(20);
tNombre.setEditable(false);
tVelocidad = new JTextField(10);
tVelocidad.setEditable(false);
tMontoGanado = new JTextField(10);
tMontoGanado.setEditable(false);
agregarComponentes(panel, tNombre, 1, 1, 2, GridBagConstraints.WEST);
agregarComponentes(panel, tVelocidad, 1, 2, 1, GridBagConstraints.WEST);
agregarComponentes(panel, tMontoGanado, 1, 3, 1, GridBagConstraints.WEST);
ManejoEventos me = new ManejoEventos();
realizarCarrera = new JButton("Realizar Carrera");
realizarCarrera.addActionListener(me);
resultado = new JButton("Resultado");
resultado.addActionListener(me);
agregarComponentes(panel, realizarCarrera, 0, 4, 1, GridBagConstraints.WEST);
agregarComponentes(panel, resultado, 2, 4, 1, GridBagConstraints.EAST);
this.add(panel);
this.setResizable(false);
this.pack();
this.setVisible(true);
}
private void agregarComponentes(JPanel p, JComponent c, int x, int y, int z, int pos)
{
GridBagConstraints gb = new GridBagConstraints ();
gb.gridx = x;
gb.gridy = y;
gb.insets = new Insets(7,7,7,7);
gb.gridwidth = z;
gb.anchor = pos;
p.add(c, gb);
}
private class ManejoEventos implements ActionListener
{
private LinkedList<String> resultado;
private double total;
public ManejoEventos()
{
resultado = new LinkedList<String>();
total = 0;
}
public void actionPerformed(ActionEvent e)
{
JButton boton = (JButton)e.getSource();
if(boton.equals(realizarCarrera))
{
Caballo c1 = new Caballo("Los Alumnitos");
String n1 = c1.getNombre();
c1.setVelocidad();
double v1 = c1.getVelocidad();
Caballo c2 = new Caballo("El Guru");
String n2 = c2.getNombre();
c2.setVelocidad();
double v2 = c2.getVelocidad();
new Thread(c1).start();
new Thread(c2).start();
NumberFormat nf1 = NumberFormat.getNumberInstance();
nf1.setMaximumFractionDigits(3);//3 decimales para velocidad
NumberFormat nf2 = NumberFormat.getNumberInstance();
nf2.setMaximumFractionDigits(0);// ningún decimal para el total
if(v1 < v2)
{
Total.setTotal(v1);
total = Total.getTotal();
resultado.add(n1);
resultado.add(nf1.format(v1));
resultado.add(nf2.format(total));
}
else
{
Total.setTotal(v2);
total = Total.getTotal();
resultado.add(n2);
resultado.add(nf1.format(v2));
resultado.add(nf2.format(total));
}
JOptionPane.showMessageDialog(Interfaz.this, "¿Quién ganó?. Presiona en el botón resultado", "Partieron !!",
JOptionPane.INFORMATION_MESSAGE);
}
if(boton.equals(resultado))// no funciona, excepto si pongo un "else"
{
int i = 0;
while(i < resultado.size())
{
tNombre.setText(resultado.get(i));
tVelocidad.setText(resultado.get(++i));
tMontoGanado.setText(resultado.get(++i));
i++;
}
}
}
}
}
Segunda Clase:
public class Caballo implements Runnable
{
private String nombre;
private double velocidad;
public Caballo()
{
nombre = new String();
velocidad = 0;
}
public Caballo(String nombre)
{
this();
setNombre(nombre);
velocidad = 0;
}
public void setNombre(String nombre)
{
if(nombre.trim().length() !=0)
{
this.nombre = nombre.trim();
}
else
{
System.out.println("Nombre no válido");
}
}
public void run()
{
setVelocidad();
int vuelta = 0;
while(vuelta <= 5)
{
try
{
Thread.sleep( (long) (velocidad));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
vuelta ++;
}
}
public void setVelocidad()
{
try
{
velocidad = Math.random()*100;
if(velocidad == 0)
{
throw new ExcepcionVelocidad();
}
}
catch(ExcepcionVelocidad v)
{
velocidad = Math.random()*100;
}
}
public double getVelocidad()
{
return velocidad;
}
public String getNombre()
{
return nombre;
}
}
tercera clase:
public class Total
{
private static double total;
public Total()
{
total = 0;
}
public static void setTotal(double cantidad)
{
total = cantidad*100;
}
public static double getTotal()
{
return total;
}
}
cuarta clase:
public class ExcepcionVelocidad extends Exception
{
public ExcepcionVelocidad()
{
}
}
Tengo la duda en la primera clase, en Interfaz. El boton "resultado" no funciona a pesar de que registro el listener. Hice un debug con eclipse, y me di cuenta que cuando en la interfaz (básica por lo demás) presiono en el boton "resultado" no entra a la sentencia "if" de ese boton, pero si pongo "else" sí entra.
¿Qué sucede?. Gracias y perdon por postear tantas clases, pero no veo sinceramente la razón. Gracias-
Valora esta pregunta


0