a me questo funziona (l'ho provato sia con appletviewer che da browser):
codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Andrea
*/
public class AppletKeyboard extends JApplet {
private JLabel label;
private class MyKeyListener extends KeyAdapter {
public void keyPressed (KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_R :
label.setBackground(Color.red);
break;
case KeyEvent.VK_Y :
label.setBackground(Color.yellow);
break;
case KeyEvent.VK_G :
label.setBackground(Color.green);
break;
}
}
}
public void init() {
label = new JLabel("Change My Color R,Y,G");
label.setOpaque(true);
this.getContentPane().add(label);
this.addKeyListener(new MyKeyListener());
this.setFocusable(true);
}
}