Java KeyListener problem [message #721788] |
Fri, 02 September 2011 17:32  |
Eclipse User |
|
|
|
Hello,
I've coded on macs and everything works, but when I use the code on my PC my KeyListener doesn't work. I know the code is correct, I think its something with my computer maybe, I'm not sure. Please help.
Thanks,
Cody
|
|
|
|
|
|
Re: Java KeyListener problem [message #723156 is a reply to message #722894] |
Wed, 07 September 2011 17:11   |
Eclipse User |
|
|
|
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();
}
}
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.06155 seconds