如何在java中设置键盘的上下左右键
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SketchPanel extends JPanel implements KeyListener
{
public SketchPanel()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent evt)
{
int keyCode =evt.getKeyCode();
int d;
if (evt.isShiftDown())
d=5;
else
d=1;
if (keyCode==KeyEvent.VK_LEFT) add(-d,0);
else if (keyCode ==KeyEvent.VK_RIGHT) add(d,0);
else if (keyCode ==KeyEvent.VK_UP) add(0,-d);
else if (keyCode ==KeyEvent.VK_DOWN) add(0,d);
//repaint();
}
public void keyReleased(KeyEvent evt){}
public void keyTyped(KeyEvent evt)
{
char keyChar =evt.getKeyChar ();
int d;
if (Character.isUpperCase(keyChar))
{ d=5;
keyChar=Character.toLowerCase(keyChar);
}
else
d=1;
if (keyChar=='h') add(-d,0);
else if (keyChar=='1') add(d,0);
else if (keyChar=='k') add(0,-d);
else if (keyChar=='j') add(d,0);
//repaint();
}
public boolean isFocusTraversable(){return true;}
public void add(int dx,int dy)
{
end.x+=dx;
end.y+=dy;
Graphics g=getGraphics();
g.drawLine(start.x,start.y,end.x,end.y);
//g.fillOval(end.x,end.y,10,10);
g.dispose();
start.x=end.x;
start.y=end.y;
}
private Point start=new Point(0,0);
private Point end=new Point(0,0);
}
class SketchFrame extends JFrame
{
public SketchFrame()
{
Container contentPane=getContentPane();
contentPane.add(new SketchPanel());
setTitle("draw by hand");
setSize(300,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
public class Sketch
{
public static void main(String args[])
{
JFrame test=new SketchFrame();
test.setVisible(true);
}
}
import java.awt.event.*;
import javax.swing.*;
class SketchPanel extends JPanel implements KeyListener
{
public SketchPanel()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent evt)
{
int keyCode =evt.getKeyCode();
int d;
if (evt.isShiftDown())
d=5;
else
d=1;
if (keyCode==KeyEvent.VK_LEFT) add(-d,0);
else if (keyCode ==KeyEvent.VK_RIGHT) add(d,0);
else if (keyCode ==KeyEvent.VK_UP) add(0,-d);
else if (keyCode ==KeyEvent.VK_DOWN) add(0,d);
//repaint();
}
public void keyReleased(KeyEvent evt){}
public void keyTyped(KeyEvent evt)
{
char keyChar =evt.getKeyChar ();
int d;
if (Character.isUpperCase(keyChar))
{ d=5;
keyChar=Character.toLowerCase(keyChar);
}
else
d=1;
if (keyChar=='h') add(-d,0);
else if (keyChar=='1') add(d,0);
else if (keyChar=='k') add(0,-d);
else if (keyChar=='j') add(d,0);
//repaint();
}
public boolean isFocusTraversable(){return true;}
public void add(int dx,int dy)
{
end.x+=dx;
end.y+=dy;
Graphics g=getGraphics();
g.drawLine(start.x,start.y,end.x,end.y);
//g.fillOval(end.x,end.y,10,10);
g.dispose();
start.x=end.x;
start.y=end.y;
}
private Point start=new Point(0,0);
private Point end=new Point(0,0);
}
class SketchFrame extends JFrame
{
public SketchFrame()
{
Container contentPane=getContentPane();
contentPane.add(new SketchPanel());
setTitle("draw by hand");
setSize(300,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
public class Sketch
{
public static void main(String args[])
{
JFrame test=new SketchFrame();
test.setVisible(true);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询