快速业务通道

java的键盘事件 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-16

java的键盘事件

时间:2011-06-19 BlogJava zht

最近看到有人问怎么模拟钢琴操作

想了想怎么实现 顺便看了下键盘事件

可以用两种方式

1、

public class KeystrokeTest extends JApplet { private JButton button = new JButton("button"); public void init() {   Container contentPane = getContentPane();   JPanel panel = new JPanel();   JCheckBox checkbox = new JCheckBox("checkbox");   JButton southButton = new JButton("south button");   Listener listener = new Listener();   panel.setBorder(BorderFactory    .createTitledBorder(("Ancestor of button and checkbox")));   checkbox.registerKeyboardAction(listener, KeyStroke.getKeyStroke (    KeyEvent.VK_F, 0, false), JComponent.WHEN_FOCUSED);   panel.registerKeyboardAction(listener, KeyStroke.getKeyStroke (    KeyEvent.VK_A, InputEvent.ALT_MASK, false),    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);   southButton.registerKeyboardAction(listener, KeyStroke.getKeyStroke(    KeyEvent.VK_W, 0, true), JComponent.WHEN_IN_FOCUSED_WINDOW);   panel.add(button);   panel.add(checkbox);   contentPane.add(panel, "Center");   contentPane.add(southButton, "South"); } } class Listener implements ActionListener { public void actionPerformed(ActionEvent e) {   Object src = e.getSource();   String cname = src.getClass().getName();   if (src instanceof JCheckBox) {   System.out.print("''f'' key PRESSED when checkbox");   System.out.println(" had focus");   } else if (src instanceof JPanel) {   System.out.print("''ALT-a'' key PRESSED when ancestor");   System.out.println(" of titled panel had focus");   } else if (src instanceof JButton) {   System.out.print("''w'' key RELEASED when any");   System.out.println(" component in window had focus");   }   System.out.println("Source: " + cname);   System.out.println(); } }

javaμ??ü?ìê??t(2)

ê±??:2011-06-19 BlogJava zht

2?¢

public class T { public static void main(String[] args) { ?? try { ?? UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName ()); ?? } catch (Exception e) { ?? } ?? final JTextField tf = new JTextField(); ?? tf.setEnabled(false); ?? JPanel p = new JPanel(new GridLayout(0, 3, 5, 5)); ?? for (int i = 0; i <= 9; i++) { ?? final JButton btn = new JButton(String.valueOf(i)); ?? p.add(btn); ?? btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( ???? KeyStroke.getKeyStroke((char) (i + ''0'')), "PressKeyAction"); ?? btn.getActionMap().put("PressKeyAction", new AbstractAction() { ???? public void actionPerformed(ActionEvent e) { ???? btn.doClick(); ???? } ?? }); ?? btn.addActionListener(new ActionListener() { ???? public void actionPerformed(ActionEvent e) { ???? tf.setText(tf.getText() + btn.getText()); ???? } ?? }); ?? } ?? JFrame f = new JFrame(); ?? JComponent contentPane = (JComponent) f.getContentPane(); ?? contentPane.setLayout(new BorderLayou

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号