gpt4 book ai didi

java - 如何在按钮单击java时显式触发按键事件

转载 作者:行者123 更新时间:2023-11-29 05:20:53 25 4
gpt4 key购买 nike

是否有任何机制可以在单击按钮时显式触发按键事件。当我单击在 Java UI 中名为“+”的按钮时,它将明确触发“+”按键事件。

最佳答案

您可以通过多种方式实现这一目标,具体使用哪一种取决于您的尝试。

你可以...

使用 java.awt.Robot,这将允许您在 native 事件队列中生成按键事件。不过请记住,此事件将(最终)分派(dispatch)给具有当前键盘焦点的组件...

Robot bot = new Robot();
bot.setAutoDelay(250);
bot.keyPress(KeyEvent.SHIFT_KEY);
bot.keyPress(KeyEvent.EQUALS); // Shift + "=" = +
bot.keyRelease(KeyEvent.EQUALS);
bot.keyRelease(KeyEvent.SHIFT_KEY);

你可以...

手动发送事件。如果您尝试将事件发送到另一个组件,例如一个字段,您可以手动分派(dispatch)该事件

field.dispatchEvent(new KeyEvent(field, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.SHIFT_DOWN_MASK, KeyEvent.VK_EQUALS, '+'));
field.dispatchEvent(new KeyEvent(field, KeyEvent.KEY_TYPED, System.currentTimeMillis(), KeyEvent.SHIFT_DOWN_MASK, KeyEvent.VK_EQUALS, '+'));
field.dispatchEvent(new KeyEvent(field, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), KeyEvent.SHIFT_DOWN_MASK, KeyEvent.VK_EQUALS, '+'));

例如……

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ButtonTrigger {

public static void main(String[] args) {
new ButtonTrigger();
}

public ButtonTrigger() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private JTextField field;
private JButton button;

public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;

field = new JTextField(10);
button = new JButton("+");

add(field, gbc);
add(button, gbc);

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field.dispatchEvent(new KeyEvent(field, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.SHIFT_DOWN_MASK, KeyEvent.VK_EQUALS, '+'));
field.dispatchEvent(new KeyEvent(field, KeyEvent.KEY_TYPED, System.currentTimeMillis(), KeyEvent.SHIFT_DOWN_MASK, KeyEvent.VK_UNDEFINED, '+'));
field.dispatchEvent(new KeyEvent(field, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), KeyEvent.SHIFT_DOWN_MASK, KeyEvent.VK_EQUALS, '+'));
}
});
}

}

}

关于java - 如何在按钮单击java时显式触发按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24817584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com