I’m working on a personal project to improve my Java skills. At the moment I’m trying to figure out how to implement keyboard input, but the examples I’ve seen so far haven’t been much use. I’ve made a few attempts but nothing has worked so far. With that in mind, I created a very small program to use as a stepping stone:
package testing;
import java.awt.*;
import javax.swing.*;
public class Testing extends JFrame
{
JButton movingButton;
Testing()
{
setLayout(null);
setSize(500,500);
movingButton = new JButton("This Button Moves");
add(movingButton);
movingButton.setBounds(150,150,200,100);
}
public static void main(String[] args)
{
Testing gui = new Testing();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setTitle("Test Window");
}
}
Basically, I’m trying to figure out what I need to change so that if the GUI window is selected, pressing the Left key changes movingButton.setLocation to 100,150.