我正在创建一个程序,其中(现在)有一个由箭头键控制的跑卫,以及一个阻止他的线卫(将能够自动向跑步方向移动)。我能够让跑步移动,但它现在不起作用了。这是我到目前为止的代码:
JFrame类:
public class FootballFrame extends JFrame {
Panels panels;
public FootballFrame() throws IOException{
setTitle("Tackle Breaking Running Back");
panels = new Panels();
add(panels);
setSize(1166, 795);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
小组类:
public class Panels extends JPanel{
private FootballField field;
private ScoreboardPanel score;
public Panels() throws IOException{
setLayout(new BorderLayout());
score = new ScoreboardPanel();
score.setBorder(new EmptyBorder(10, 10, 10, 10));
add(score, "North");
field = new FootballField();
field.setBorder(new EmptyBorder(10, 10, 10, 10));
add(field, "Center");
}
}
FootballField Class:
public class FootballField extends JPanel implements KeyListener{
private Image footballField;
private ImageIcon runningBackImage, linebackerImage;
private JButton runningBack, linebacker;
public FootballField() throws IOException{
super();
//Set layout to null to be able to freely position objects
setLayout(null);
//Creates the football field image
footballField = Toolkit.getDefaultToolkit().getImage("footballfield.jpeg");
//Creates the running back iage
runningBackImage = new ImageIcon("football_player.png");
//Sets the button to be the runningBack image
runningBack = new JButton(runningBackImage);
//Allows us to place the running back using null layout
//Lookup the first part with Rectangle and Point, and check why we need preferredSize
runningBack.setBounds(new Rectangle(new Point(925, 325), runningBack.getPreferredSize()));
//These 2 lines make sure that the button is the exact size of the image
runningBack.setBorder(BorderFactory.createEmptyBorder());
runningBack.setContentAreaFilled(false);
//Add the running back to the screen
add(runningBack);
//Create the linebacker
linebackerImage = new ImageIcon("linebacker.png");
//Set the button to be the linebacker image
linebacker = new JButton(linebackerImage);
//Allow to place the linebacker using the null layout
linebacker.setBounds(new Rectangle(new Point(350, 325), linebacker.getPreferredSize()));
//Make sure the button is the same size as the image
linebacker.setBorder(BorderFactory.createEmptyBorder());
linebacker.setContentAreaFilled(false);
//Add the linebacker to the screen
add(linebacker);
//Add the listeners, and make sure keys are being recorded
setFocusable(true);
addKeyListener(this);
requestFocusInWindow();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//Draws the image created in the constructor
g.drawImage(footballField, 0, 0, this);
}
@Override
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
if(k == e.VK_LEFT) {
Point currentPoint = runningBack.getLocation();
runningBack.setLocation(currentPoint.x - 4, currentPoint.y);
}
else if(k == e.VK_RIGHT) {
Point currentPoint = runningBack.getLocation();
runningBack.setLocation(currentPoint.x + 4, currentPoint.y);
}
else if(k == e.VK_UP) {
Point currentPoint = runningBack.getLocation();
runningBack.setLocation(currentPoint.x, currentPoint.y - 4);
}
else if(k == e.VK_DOWN) {
Point currentPoint = runningBack.getLocation();
runningBack.setLocation(currentPoint.x, currentPoint.y + 4);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
Scoreboard类似乎对keylistener的工作没有影响,所以我认为这不是问题。