Help with KeyListener? (2)

1 Name: #!/usr/bin/anonymous : 2010-03-05 15:19 ID:zdVcpw5h

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.text.AbstractDocument.Content;

public class Window extends JFrame implements KeyListener{

public static void createAndShowGUI() {

JFrame frame = new JFrame("Tetris");
frame.getContentPane().add(new Rules());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 800);
frame.setFocusable(true);
frame.setVisible(true);
frame.setResizable(false);

Container content = frame.getContentPane();
content.setFocusable(true);
content.setBackground(Color.black);

}

public void keyListener(){

}

public static void main(String[] args){

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
}
);
}
public void keyPressed(KeyEvent e) {

int keyCode = e.getKeyCode();
System.out.println(keyCode);

if (e.getKeyCode() == 37){
Rules.moveLeft();
}


}

public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {

}

}

So how am I supposed to add KeyListener to this code? I've tried to add it about everything, but I always an error such as "can't use this in a static content". I'm trying to get the console to print out the keycode when I type in a key.

halp?

2 Name: #!/usr/bin/anonymous : 2010-03-06 18:22 ID:aAC82o5M

Ideally you should put the GUI in a separate class from the driver with the main method. In this simple example you can easily fix your problem by putting everything you have in createAndShowGUI to be a constructor for the GUI, like this:
http://pastebin.com/5iDatWaJ

This thread has been closed. You cannot post in this thread any longer.