日期:2014-05-20 浏览次数:20788 次
import java.awt.FlowLayout; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; public class KeyEventDemo extends JFrame implements KeyListener { /** * */ private static final long serialVersionUID = -9124455865998643315L; private static KeyEventDemo frm; private static JTextField tf; private static JTextArea ta; static { if (frm == null) frm = new KeyEventDemo(); if (tf == null) tf = new JTextField(20); if (ta == null) ta = new JTextArea("", 5, 20); } public static void main(String args[]) { frm.setSize(500, 300); frm.setTitle("KeyEvent Demo"); frm.setLayout(new FlowLayout(FlowLayout.CENTER)); tf.addKeyListener(frm); ta.setEditable(false); frm.add(tf); frm.add(ta); frm.setVisible(true); } // 当tf组件触发KeyEvent事件时,根据事件的种类执行下列的程序代码 public void keyPressed(KeyEvent e) { // 当按键按下时 switch (e.getKeyCode()) { case KeyEvent.VK_A: { ta.append("恭喜,你按下了a键\r\n"); break; } case KeyEvent.VK_B: { ta.append("恭喜,你按下了b键\r\n"); break; } default: { return; } } } public void keyReleased(KeyEvent e) { // 当按键放开时 // ta.append("keyReleased() 被调用\n"); } public void keyTyped(KeyEvent e) { // 键入内容时 // ta.append("keyTyped() 被调用\n"); } }
------解决方案--------------------
public void keyTyped(KeyEvent e) { // 键入内容时
// ta.append("keyTyped() 被调用\n");
}
}
------解决方案--------------------
JAVA技术总群 161734506 欢迎爱好者加入