java swing 按键_以编程方式单击Java Swing中的GUI按钮

这篇博客探讨了如何在Java Swing中模拟按键事件,尤其是如何通过编程方式实现按住SHIFT + A来触发按钮的点击行为,以及如何在按键释放时执行相关操作。文章详细解释了使用ButtonModel,按键绑定以及自定义Action来实现这一功能的方法。
摘要由CSDN通过智能技术生成

即使询问者对button.setMnemonic()感到满意,我仍在寻找类似设置助记符后的情况,即model.setPressed(true); model.setArmed(true);。实际上,您可以按住ALT + A而不发生任何事情(除了视觉变化)。 并且在释放键A(带有或不带有ALT)时,该按钮将触发ActionEvent。

我发现我可以使用button.setMnemonic()来获取ButtonModel(请参阅Java 8 API),然后使用model.setPressed(true); model.setArmed(true);(均由助记符更改)来直观地按下按钮,并通过将它们都设置为false来直观地释放按钮。 同时按下并布防了该按钮,该按钮会自动触发ActionEvent(调用model.setArmed(false)仅在视觉上更改该按钮)。

[摘自ButtonModel Java API文档]   当模型处于预备状态时释放鼠标时,将触发一个按钮,并触发一个ActionEvent [...]

为了使应用程序在按钮可见时(没有包含窗口或按钮需要成为焦点所有者,即当窗口中的另一个组件成为焦点时)对按键反应做出反应,我使用了按键绑定(请参见Java官方教程)。

工作代码:按SHIFT + A可以直观地按下按钮(与在button.setMnemonic()设置助记符后,按Alt键相反)。 并释放键以在控制台上打印操作命令(“按钮”)。

// MnemonicCode.java

import javax.swing.*;

import java.awt.event.*;

public class MnemonicCode extends JFrame

{

public MnemonicCode(int keyCode)

{

JButton button = new JButton("button");

getContentPane().add(button);

addMnemonicToButton(button,keyCode);

button.addActionListener(new ActionListener () {

public void actionPerformed(ActionEvent e)

{

System.out.println(e.getActionCommand());

}

});

pack();

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setVisible(true);

}

public static void main(String[] args) throws Exception

{

MnemonicCode bp = new MnemonicCode(KeyEvent.VK_A);

}

void addMnemonicToButton(JButton button,int keyCode)

{

int shiftMask = InputEvent.SHIFT_DOWN_MASK;

// signature: getKeyStroke(int keyCode, int modifiers, boolean onKeyRelease)

KeyStroke keyPress = KeyStroke.getKeyStroke(keyCode,shiftMask,false);

KeyStroke keyReleaseWithShift = KeyStroke.getKeyStroke(keyCode,shiftMask,true);

// get maps for key bindings

InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

ActionMap actionMap = button.getActionMap();

// add key bindings for pressing and releasing the button

inputMap.put(keyPress,"press"+keyCode);

actionMap.put("press"+keyCode, new ButtonPress(button));

inputMap.put(keyReleaseWithShift,"releaseWithShift"+keyCode);

actionMap.put("releaseWithShift"+keyCode, new ButtonRelease(button));

///*

// add key binding for releasing SHIFT before A

// if you use more than one modifier it gets really messy

KeyStroke keyReleaseAfterShift = KeyStroke.getKeyStroke(keyCode,0,true);

inputMap.put(keyReleaseAfterShift,"releaseAfterShift"+keyCode);

actionMap.put("releaseAfterShift"+keyCode, new ButtonRelease(button));

//*/

}

class ButtonPress extends AbstractAction

{

private JButton button;

private ButtonModel model;

ButtonPress(JButton button)

{

this.button = button;

this.model = button.getModel();

}

public void actionPerformed(ActionEvent e)

{

// visually press the button

model.setPressed(true);

model.setArmed(true);

button.requestFocusInWindow();

}

}

class ButtonRelease extends AbstractAction

{

private ButtonModel model;

ButtonRelease(JButton button)

{

this.model = button.getModel();

}

public void actionPerformed(ActionEvent e)

{

if (model.isPressed()) {

// visually release the button

// setPressed(false) also makes the button fire an ActionEvent

model.setPressed(false);

model.setArmed(false);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值