键盘驱动组件事件
2014-12-30 16:03
218 查看
如何利用键盘驱动组件事件???
1:使用组件提供的getInputMap()返回一个InputMap对象,
该对象用于将KeyStroke对象(代
表键盘或者其他的输入事件)和名字关联起来;
2:使用组件的getActionMap()返回一个ActionMap对象,
该对象用于将指定的名字与事件Actioc联系起来(Action是ActionListener的子接口)
1:jtf.getInputMap().put(keyStroke, name);
2:jtf.getActionMap().put(name, action);
Demo1
public class BindKey extends JFrame {
JTextArea jta=new JTextArea(5,30);
JButton btn=new JButton("发送");
JTextField jtf=new JTextField(15);
public BindKey()
{
this.add(jta);
JPanel jp=new JPanel();
jp.add(jtf);
jp.add(btn);
this.add(jp,BorderLayout.SOUTH);
/**
* Action是ActionListener的子接口
*/
Action sentMsg=new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jta.append(jtf.getText()+"\n");
jtf.setText("");
}
};
btn.addActionListener(sentMsg);
// jtf.getInputMap().put(keyStroke, name);
jtf.getInputMap().put(KeyStroke.getKeyStroke('\n', java.awt.event.InputEvent.CTRL_MASK), "send");
/// jtf.getActionMap().put(name, action);
jtf.getActionMap().put("send",sentMsg);
this.pack();
this.setTitle("键盘与事件的绑定");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BindKey b=new BindKey();
}
}
1:使用组件提供的getInputMap()返回一个InputMap对象,
该对象用于将KeyStroke对象(代
表键盘或者其他的输入事件)和名字关联起来;
2:使用组件的getActionMap()返回一个ActionMap对象,
该对象用于将指定的名字与事件Actioc联系起来(Action是ActionListener的子接口)
1:jtf.getInputMap().put(keyStroke, name);
2:jtf.getActionMap().put(name, action);
Demo1
public class BindKey extends JFrame {
JTextArea jta=new JTextArea(5,30);
JButton btn=new JButton("发送");
JTextField jtf=new JTextField(15);
public BindKey()
{
this.add(jta);
JPanel jp=new JPanel();
jp.add(jtf);
jp.add(btn);
this.add(jp,BorderLayout.SOUTH);
/**
* Action是ActionListener的子接口
*/
Action sentMsg=new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jta.append(jtf.getText()+"\n");
jtf.setText("");
}
};
btn.addActionListener(sentMsg);
// jtf.getInputMap().put(keyStroke, name);
jtf.getInputMap().put(KeyStroke.getKeyStroke('\n', java.awt.event.InputEvent.CTRL_MASK), "send");
/// jtf.getActionMap().put(name, action);
jtf.getActionMap().put("send",sentMsg);
this.pack();
this.setTitle("键盘与事件的绑定");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BindKey b=new BindKey();
}
}
相关文章推荐
- 解决因焦点转移到其它组件,导致在 JFrame 中捕获不到键盘按键事件问题的方法
- python Tkinter图形用户界面组件(鼠标、键盘事件)
- 浅谈JSF的两大两点:面向组件编程和事件驱动模式
- 深入探究Tedit组件的键盘事件和鼠标事件
- Android 组件间通信--事件驱动
- 采用事件驱动编程以及GUI组件开发一个贷款计算器
- 【JAVA语言程序设计基础篇】--事件驱动程序设计--键盘事件
- 如何使得一个JComponent组件不论是否获得焦点,都可以响应键盘事件
- 【JAVA语言程序设计基础篇】--事件驱动程序设计--键盘事件
- 事件驱动的JScript面对象编程
- Netty2: 事件驱动的NIO框架(实践)
- Java Swing中的键盘事件处理
- C#处理鼠标和键盘事件
- DataGrid模板列中TextBox的焦点相应键盘事件
- 事件驱动的JScript面对象编程(例)
- C#处理鼠标和键盘事件
- HOW TO:在 Visual Studio .NET 中处理 Office 2003 Web 组件的事件
- Java Swing中的键盘事件处理
- [原创]代码范例:自定义组件和自定义事件
- JIURL键盘驱动 3