我想在一個簡單的計算器上添加一個使用Ctrl鍵 +JButton單擊鼠標的快捷選項。我知道如何輸入Ctrl+ CusingKeyStroke但我不知道如何JButton使用鼠標單擊來完成。這是我所擁有的:private void displayCopyRight() { buttons[13].setAction(new AbstractAction("" + button_Shapes[13]) { { putValue(Action.ACTION_COMMAND_KEY, getValue(Action.NAME)); } public void actionPerformed(ActionEvent e){ display.setText("[c] Khoa-Nguyen"); } }); int c = JComponent.WHEN_IN_FOCUSED_WINDOW; KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK); buttons[13].getInputMap(c).put(ks, button_Shapes[13]); buttons[13].getActionMap().put(button_Shapes[13], buttons[13].getAction()); }這只會將鍵盤上的 Ctrl + 字母 C 綁定在一起。我想要做的是使用鼠標綁定 Ctrl + JButton 單擊。按鈕 [13] 是字母 C 作為 JButton。請幫忙。
1 回答

慕的地6264312
TA貢獻1817條經驗 獲得超6個贊
您可以嘗試檢查 ActionEvent 的修飾符屬性。它是一個掩碼字段 如果Ctrl按住鍵,則可以測試修飾符字段的 ActionEvent.CTRL_MASK。
public void actionPerformed(ActionEvent e){
if ((ActionEvent.CTRL_MASK & e.getModifiers()) != 0){
// Do your action here
}
}
添加回答
舉報
0/150
提交
取消