一个Java窗口带按键检测(跑Java可视小程序时用),沒加修饰,俺自己用滴。 package math; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; /** * 基本的绘图窗口,回车关闭窗口 */ @SuppressWarnings("serial") public class GraphicTest extends Frame implements KeyListener { /** * 按键检测 */ public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case 10: System.out.println("I am enter"); System.exit(0); break; case 37: // 左 break; case 38: // 上 break; case 39: // 右 break; case 40: // 下 break; } this.repaint(); } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public GraphicTest() { super("Frame-By-Lazybone"); setSize(1000, 800); this.addKeyListener(this); this.setVisible(true); } int width = 25; int startX = 50; int startY = 50; int count = 25; /** 绘制方法 **/ public void paint(Graphics g) { g.setColor(Color.black); /** 画棋盘 **/ for (int i = 1; i < count + 1; i++) { g.drawLine(startX + i * width, width + startX, startX + i * width, count * width + startY); } for (int i = 1; i < count + 1; i++) { g.drawLine(startX + width, i * width + startY, count * width + startX, i * width + startY); } } public static void main(String[] args) { new GraphicTest(); } }