I originally wanted to write a program to draw lines according to the input up and down, but why does this getKeyCode only return 0?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class q09 extends JFrame
{
private int x=150,y=150,index=0;
private line []l;
public q09()
{
huaban p1=new huaban();
add(p1);
l=new line[100];
p1.setFocusable(true);
p1.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
System.out.println(e.getKeyCode());
//getKeyCode
}
});
}
class line
{
private int sx,sy,VK,px,py;
public line(int VK)
{
sx=x;
sy=y;
px=x;
py=y;
switch(VK)
{
case KeyEvent.VK_UP:py+=10;
case KeyEvent.VK_DOWN:py-=10;
case KeyEvent.VK_LEFT:px-=10;
case KeyEvent.VK_RIGHT:px+=10;
}
x=px;
y=py;
}
}
public static void main(String[] args)
{
JFrame frame=new q09();
frame.setTitle("q09");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
class huaban extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i=0;i<index;iPP)
g.drawLine(l[i].sx, l[i].sy,l[i].px,l[i].py);
}
}
}