So I am writing a simple fighting game in Java that includes both a title screen JPanel, as well as a Playspace JPanel. I have been experiencing some issues with getting the Playspace's KeyListener to work after adding the title screen. I have tried numerous other suggestions from other SO posts, and have seen none work so far. Below is my Playspace constructor, with the int mode being single or multiplayer, and app being the JFrame. It should be noted that both Title and Play screens are in the same frame.
/** Constructor, sets frame and arrays up **/
public Playspace(int mode, Application app) {
// Sets up JPanel
super();
// Application variables
this.app = app;
HEIGHT = app.getHeight();
WIDTH = app.getWidth();
setLayout(null);
setLocation(0, 0);
setSize(WIDTH, HEIGHT);
backgroundColor = new Color(50, 50, 60);
backgroundColor.brighter();
setBackground(backgroundColor);
setVisible(true);
setFocusable(true);
requestFocusInWindow();
addKeyListener(new KeyListener() {
/** Keypress detection **/
@Override
public void keyPressed(KeyEvent e) {
int i = 0;
if (runnable[i]) {
// WASD Controls
if (e.getKeyCode() == KeyEvent.VK_A) {
players[i].setLeftPressed(true);
players[i].setDirection(-1);
}
if (e.getKeyCode() == KeyEvent.VK_D) {
players[i].setRightPressed(true);
players[i].setDirection(1);
}
if (e.getKeyCode() == KeyEvent.VK_S) {
players[i].setDownPressed(true);
}
if (e.getKeyCode() == KeyEvent.VK_W) {
players[i].setUpPressed(true);
players[i].setUpReleased(false);
}
if (e.getKeyCode() == KeyEvent.VK_SHIFT && e.getKeyLocation() == KeyEvent.KEY_LOCATION_LEFT
&& players[i].isShiftReleased()) {
players[i].setShiftPressed(true);
players[i].setShiftReleased(false);
}
}
i = 1;
if (runnable[i]) {
// ULDR Controls
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
players[i].setLeftPressed(true);
players[i].setDirection(-1);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
players[i].setRightPressed(true);
players[i].setDirection(1);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
players[i].setDownPressed(true);
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
players[i].setUpPressed(true);
players[i].setUpReleased(false);
}
if (e.getKeyCode() == KeyEvent.VK_SHIFT && e.getKeyLocation() == KeyEvent.KEY_LOCATION_RIGHT
&& players[i].isShiftReleased()) {
players[i].setShiftPressed(true);
players[i].setShiftReleased(false);
}
}
}
/** Key Release detection **/
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_F11) {
developerMode = !developerMode;
}
int i = 0;
// WASD Controls
if (e.getKeyCode() == KeyEvent.VK_A) {
players[i].setLeftPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_S) {
players[i].setDownPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_D) {
players[i].setRightPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_W) {
players[i].setUpPressed(false);
players[i].setUpReleased(true);
}
if (e.getKeyCode() == KeyEvent.VK_SHIFT && e.getKeyLocation() == KeyEvent.KEY_LOCATION_LEFT) {
players[i].setShiftPressed(false);
players[i].setShiftReleased(true);
}
i = 1;
// ULDR Controls
if (e.getKeyCode() == KeyEvent.VK_UP) {
players[i].setUpPressed(false);
players[i].setUpReleased(true);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
players[i].setDownPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
players[i].setLeftPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
players[i].setRightPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_SHIFT && e.getKeyLocation() == KeyEvent.KEY_LOCATION_RIGHT) {
players[i].setShiftPressed(false);
players[i].setShiftReleased(true);
}
}
/** We need this, but would rather forget it... **/
@Override
public void keyTyped(KeyEvent e) {
}
});
setVisible(true);
**It continues from here, but this is the setup in its entirety
Thanks to all that can provide help
One more note, I have tried adding some print statements to the KeyListener, but to no avail, the listener is never called.