如何在java中设置键盘的上下左右键

如何在java中设置键盘的上下左右键,第1张

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)

}

}

Android虚拟键盘上下左右键按下和d起的响应事件,代码非常简单,列举如下:

首先,新建工程

在mian.xml布局中添加一个TextView控件(主要用来显示按键反馈)

然后,在java文件中写代码,参看下面代码:

public class HelloAndroidActivity extends Activity {

private TextView mTextView

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState)

setContentView(R.layout.main)

//获取布局中的TextView控件

mTextView = (TextView) findViewById(R.id.textView1)

}

// 按键按下,所触发的事件

public boolean onKeyDown(int keyCode, KeyEvent event) {

switch (keyCode) {

case KeyEvent.KEYCODE_DPAD_CENTER:

display("中键被按下")

break

case KeyEvent.KEYCODE_DPAD_DOWN:

display("下键被按下")

break

case KeyEvent.KEYCODE_DPAD_LEFT:

display("左键被按下")

break

case KeyEvent.KEYCODE_DPAD_RIGHT:

display("右键被按下")

break

case KeyEvent.KEYCODE_DPAD_UP:

display("上键被按下")

break

}

return super.onKeyDown(keyCode, event)

}

// 按键d起,所触发的事件

public boolean onKeyUp(int keyCode, KeyEvent event) {

switch (keyCode) {

case KeyEvent.KEYCODE_DPAD_CENTER:

display("中键被d起")

break

case KeyEvent.KEYCODE_DPAD_DOWN:

display("下键被d起")

break

case KeyEvent.KEYCODE_DPAD_LEFT:

display("左键被d起")

break

case KeyEvent.KEYCODE_DPAD_RIGHT:

display("右键被d起")

break

case KeyEvent.KEYCODE_DPAD_UP:

display("上键被d起")

break

}

return super.onKeyUp(keyCode, event)

}

// 用来显示文本框内容

public void display(String str) {

mTextView.setText(str)

}

}

运行工程即可看到效果

在你的

jFrame

窗体上添加键盘监听事件,

在监听事件里添加几个判断,

keyCode

分别为

"上,下,左,右"

时,

修改你要移动的物体的坐标位置,就可以达到按键盘控制物体移动了.


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11595064.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存