gpt4 book ai didi

java - 用键盘激活 JButton

转载 作者:行者123 更新时间:2023-12-01 18:44:50 25 4
gpt4 key购买 nike

这是我的 JButton,“我可以显示完整代码”,

        JButton cor =new JButton();
cor.setText("Coor");
mainframe.add(cor);
window.getContentPane().add(mainframe);
window.pack();
window.setVisible(true);

我需要帮助,如何使用键盘激活此按钮,例如按“CTRL + A”

我红色了一些网站,发现我必须使用“按键绑定(bind)”,但仍然可以了解如何操作。

请不要说这个主题是重复的,因为以前的主题对我没有帮助。

cor.getInputMap().put(KeyStroke.getKeyStroke("F2"),"act"); 

Action act = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int xC = (int) b.getX();
int yC = (int) b.getY();
textArea.replaceSelection("X-Coordinates:" + xC + " Y-Coordinates: " + yC + "\n");
}
};

最佳答案

您只需更改 JButton 的属性即可获得短路径(在本例中为 ALT+C)

cor.setMnemonic(KeyEvent.VK_C);

您的代码将如下所示:

    JButton cor =new JButton();
cor.setText("Coor");
cor.setMnemonic(KeyEvent.VK_C);
mainframe.add(cor);
window.getContentPane().add(mainframe);
window.pack();
window.setVisible(true);

您还会在按钮名称中获得一个额外的字母“C”,并带有下划线,向用户显示该帮助。

关于java - 用键盘激活 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18241746/

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