Question:
java won't listen for key and action events and i have no clue why...?
Evelyn R
2010-12-21 10:30:02 UTC
when i run the following program, it listens to the buttons
but clicking the keys does nothing? why?


import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.*;



public class KeyBoardTest extends JApplet implements KeyListener, ActionListener
{
private String event = " "; // description of keyboard event

private int xcoordinate = 400; // starting xcoordinate of the message
private int ycoordinate = 300; // starting ycoordinate of the event
private int increment = 20; // int to increment location upon keyboard events

private JButton topButton; // button to reset location to the top
private JButton centerButton; // button to reset location to the center

@Override
public void init() // set up GUI
{
setLayout(new FlowLayout()); // set the layout of the applet
setSize(800, 600); // set the size of the applet
addKeyListener(this); // listen for keyboard events

centerButton = new JButton("Reset to Center :"); // set up Center button
centerButton.addActionListener(this);
add(centerButton);

topButton = new JButton("Reset to Top Left :"); // set up Top Left button
topButton.addActionListener(this);
add(topButton);

event = "You are at (" + xcoordinate + ", " + ycoordinate + ") ";
}

@Override
public void paint(Graphics g)
{
super.paint(g);

g.drawString(event, xcoordinate, ycoordinate); // draw messgae to the applet
}

public void keyPressed(KeyEvent e) // handle key presses
{
}

public void keyReleased(KeyEvent e) // handle key releases
{
}

public void keyTyped(KeyEvent e) // handle typing on applet
{

if (e.getKeyChar() == 'h') // when 'h' key is pressed
{
ycoordinate = ycoordinate + increment;
}
if (e.getKeyChar() == 'j') // when 'j' key is pressed
{
ycoordinate = ycoordinate - increment;
}
if (e.getKeyChar() == 'k') // when 'k' key is pressed
{
xcoordinate = xcoordinate - increment;
}
if (e.getKeyChar() == 'l') // when 'l' key is pressed
{
xcoordinate = xcoordinate + increment;
}

event = "You are at (" + xcoordinate + ", " + ycoordinate + ") ";

repaint();
}

public void actionPerformed(ActionEvent e) // handle button clicks
{
if(e.getSource() == topButton) // process clicking "Center" button
{
xcoordinate = 10; // reset the xcoordinate
ycoordinate = 50; // reset the ycoordinate
}
if(e.getSource() == centerButton) // process clicking "Clear" button
{
xcoordinate = 400; // reset the xcoordinate
ycoordinate = 300; // reset the ycoordinate
}

event = "You are at (" + xcoordinate + ", " + ycoordinate + ") ";

requestFocus();
repaint();


}
}
Five answers:
2010-12-22 02:31:47 UTC
The movement keys (j,h,k,l) work here as long as:

a) The applet has focus

b) The buttons do not have focus.



Solutions:

a) Make sure the applet has focus.

b) Either add the KeyListener to the buttons as well, or add KeyBindings to the lot.



As an aside. I usually do not post to Yahoo forums. For best answers on Java & applets, I would recommend either the Oracle forums or StackOverflow.
jigar
2016-12-02 17:06:18 UTC
Java comes with JEditPane. you would be able to desire to create an occasion and additionally set the "style" to .rtf The Manning Publishers e book "Swing" has an entire editor occasion (version a million is in observe checklist on line). under hyperlink is an occasion code of establishing for view an .rtf report. Asfaras strikes and activities... factors, such because of the fact the Mouse and Buttons have pre-defined habit. You upload an ActionListener and then the ActionListener seems for the activities this is able to "listening to". you are able to write ActionEvents as their own techniques or connect those upon the ingredient's advent -- that's my decision in distinctive situations different than the Mouse. occasion... JButton button = new JButton("Erase annoying tension C:domicile windows"); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { device.out.println("dummy erase"); } }); It takes some learn with strikes.
Cleora
2016-02-21 21:00:59 UTC
La desanimación es doliente por esto no abandone puesto que la salida para quedarte embarazada existe, es aquí https://tr.im/kh0T3 Tengo una amiga que se quedó embarazada con la ayuda de este procedimiento, ulteriormente a un montón de tentativas y fiascos, encontré esta página y intenté el método, en final nada podía perder en cambio ha ganado todo porque se quedó embarazada en menos de 2 semanas.
Gema
2016-02-18 22:57:09 UTC
Si quieres vivir a lo grande entonces tienes que entrar en esta página https://tr.im/jNItX . Yo he embolsado un montón de dinero con estas encuestas porque, tras quedarme embarazada he sido despedida y de esta forma fue como busqué algo que hacer desde casa. Aunque todos opinaban que no voy a poder ganar tan fácil yo sí que he logrado y solo dando mi opinión. Ulteriormente al enseñarlos mis cheques se han apuntado y ellos en este lugar y en presente podemos hacer vacaciones largas en sitios donde ni no habíamos soñado.
Mr. Shar Pei
2010-12-21 19:36:42 UTC
www.clearsidetechnologies.com


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...