从配置文件将 MenuItem 加速器加载到 FXML 中?

Load MenuItem accelerators into FXML from a configuration file?

我正在开发一个应用程序,它现在有一个带有加速器的扩展菜单。

我正在寻找一种方法,使用户可以为某些功能绑定按键,并为下次启动保存按键绑定。

有没有办法从配置文件加载 FXML 文件的 MenuItem 加速器?

否则,我可能需要为每个 MenuItem 函数单独序列化一个映射。

提前感谢您提供任何信息。

您可以使用 KeyCombination.getName() to get a String representation of a MenuItem accelerator and KeyCombination.keyCombination(String)String 表示创建 MenuItem 加速器。参见示例:

KeyCombination accelerator =  new KeyCodeCombination(KeyCode.D, KeyCombination.CONTROL_DOWN);
MenuItem menuItem = new MenuItem("Click me");
menuItem.setAccelerator(accelerator);

...

//save accelerator to the config file
String accString = menuItem.getAccelerator().getName();
saveAcceleratorToConfig(accString); // save string to file

...

//load accelerator from the config file
String accString = loadAcceleratorFromConfig(); //load string from file
menuItem.setAccelerator(KeyCombination.keyCombination(accString));