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?
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