• Sábado 21 de Septiembre de 2024, 16:32

Autor Tema:  ActionListener en ciclo y cargar imágenes en ventana  (Leído 2354 veces)

Hidetoxin

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
ActionListener en ciclo y cargar imágenes en ventana
« en: Lunes 18 de Abril de 2011, 23:28 »
0
Hola soy nuevo en estos foros y también en el lenguaje Java   :hola:  . Espero me puedan hacer el favor de ayudarme a aclarar mis dudas, mi consulta es la siguiente: tengo que programar  :comp: un juego del ahorcado en el cual el usuario tiene 6 intentos para adivinar la palabra y cada vez que no acierte tiene que aparecer en la ventana la imagen del ahorcado, sin embargo no sé como cargar imágenes en la ventana  :(  .  En uno de los temas de este foro vi que se podía hacer de la siguiente manera, pero por alguna razón no me parece funcionar, ¿ Me he equivocado en algo o se hace de alguna otra manera ?

Código para cargar imágenes:

Código: Text
  1.  
  2. private JLabel etiquetas[] = new JLabel[6];
  3.    
  4. private ImageIcon imagenes[] = new ImageIcon[6];
  5.  
  6. getContentPane( ).setLayout( null );
  7.        
  8. imagenes[0] = new ImageIcon( "BlackCat.jpg" );
  9. etiquetas[0] = new JLabel( imagenes[0] );
  10. etiquetas[0].setBounds( 20 , 30 , 320 , 480 );
  11. getContentPane( ).add( etiquetas[0] );
  12.  
  13.  

Y otra duda que tengo es que tengo un teclado para ingresar las letras. El teclado es un arreglo de JButtons y los inicializo en un ciclo, ahora mi pregunta ¿ hay manera de implementar un Actionlistener por cada botón en un ciclo ? lo intente de la siguiente forma pero no funciona.

Código: Text
  1.  
  2. String teclado = qwerty + asdfg + zxcv;
  3.        
  4.         for( int i = 0 ; i < teclado.length( ) ; i++ )
  5.         {
  6.             botones[i].addActionListener( new ActionListener( ) {
  7.                 public void actionPerformed( ActionEvent evt ){
  8.                     letra = botones[i].getText(  );
  9.                     adivinarAction( evt );
  10.                     }  }  );
  11.         }
  12.  
  13.  

También les dejo mi código completo como referencia.

Código Completo:

Código: Text
  1.  
  2. import java.util.*;
  3. import java.awt.*;                                                                                                            
  4. import javax.swing.*;    
  5. import java.awt.event.*;  
  6.  
  7. public class Ahorcado extends JFrame{
  8.    
  9.     private JTextField palabra;
  10.    
  11.     private JButton botones[] = new JButton[27] , nuevo;
  12.    
  13.     private String qwerty = "qwertyuiop" , asdfg = "asdfghjklñ" , zxcv = "zxcvbnm" , auxiliar1 , auxiliar2 , letra;
  14.    
  15.     private String animales[] = { "jirafa" , "ballena" , "rinoceronte" , "gorila" , "aguila" , "murcielago" , "avestruz" , "reno" , "mapache" , "capibara" , "ormitorrinco" };
  16.  
  17.     private JLabel etiquetas[] = new JLabel[6];
  18.    
  19.     private ImageIcon imagenes[] = new ImageIcon[6];
  20.    
  21.     int j;
  22.  
  23.     Ahorcado( )
  24.     {
  25.        
  26.         setTitle( "Juego del ahorcado" );
  27.         setSize( 930 , 700 );
  28.         setResizable( false );
  29.         iniciar( );
  30.         accionar( );
  31.         getRootPane( ).setDefaultButton( nuevo );
  32.        
  33.     }
  34.    
  35.     private void iniciar( )
  36.     {
  37.        
  38.         getContentPane( ).setLayout( null );
  39.        
  40.         imagenes[0] = new ImageIcon( "BlackCat.jpg" );
  41.         etiquetas[0] = new JLabel( imagenes[0] );
  42.         etiquetas[0].setBounds( 20 , 30 , 320 , 480 );
  43.         getContentPane( ).add( etiquetas[0] );
  44.        
  45.         palabra = new JTextField( "" );
  46.         palabra.setFont( new Font( "Arial" , 1 , 50 ) );
  47.         palabra.setHorizontalAlignment( SwingConstants.CENTER );
  48.         palabra.setToolTipText( "Adivina la palabra" );
  49.         palabra.setBounds( 20 , 300 , 880 , 70 );
  50.         palabra.setText( "" );
  51.         palabra.setEditable( false );
  52.         getContentPane( ).add( palabra );
  53.        
  54.         nuevo = new JButton( "" );
  55.         nuevo.setFont( new Font( "Arial" , 1 , 25 ) );
  56.         nuevo.setToolTipText( "Presiona aquí para adivinar otra palabra" );
  57.         nuevo.setText( "Nuevo juego" );
  58.         nuevo.setBounds( 650 , 570 , 250 , 70 );
  59.         getContentPane( ).add( nuevo );
  60.  
  61.         int x = 20 , y = 400;
  62.        
  63.         String teclado = qwerty + asdfg + zxcv;
  64.        
  65.         for( int i = 0 ; i < 27 ; i++ )
  66.         {
  67.             botones[i] = new JButton( "" );
  68.             botones[i].setFont( new Font( "Arial" , 1 , 25 ) );
  69.             botones[i].setToolTipText( "Introduce la letra " + Character.toString( teclado.charAt( i ) ) );
  70.             botones[i].setBounds( x , y , 70 , 70);
  71.             x += 90;
  72.             if( i == qwerty.length( ) - 1 )
  73.             {
  74.                 y += 85;
  75.                 x = 20;
  76.             }
  77.             else if( i == qwerty.length( ) + asdfg.length( ) - 1 )
  78.             {
  79.                 y += 85;
  80.                 x = 20;
  81.             }
  82.             getContentPane( ).add( botones[i] );
  83.         }
  84.        
  85.         for( int i = 0; i < teclado.length( ) ; i++ )
  86.              botones[i].setText( Character.toString( teclado.charAt( i ) ) );
  87.        
  88.         }
  89.    
  90.     private void accionar( )
  91.     {
  92.        
  93.         String teclado = qwerty + asdfg + zxcv;
  94.        
  95.         for( int i = 0 ; i < teclado.length( ) ; i++ )
  96.         {
  97.             botones[i].addActionListener( new ActionListener( ) {
  98.                 public void actionPerformed( ActionEvent evt ){
  99.                     /*letra = botones[j++].getText( );*/
  100.                     adivinarAction( evt );
  101.                     } } );
  102.         }
  103.        
  104.         nuevo.addActionListener( new ActionListener( ) {
  105.             public void actionPerformed( ActionEvent evt ){
  106.                 jugarAction( evt ); } } );
  107.        
  108.     }
  109.    
  110.     private void adivinarAction( ActionEvent e )
  111.     {
  112.        
  113.         palabra.setText( letra );
  114.        
  115.     }
  116.    
  117.     private void jugarAction( ActionEvent e )
  118.     {
  119.        
  120.         Random rndm = new Random( );
  121.        
  122.         auxiliar1 = animales[ rndm.nextInt( 11 ) ];
  123.         auxiliar2 = "";
  124.        
  125.         for( int i = 0 ; i < auxiliar1.length( ) ; i++ )
  126.              auxiliar2 += "-";
  127.        
  128.         palabra.setText( auxiliar2 );
  129.        
  130.     }
  131.    
  132.     public static void main( String args[] ){
  133.        
  134.         try
  135.         {
  136.             UIManager.setLookAndFeel(
  137.             UIManager.getSystemLookAndFeelClassName( ) );}
  138.             catch( Exception e )
  139.             {
  140.                 System.out.print( "No se puede continuar" + e );
  141.             }
  142.                      
  143.             Ahorcado objeto = new Ahorcado( );
  144.             objeto.setVisible( true );
  145.                      
  146.         }
  147.    
  148. }
  149.  
  150.  

¡ Saludos  !  :hola: