ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ package draw.a5; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Test extends JFrame { Wdmb4 mb=null; public static void main(String[] args) { Test lx=new Test(); } public Test() { mb=new Wdmb4(); this.add(mb); this.addKeyListener(mb); this.setSize(400,300); this.setLocation(300,280); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } } class Wdmb4 extends JPanel implements KeyListener { int x=50, y=60; public void paint(Graphics g) { super.paint(g); g.fillRect(x,y,30,30); } public void keyTyped(KeyEvent e) { // 有字符输出的函数 } public void keyPressed(KeyEvent e) { // System.out.println("键盘被按下"); // System.out.println((char)e.getKeyCode()); if(e.getKeyCode()==KeyEvent.VK_DOWN) { //System.out.println("下"); y+=10; } else if(e.getKeyCode()==KeyEvent.VK_UP) { //System.out.println("上"); y--; } else if(e.getKeyCode()==KeyEvent.VK_LEFT) { //System.out.println("左"); x--; } else if(e.getKeyCode()==KeyEvent.VK_RIGHT) { //System.out.println("右"); x+=8; } this.repaint();//重绘 } public void keyReleased(KeyEvent e) { //按键抬起 } } ~~~ 移动上,下,左,右键方块会动 ![](https://box.kancloud.cn/38b760853e75923f868eeb42a09619f5_396x299.png)