JAVA键盘事件
我下面定义的面板在主类中测试时,按上下键并没有改变图像的位置,请问为什么importjava.awt.*;importjava.awt.event.*;importjav...
我下面定义的面板在主类中测试时,按上下键并没有改变图像的位置,请问为什么
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainPanel extends JPanel
{
ImageIcon image;
int x=100,y=0;
int moveY=10;
public MainPanel()
{
setPreferredSize(new Dimension(300,300));
addKeyListener(new K());
image = new ImageIcon("abc.gif");
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
image.paintIcon(this,page,x,y);
}
private class K implements KeyListener
{
public void keyPressed(KeyEvent event)
{
switch(event.getKeyCode())
{
case KeyEvent.VK_UP:
y=y-50;break;
case KeyEvent.VK_DOWN:
y=y+50;break;
}
repaint();
}
public void keyTyped(KeyEvent event){}
public void keyReleased(KeyEvent event){}
}
} 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainPanel extends JPanel
{
ImageIcon image;
int x=100,y=0;
int moveY=10;
public MainPanel()
{
setPreferredSize(new Dimension(300,300));
addKeyListener(new K());
image = new ImageIcon("abc.gif");
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
image.paintIcon(this,page,x,y);
}
private class K implements KeyListener
{
public void keyPressed(KeyEvent event)
{
switch(event.getKeyCode())
{
case KeyEvent.VK_UP:
y=y-50;break;
case KeyEvent.VK_DOWN:
y=y+50;break;
}
repaint();
}
public void keyTyped(KeyEvent event){}
public void keyReleased(KeyEvent event){}
}
} 展开
展开全部
//改了很多,方法有点不同
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainPanel extends JFrame
{
ImageIcon image;
int x = 100, y = 0;
int moveY = 10;
JLabel jp;
public MainPanel()
{
setPreferredSize(new Dimension(300, 300));
setSize(300,300);
image = new ImageIcon("src/abc.gif");
jp=new JLabel(image);
getContentPane().add(jp);
jp.setBounds(50, 50, 90, 90);
addKeyListener(new K());
setVisible(true);
}
private class K implements KeyListener
{
public void keyPressed(KeyEvent event)
{
if(event.getKeyCode()==KeyEvent.VK_UP)
{
x = 1;
jp.setBounds(100, 100, 90, 90);
}
else if(event.getKeyCode()==KeyEvent.VK_DOWN)
{
x = -1;
jp.setBounds(0, 0, 90, 90);
}
}
public void keyTyped(KeyEvent event)
{
}
public void keyReleased(KeyEvent event)
{
}
}
public static void main(String[] args)
{
MainPanel m=new MainPanel();
m.pack();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainPanel extends JFrame
{
ImageIcon image;
int x = 100, y = 0;
int moveY = 10;
JLabel jp;
public MainPanel()
{
setPreferredSize(new Dimension(300, 300));
setSize(300,300);
image = new ImageIcon("src/abc.gif");
jp=new JLabel(image);
getContentPane().add(jp);
jp.setBounds(50, 50, 90, 90);
addKeyListener(new K());
setVisible(true);
}
private class K implements KeyListener
{
public void keyPressed(KeyEvent event)
{
if(event.getKeyCode()==KeyEvent.VK_UP)
{
x = 1;
jp.setBounds(100, 100, 90, 90);
}
else if(event.getKeyCode()==KeyEvent.VK_DOWN)
{
x = -1;
jp.setBounds(0, 0, 90, 90);
}
}
public void keyTyped(KeyEvent event)
{
}
public void keyReleased(KeyEvent event)
{
}
}
public static void main(String[] args)
{
MainPanel m=new MainPanel();
m.pack();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询