0

我的代码如下:

 public static int mShortcut=50;

 @FXML private void OnSignIn(ActionEvent event )
 {

      setShortcut(101);
      CheckShortCut();


 }
 public void setShortcut(int shortcut) 
{
     mShortcut=shortcut;  
     mMenuItemProdType.getAccelerator();
     CheckShortCut();

}
 public  void CheckShortCut()
{
    switch(mShortcut)
    {
        case 101:
             System.out.println("Enter in 3 Case");

             mMenuItemProdType.setAccelerator(new KeyCodeCombination(KeyCode.T, KeyCombination.CONTROL_DOWN, KeyCodeCombination.SHORTCUT_DOWN));
             break;
        case 50:
              System.out.println("Enter in 50 Case");

             mMenuItemProdType.setAccelerator(null);
            break;
         default:
             mMenuItemProdType.setAccelerator(null);
             break;
    }

} 

我的要求用户如果没有登录就无法访问快捷键,所以在 OnSignIn 我放了两个方法 setShortcut(101); 和 CheckShortCut(); 但是在上面的代码中登录后我无法获得快捷方式事件所以知道如何解决它吗?

4

1 回答 1

0

我通过禁用菜单项解决了这个问题。

@FXML private MenuItem signMenuItem;
@FXML private MenuItem openMenuItem;
@FXML private MenuItem saveMenuItem;

@FXML
private void OnSignIn(ActionEvent event) {
    if (sign("admin", "1234")) {
        openMenuItem.setDisable(false);
        saveMenuItem.setDisable(false);
    }
}

private boolean sign(String name, String pass) {
    // do sign in
    return true;
}
@Override
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
    this.signMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.ENTER, KeyCombination.CONTROL_DOWN, KeyCombination.SHORTCUT_DOWN));
    this.openMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.O, KeyCombination.CONTROL_DOWN, KeyCombination.SHORTCUT_DOWN));
    this.saveMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN, KeyCombination.SHORTCUT_DOWN));
    openMenuItem.setDisable(true);
    saveMenuItem.setDisable(true);
}
于 2013-01-25T10:03:08.733 回答