How to manipulate key events?
Hello guys,
I made a method that make a JFrame with some JLabels.
Now I want to make a method that execute some instructions when the user press the left or right key button.
I guess I need to add an actionListener in the JFrame and make an actionPerformed method. But how to identify when the left or right key are pressed?
Does someone have any tips?
> Look at KeyListener and KeyAdapter in the Java
> tutorial. You might have to resort to key binding,
> but this is unlikely.
I'm going to politely disagree. KeyListeners and especially KeyAdapters are lower level constructs, and in general you want to stick with higher level constructs if possible. To quote the Java key binding tutorial:
An alternative to key bindings is using key listeners. Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code. Key listeners are also difficult if the key binding is to be active when the component doesn't have focus. Some of the advantages of key bindings are they're somewhat self documenting, take the containment hierarchy into account, encourage reusable chunks of code (Action objects), and allow actions to be easily removed, customized, or shared. Also, they make it easy to change the key to which an action is bound. Another advantage of Actions is that they have an enabled state which provides an easy way to disable the action without having to track which component it is attached to.
> Your JFrame has some labels, but does it also have
> JButtons? I'm assuming so (left and right ?key?
> button?). If so, do you know how to add an
> actionlistener for a button? That's what you need to
> look up and study. Also, if you have two buttons
> sharing the same actionlistener, then you will need
> to get look at the ActionEvent object returned to
> find out which button was pressed. Look at the
> getSource() method of ActionEvent for further
> details.
My JFrame doesn't have JButtons. It only has three JLabels with text ("START", "OPTIONS", "QUIT").
The first Label has yellow foreground and the others has white foreground(the frame has black background).
I want to change the foregronds when the user press the right or left key (like options screen in a game, using the keyboard to select the option).
I'm reading about key bindings but I'm having problems.
Do I need to import some file to the getInputMap method work?
My code, until now:
import javax.swing.*;
import java.awt.*;
class Screens {
private JFrame window = new JFrame( "Bruno's Game" );
private Container c = window.getContentPane();
// Set Screen Settings
public void setScreenSettings () {
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setSize( 320, 240 );
c.setBackground( new Color( 0,0,0 ) );
window.setVisible( true );
}
// Options Screen
public void optionsScreen () {
String optionsString[] = { "START", "OPTIONS", "QUIT" };
JLabel options[] = new JLabel[ optionsString.length ];
for (int i = 0; i < options.length; i++) {
options[i] = new JLabel();
options[i].setForeground( new Color( 255,255,255 ) );
options[i].setSize( 300, 30 + (50 * i) );
options[i].setLocation( 10,10 );
options[i].setText( optionsString[i] );
c.add( options[i] );
}
options[0].setForeground( new Color( 255, 255, 0 ) );
InputMap inputMap = window.getInputMap();
}
}
But the javac returns the following error:
Cannot find symbol method getInputMap();
Thanks for replies.
Message was edited by:
Bruno_Grasselli