Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/05/2013, 06:06
chisbi
 
Fecha de Ingreso: mayo-2013
Mensajes: 5
Antigüedad: 11 años
Puntos: 0
Problemas con keypressed y menu de panel con boton

Hola a todos,

estoy ya casi finalizando un juego de naves en Java y tengo un gran problema.

Tengo creado una clase Tadapter para capturarme las teclas de movimiento y disparo de la nave y funciona correctamente.

public class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent f) {

nave1.keyReleased(f);

}

public void keyPressed(KeyEvent f) {
nave1.keyPressed(f);
System.out.println("Funciona keyPressed");
}

Por otro lado tengo una metodo con cuatro Radio Button para elegir el nivel de dificultad y un boton para aceptar la eleccion.


El tema es que ejecuto el programa y me sale el menu del juego con el selector de dificultad y con el Systen.out.println veo que me captura el teclado, pero cuando marco cualquier Radio Button o el boton ya no me responde el teclado. Con lo que la nave ni se mueve ni dispara.

Antes de ponerle el menu del juego me iba perfectamente.

Seguro que es algo facil, pero estoy un poco verde.

Puedo enviaros el codigo si quereis verlo.

Muchas gracias por adelantado.



private void selectorDificultad() {

GrupoDificultad = new ButtonGroup();
jPanel1 = new JPanel();
jRadioButton1 = new JRadioButton();
jRadioButton2 = new JRadioButton();
jRadioButton3 = new JRadioButton();
jRadioButton4 = new JRadioButton();
jButton1 = new JButton();
jLabel1 = new JLabel();
jRadioButton1.setSelected(true);


jPanel1.setBackground(new java.awt.Color(0, 0, 0));

jRadioButton1.setBackground(new java.awt.Color(0, 0, 0));
GrupoDificultad.add(jRadioButton1);
jRadioButton1.setForeground(new java.awt.Color(255, 255, 255));
jRadioButton1.setText("Facil");
jRadioButton1.setName("JRButtonFacil");
jRadioButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jRadioButton1.isSelected())
jRadioButton1.setSelected(true);
nivel_dificultad = 5;
velo_alien = 4;
updateUI();
}
});


jRadioButton2.setBackground(new java.awt.Color(0, 0, 0));
GrupoDificultad.add(jRadioButton2);
jRadioButton2.setForeground(new java.awt.Color(255, 255, 255));
jRadioButton2.setText("Normal");
jRadioButton2.setName("JRButtonNormal");
jRadioButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jRadioButton2.isSelected())
jRadioButton2.setSelected(true);
nivel_dificultad = 7;
velo_alien = 6;
updateUI();
}
});


jRadioButton3.setBackground(new java.awt.Color(0, 0, 0));
GrupoDificultad.add(jRadioButton3);
jRadioButton3.setForeground(new java.awt.Color(255, 255, 255));
jRadioButton3.setText("Complicado");
jRadioButton3.setName("JRButtonComplicado");
jRadioButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jRadioButton3.isSelected())
jRadioButton3.setSelected(true);
nivel_dificultad = 10;
velo_alien = 10;
updateUI();
}
});

jRadioButton4.setBackground(new java.awt.Color(0, 0, 0));
GrupoDificultad.add(jRadioButton4);
jRadioButton4.setForeground(new java.awt.Color(255, 255, 255));
jRadioButton4.setText("Imposible");
jRadioButton4.setName("JRButtonImposible");
jRadioButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jRadioButton4.isSelected())
jRadioButton4.setSelected(true);
nivel_dificultad = 15;
velo_alien = 14;
updateUI();
}
});

jButton1.setBackground(new java.awt.Color(0, 0, 0));
jButton1.setText("Comienzo");
jButton1.setName("JButtonStart");
jButton1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

alien1Activos = getAlien1();
alien2Activos = getAlien2();
InGame = true;
nave1.visible = true;

EliminarPanel();
}
});

jLabel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Elegir nivel dificultad");
jLabel1.setName("JLabelElegirNivel");

GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout
.setHorizontalGroup(jPanel1Layout
.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addContainerGap()
.addGroup(
jPanel1Layout
.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addComponent(
jRadioButton3)
.addComponent(
jRadioButton2)
.addComponent(
jRadioButton4)
.addComponent(jLabel1)
.addComponent(
jRadioButton1))
.addContainerGap(
GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGroup(
GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(120, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(62, 62, 62)));
jPanel1Layout
.setVerticalGroup(jPanel1Layout
.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addComponent(
jLabel1,
GroupLayout.PREFERRED_SIZE,
25,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton1)
.addPreferredGap(
LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton2)
.addPreferredGap(
LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
jRadioButton3,
GroupLayout.PREFERRED_SIZE,
23,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton4)
.addPreferredGap(
LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addContainerGap(
GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)));

GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGro up(
javax.swing.GroupLayout.Alignment.LEADING).addGrou p(
layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jPanel1,
GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addContainerGap(499, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup (
GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jPanel1,
GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addContainerGap(400, Short.MAX_VALUE)));
}