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

猜你喜欢

转载自blog.csdn.net/qq_36935585/article/details/62891435