跪求高手----java小错误---键盘事件-------(功能:实现键盘上下左右键移动图片也跟着走动)
为什么我写的键盘点击了,图片没有跟动?publicclassFirstextendsJFrameimplementsKeyListener{privateImageIco...
为什么我写的键盘点击了,图片没有跟动?
public class First extends JFrame implements KeyListener
{
private ImageIcon i=new ImageIcon("1.gif");
private JLabel label=new JLabel(i);
private JPanel panel=new JPanel();
public First()
{
panel.setLayout(null);
this.add(panel); panel.add(label);
this.setSize(500,500);
label.setBounds(200, 300,50, 40);
this.addKeyListener(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void ss(KeyEvent e)
{
int x=label.getX();
int y=label.getY();
System.out.print(x+" "+y+"...");
if(e.getKeyChar()==KeyEvent.VK_UP)
{
label.setLocation(x, y-5);
}
else if(e.getKeyChar()==KeyEvent.VK_DOWN)
{
label.setLocation(x, y+5);
}
else if(e.getKeyChar()==KeyEvent.VK_LEFT)
{
label.setLocation(x-5, y);
}
else if(e.getKeyChar()==KeyEvent.VK_RIGHT)
{
label.setLocation(x+5, y);
}
}
@Override
public void keyPressed(KeyEvent e) {
ss(e);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args)
{
new First();
}
} 展开
public class First extends JFrame implements KeyListener
{
private ImageIcon i=new ImageIcon("1.gif");
private JLabel label=new JLabel(i);
private JPanel panel=new JPanel();
public First()
{
panel.setLayout(null);
this.add(panel); panel.add(label);
this.setSize(500,500);
label.setBounds(200, 300,50, 40);
this.addKeyListener(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void ss(KeyEvent e)
{
int x=label.getX();
int y=label.getY();
System.out.print(x+" "+y+"...");
if(e.getKeyChar()==KeyEvent.VK_UP)
{
label.setLocation(x, y-5);
}
else if(e.getKeyChar()==KeyEvent.VK_DOWN)
{
label.setLocation(x, y+5);
}
else if(e.getKeyChar()==KeyEvent.VK_LEFT)
{
label.setLocation(x-5, y);
}
else if(e.getKeyChar()==KeyEvent.VK_RIGHT)
{
label.setLocation(x+5, y);
}
}
@Override
public void keyPressed(KeyEvent e) {
ss(e);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args)
{
new First();
}
} 展开
2个回答
展开全部
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class First extends JFrame implements KeyListener {
private ImageIcon i = new ImageIcon("1.gif");
private JLabel label = new JLabel(i);
private JPanel panel = new JPanel();
public First() {
panel.setLayout(null);
getContentPane().add(panel);
panel.add(label);
this.setSize(500, 500);
label.setBounds(200, 300, 50, 40);
this.addKeyListener(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void ss(KeyEvent e) {
int x = label.getX();
int y = label.getY();
System.out.print(x + " " + y + "...");
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP) {
label.setLocation(x, y - 5);
} else if (keyCode == KeyEvent.VK_DOWN) {
label.setLocation(x, y + 5);
} else if (keyCode == KeyEvent.VK_LEFT) {
label.setLocation(x - 5, y);
} else if (keyCode == KeyEvent.VK_RIGHT) {
label.setLocation(x + 5, y);
}
}
@Override
public void keyPressed(KeyEvent e) {
ss(e);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
new First();
}
}
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class First extends JFrame implements KeyListener {
private ImageIcon i = new ImageIcon("1.gif");
private JLabel label = new JLabel(i);
private JPanel panel = new JPanel();
public First() {
panel.setLayout(null);
getContentPane().add(panel);
panel.add(label);
this.setSize(500, 500);
label.setBounds(200, 300, 50, 40);
this.addKeyListener(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void ss(KeyEvent e) {
int x = label.getX();
int y = label.getY();
System.out.print(x + " " + y + "...");
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP) {
label.setLocation(x, y - 5);
} else if (keyCode == KeyEvent.VK_DOWN) {
label.setLocation(x, y + 5);
} else if (keyCode == KeyEvent.VK_LEFT) {
label.setLocation(x - 5, y);
} else if (keyCode == KeyEvent.VK_RIGHT) {
label.setLocation(x + 5, y);
}
}
@Override
public void keyPressed(KeyEvent e) {
ss(e);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
new First();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询