package com.zym.lesson13; import javax.swing.*; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class TestKeyboardListener { public static void main(String[] args) { new KeyWinFrame("lesson13-键盘监听事件"); } } class KeyWinFrame extends JFrame { //构造器 public KeyWinFrame(String title){ setBounds(100,100,400,200); setTitle(title); setBackground(Color.orange); this.addKeyListener(new MyKeyboardListener()); //设置可见 setVisible(true); } //适配器模式 ,监听器类 private class MyKeyboardListener extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { int keyCode= e.getKeyCode(); System.out.println(keyCode); if(keyCode==KeyEvent.VK_LEFT){ System.out.println("你按下了LEFT键"); } } } }
标签:java,awt,监听,键盘,public,lesson13,import,keyCode From: https://www.cnblogs.com/zym97816/p/17764266.html