When the code runs I get no errors. It runs all parts except for my keypress. Where something is going wrong.
import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;
// Inheriting the JFrame class
public class Main extends JFrame {
Scanner d = new Scanner(System.in);
//Defining the Frame
JFrame f;
char input;
int x = 250;
int y = 100;
//Constructor
Main()
{
ImageIcon p = new ImageIcon("Player.png");
JLabel b = new JLabel(p);
b.setBounds(x, y, 50, 50);
add(b);
System.out.println("started");
b.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
System.out.println("*Event Listener Run*");
input = e.getKeyChar();
if (input == 'w')
{
y-=50;
}
else if (input == 'a')
{
x-=50;
}
else if (input == 's')
{
y+=50;
}
else if (input == 'd')
{
x+=50;
}
System.out.println("Cords: "+x+","+y);
b.setBounds(x, y, 50, 50);
}
public void keyTyped(KeyEvent e){
}
public void keyReleased(KeyEvent e){
System.out.println("keyReleased");
}
});
//Set up Window
System.out.println("*setup window*");
setSize(800, 600);
setLayout(null);
setVisible(true);
System.out.println("*setup window done*");
}
public static void main(String[] args){
// Create the window
new Main();
}
}
I added error println
's to see if some of the code isn't running, however it all is.