Login: password:
Forgot your password?
Programmazione 2 - a.a. 2012/13
Intercettazione tasti > Esempio di keylistener > in depth
Esempio di keylistener

class MyPanel extends JPanel implements KeyListener {
private char c = 'e';

public MyPanel() {
this.setPreferredSize(new Dimension(500, 500));
addKeyListener(this);
}

public void addNotify() {
super.addNotify();
requestFocus();
}

public void paintComponent(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
g.drawString("the key that pressed is " + c, 250, 250);
}

public void keyPressed(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) {
c = e.getKeyChar();
repaint();
}

public static void main(String[] s) {
JFrame f = new JFrame();
f.getContentPane().add(new MyPanel());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}

tratto da stackoverflow

Intercettazione tasti > in depth


powered by segue
segue_logo