Home » Eclipse Projects » Rich Client Platform (RCP) » Java KeyListener problem(KeyListener Method in java doesn't work on my PC)
| | |
Re: Java KeyListener problem [message #722894 is a reply to message #722846] |
Wed, 07 September 2011 05:27 |
Eclipse User |
|
|
|
Originally posted by:
On 2011-09-07 00:40, codytheking93 wrote:
> This is a little bit of the code. It worked on my friends computer with
> eclipse when I copy and pasted it, but it did not work on mine.
>
> screen is a global int variable and when screen = 0 it shows the title
> screen and when screen = 1 the game screen shows, but when I press 's'
> the screen still shows the title screen.
>
> public void keyPressed(KeyEvent e){}
> public void keyReleased(KeyEvent e){}
> public void keyTyped(KeyEvent e){
> if(screen == 0 && e.getKeyChar() == 's') //changes title screen to game
> screen
> screen++;
> }
Please provide a minimum working example, otherwise we are completely in
the dust here. For example, from your example it is unclear, *where* you
registered the KeyListener. Minimum programs should be easy to write,
just look at the snippets given on
http://www.eclipse.org/swt/snippets/
Another example can be found by googling for "SWT example KeyListener":
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/KeyListenerExample.htm
Greetings from Bremen,
Daniel Krügler
|
|
|
Re: Java KeyListener problem [message #723156 is a reply to message #722894] |
Wed, 07 September 2011 21:11 |
codytheking93 Messages: 4 Registered: September 2011 |
Junior Member |
|
|
ok here is code. The mouselistener works, but the keylistener does not. When I click the screen goes red, which is what it is supposed to do. I press 'c' and nothing happens. Even if I do it before I click.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class key_work extends JApplet implements KeyListener, MouseListener{
int screen = 0;
int sw = 500, sh = 500;
public void init(){
resize(sw, sh);
addKeyListener(this);
addMouseListener(this);
}
public void paint(Graphics g){
if(screen == 0)
{
g.clearRect(0, 0, sw, sh);
g.setColor(Color.black);
g.fillRect(0, 0, sw, sh);
}
else if(screen == 1)
{
g.clearRect(0, 0, sw, sh);
g.setColor(Color.red);
g.fillRect(0, 0, sw, sh);
}
else if(screen > 2)
{
g.clearRect(0, 0, sw, sh);
g.setColor(Color.blue);
g.fillRect(0, 0, sw, sh);
}
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){
if(screen == 0)
screen = 1;
repaint();
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){
if(screen == 0 && e.getKeyChar() == 'c')
screen = 2;
repaint();
}
}
|
|
| | | |
Goto Forum:
Current Time: Thu May 09 15:08:58 GMT 2024
Powered by FUDForum. Page generated in 0.04119 seconds
|