禁用JButton的助记符功能

3

我需要帮助取消JButton的助记符。实际上,我正在使用第三方API,他们将助记符设置为“Alt C”。因此,我想要删除这个助记符,并且不想为这个compButton设置任何内容(即想要删除助记符)。

    // Alt + 'C' selects the comp.
     compButton.setMnemonic(KeyEvent.VK_C);
1个回答

4
如何使用 compButton.setMnemonic(0);
编辑:
我看到了 javax/swing/AbstractButton.java:
/**
 * Returns key bindings associated with this object
 *
 * @return the key bindings, if supported, of the object;
 * otherwise, null
 * @see AccessibleKeyBinding
 * @since 1.4
 */
public AccessibleKeyBinding getAccessibleKeyBinding() {
    int mnemonic = AbstractButton.this.getMnemonic();
    if (mnemonic == 0) {
        return null;
    }
    return new ButtonKeyBinding(mnemonic);
}

因此,compButton.setMnemonic(0);compButton.setMnemonic(-1); 更好。

1
0 实际上是为 KeyEvent 定义的。如果您想采用这种方法,我建议使用负值。 - user1803551

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接