java回车事件触发

回车事件:javaSwingUtils .enterPressesWhenFocused(Button)

java swing的文本框回车事件:SwingUtils .enterPressesWhenFocused(JTextField textField,ActionListener actionListener) 。其中actionListener是需要触发的事件。


import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JTextField;

import javax.swing.KeyStroke;

//SwingUtils 实用方法类

public class SwingUtils {

/**

* 对指定的button添加回车驱动事件的功能

* @param button

*/

public static void enterPressesWhenFocused(JButton button) {

button.registerKeyboardAction(button.getActionForKeyStroke(KeyStroke

.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),

KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),

JComponent.WHEN_FOCUSED);

button.registerKeyboardAction(button.getActionForKeyStroke(KeyStroke

.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),

KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),

JComponent.WHEN_FOCUSED);

}

/**

* 在文本输入框中回车触发事件

* @param textField

* @param actionListener

*/

public static void enterPressesWhenFocused(JTextField textField,

ActionListener actionListener) {

textField.registerKeyboardAction(actionListener,

KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),

JComponent.WHEN_FOCUSED);

textField.registerKeyboardAction(actionListener,

KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),

JComponent.WHEN_FOCUSED);

}

}

http://wenku.baidu.com/link?url=8gicLhbf3rLdsLn0yT_QHYolEZuGWwg35Q09Ceo4L0XnnChx0YVKbqguUd64ix6rA2YDL0Qil9JtdeNpiiW20NUtafvnCUP31WB3d8IhHtG

http://www.runoob.com/cplusplus/cpp-data-encapsulation.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值