gpt4 book ai didi

java - 按 Enter 键不会选择复选框

转载 作者:行者123 更新时间:2023-12-01 10:57:30 24 4
gpt4 key购买 nike

我已经实现了一个 JDialog,并除了“确定”和“取消”按钮之外添加了一个复选框。我已将“取消”设置为默认按钮,当出现对话框时,默认选择“取消”,按下 Enter 键后,将执行取消操作。

通过按 Tab 键,当我到达复选框并按 Enter 键时,它不会选择该复选框。但是,按空格键时,它会被选中。

但是,使用 Tab 键在“确定”和“取消”按钮之间切换,然后按 Enter 键,会执行相应的操作。

请建议应该做什么,以便在按 Enter 键时选中复选框。

最佳答案

每个 LAF 都有一个用于选择复选框的默认键。

查看Key Bindings这将显示 LAF 中复选框的默认绑定(bind)。

如果您想为另一个按键添加相同的功能,那么您需要管理按键绑定(bind)。因此,要处理 Enter 键,您应该使用:

InputMap im = checkB.getInputMap();
KeyStroke existingKeyStroke = KeyStroke.getKeyStroke("SPACE");
KeyStroke addedKeyStroke = KeyStroke.getKeyStroke("ENTER");
im.put(addedKeyStroke, im.get(existingKeyStroke));
existingKeyStroke = KeyStroke.getKeyStroke("released SPACE");
addedKeyStroke = KeyStroke.getKeyStroke("released ENTER");
im.put(addedKeyStroke, im.get(existingKeyStroke));

上面的代码将支持单个组件上的 Enter 键。如果您希望应用程序中的所有复选框都支持 Enter 键,那么您可以使用:

InputMap im = (InputMap)UIManager.get("CheckBox.focusInputMap");

关于java - 按 Enter 键不会选择复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33550148/

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