代码运行时没有任何错误。它运行除我的按键以外的所有部分。哪里出了问题。
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();
}
}
我添加了错误println
,以查看某些代码是否未运行,但所有代码都在运行。
答案 0 :(得分:0)
正如@this_is_cat先前解释的那样,侦听器需要在JFrame上,而不是在JLabel上。
此外,使用setLayout(null)也不是一个好习惯,但在这里使用它是因为我们只有一个组件可以使用。
package keylistener1;
import java.awt.Dimension;
import java.awt.HeadlessException;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
// Inheriting the JFrame class
public class KeyListener1 extends JFrame {
private static final String ICON_FILE_PATH = "btn_home.gif";
// Scanner d = new Scanner(System.in);
private JLabel label = new JLabel("image not found");
private JFrame jframe = new JFrame();
private int xpos = 250;
private int ypos = 250;
public KeyListener1() throws HeadlessException {
initFrame();
}
private JFrame initFrame() {
jframe.setLayout(null);
jframe.setPreferredSize(new Dimension(525, 525));
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
jframe.setTitle("Key Listener");
// set up image icon
Class cls = this.getClass();
try (InputStream imageSource = cls.getResourceAsStream(ICON_FILE_PATH)) {
BufferedImage bi = ImageIO.read(imageSource);
ImageIcon p = new ImageIcon(bi);
setLabel(new JLabel(p));
getLabel().setBounds(getXpos(), getYpos(), 50, 50);
} catch (IOException ex) {
Logger.getLogger(KeyListener1.class.getName()).log(Level.SEVERE, null, ex);
}
initKeyListener();
jframe.add(getLabel());
jframe.pack();
System.out.println("Starting coordinates: " + getXpos() + "," + getYpos());
return jframe;
}
private void initKeyListener() {
getJframe().addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
System.out.println("jf: " + e.getKeyChar());
char input = e.getKeyChar();
if (input == 'w') {
// ypos -= 50;
setYpos(getYpos() - 50);
} else if (input == 'a') {
// xpos -= 50;
setXpos(getXpos() - 50);
} else if (input == 's') {
// ypos += 50;
setYpos(getYpos() + 50);
} else if (input == 'd') {
// xpos += 50;
setXpos(getXpos() + 50);
}
System.out.println("New Coordinates: " + getXpos() + "," + getYpos());
getLabel().setBounds(getXpos(), getYpos(), 50, 50);
}
});
}
public JLabel getLabel() {
return label;
}
public void setLabel(JLabel label) {
this.label = label;
}
public int getXpos() {
return xpos;
}
public void setXpos(int xpos) {
this.xpos = xpos;
}
public int getYpos() {
return ypos;
}
public void setYpos(int ypos) {
this.ypos = ypos;
}
public JFrame getJframe() {
return jframe;
}
public void setJframe(JFrame jframe) {
this.jframe = jframe;
}
public static void main(String[] args) {
new KeyListener1();
}
}