JAVA - 在按钮操作侦听器单击事件期间创建 KeyPress 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5682804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAVA - Creating a KeyPress Event During a Button Action Listener click event
提问by Jay
I am creating a java Sudoku GUI application at the moment.
我目前正在创建一个 java Sudoku GUI 应用程序。
The grid for showing the Sudoku puzzle is simply a 2 dimensional array of myJButtons(implementing JButton) - for this problem they can be treated as regular JButtons.
用于显示数独谜题的网格只是一个二维数组 myJButtons(实现 JButton) - 对于这个问题,它们可以被视为常规 JButtons。
The program will allow a button in the grid to be clicked, calling an actionlistener.
该程序将允许单击网格中的按钮,调用动作侦听器。
Is there a way to allow for a KeyAdapter Keypress to be created when a button is clicked to allow for a number press - physical key 1,2,3,4,5,6,7,8,9,0
有没有办法在单击按钮以允许按下数字时创建 KeyAdapter Keypress - 物理键 1,2,3,4,5,6,7,8,9,0
I would like the action listener to work only for when a button is clicked.
我希望动作侦听器仅在单击按钮时起作用。
A simpler example of this would be a Frame with a single button. when the button is pressed, the user can press a physical key on the keyboard, setting the jbutton text to the key value. Additional key presses would not change the button text, unless the button is clicked first.
一个更简单的例子是一个带有单个按钮的框架。当按钮被按下时,用户可以按下键盘上的物理键,将 jbutton 文本设置为键值。其他按键不会更改按钮文本,除非先单击该按钮。
class ClickAction implements ActionListener { // Action Listener called when button is Pressed
public void actionPerformed(ActionEvent ae) {
//need a way to create a keyevent listener here
}
}
Thanks In advance to anyone who can answer this!
提前感谢任何可以回答这个问题的人!
回答by
I would use another solution. Instead of creating KeyEvent Listeners every time a Button is clicked, you could register the key listener during the start up of the application. Then you can use a flag to check if the button was clicked first. Only if this flag is true, you perform the actions in the KeyEvent listener. Otherwise you skip all statements in the KeyEvent Listener.
我会使用另一种解决方案。您可以在应用程序启动期间注册键侦听器,而不是每次单击按钮时都创建 KeyEvent 侦听器。然后你可以使用一个标志来检查按钮是否被首先点击。仅当此标志为真时,您才在 KeyEvent 侦听器中执行操作。否则,您将跳过 KeyEvent 侦听器中的所有语句。
Here an example:
这里有一个例子:
public class TestClass {
private boolean isButtonClicked = false;
public void testYourButtons() {
JButton myButton = new JButton();
myButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
isButtonClicked = !isButtonClicked;
}
});
myButton.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent arg0) {
if (isButtonClicked) {
// TODO Do here your event handling
isButtonClicked = false;
}
}
});
}
}
回答by nikagra
As I've found, you can construct any new event and then send it through Component.processXxxEvent()
正如我发现的,您可以构建任何新事件,然后通过 Component.processXxxEvent()