Tutorial de Java

Teclado

Anterior | Siguiente

Algunas de las clases de Java proporcionan métodos de conveniencia para permitir realizar acciones a través del teclado. Por ejemplo, la clase AbstractButton dispone del método setMnemonic(), que permite especificar el carácter, que en combinación con una de las teclas de modificación (dependiendo del Look-and-Feel que se esté utilizando), hace que se ejecuten las acciones asociadas a los botones. Esta característica es muy útil en los menús, tal como ya se empleó en el ejemplo java1411.java.

El ejemplo siguiente java1423.java, es muy sencillo, pero muestra la forma en que se utilizan estos métodos. En la ventana aparecerán nueve botones, tal como muestra la figura siguiente.

Si se pulsan las teclas correspondientes a los cursores, la X que aparece sobre los botones se desplazará en la dirección correspondiente al botón que se haya pulsado.

import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.border.*;

public class java1423 extends JFrame implements ActionListener {
  protected JButton botones[] = new JButton[9];
  
  public java1423() {
    super( "Tutorial de Java, Swing" );
    Container pane = getContentPane();
    pane.setLayout( new GridLayout( 3,3 ) );
    
    Border borde = BorderFactory.createLineBorder( Color.black );
    KeyStroke arriba = KeyStroke.getKeyStroke( KeyEvent.VK_UP,0 );
    KeyStroke abajo = KeyStroke.getKeyStroke( KeyEvent.VK_DOWN,0 );
    KeyStroke izqda = KeyStroke.getKeyStroke( KeyEvent.VK_LEFT,0 );
    KeyStroke drcha = KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT,0 );
    
    JRootPane rootPane = getRootPane();
    rootPane.registerKeyboardAction( this,"arriba",arriba,
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
    rootPane.registerKeyboardAction( this,"abajo",abajo,
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
    rootPane.registerKeyboardAction( this,"drcha",drcha,
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
    rootPane.registerKeyboardAction( this,"izqda",izqda,
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
    
    for( int i=0; i < 9; i++ ) {
      JButton boton;
      boton = new JButton();
      boton.setBorder( borde );
      boton.setName( new Integer(i).toString() );
      pane.add( boton );
      
      botones[i] = boton;
    }
    setSize( 200,200 );
  }
    
  public void actionPerformed( ActionEvent evt ) {
    Component foco = getFocusOwner();
    String nombre = foco.getName();
    int indice = Integer.parseInt( nombre );
    botones[indice].setText( "" );
    String accion = evt.getActionCommand();
    
    if( accion.equals( "arriba" ) ) {
      indice = (indice < 3) ? indice + 6 : indice - 3;
    } else if( accion.equals( "abajo" ) ) {
      indice = (indice > 5) ? indice - 6 : indice + 3;
    } else if( accion.equals( "izqda" ) ) {
      indice = (indice == 0) ? indice = 8 : indice - 1;
    } else { // asume drcha
      indice = (indice == 8) ? indice = 0 : indice + 1;
    }
    
    botones[indice].setText( "X" );
    botones[indice].requestFocus();
  }
 
  static public void main( String argv[] ) {
    new java1423().show();
  }
}

Navegador

Home | Anterior | Siguiente | Indice | Correo