在Javafx中输入关键事件在对话框上不起作用? [英] Enter Key Event Is Not Working On Dialog In Javafx?

查看:147
本文介绍了在Javafx中输入关键事件在对话框上不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了下面的代码,它可以正常使用鼠标事件,但是当我在任何按钮上使用按键事件,即ENTER键而不显示结果时。

 警报警报=新警报(AlertType.CONFIRMATION); 
alert.setTitle(null);
alert.setHeaderText(null);
alert.setGraphic(null);
alert.setContentText(选择你的选项。);

ButtonType buttonTypeOne = new ButtonType(One);
ButtonType buttonTypeTwo = new ButtonType(Two);
ButtonType buttonTypeThree = new ButtonType(Three);

alert.getButtonTypes()。setAll(buttonTypeOne,buttonTypeTwo,buttonTypeThree);

可选< ButtonType> result = alert.showAndWait();
if(result.get()== buttonTypeOne){
System.out.println(One);
} else if(result.get()== buttonTypeTwo){
System.out.println(Two);
} else if(result.get()== buttonTypeThree){
System.out.println(Three);
}


解决方案

我不建议全部制作按钮响应输入,因为这与大多数UI对话框的工作方式相反。



通常情况下,当您按 space 而不是 enter 时,会触发一个具有焦点的按钮。然而,在特定键上会激活特殊按钮:A



如果您不希望JavaFX根据操作系统标准重新定位按钮,但您希望它们仍然响应输入 esc 键,然后您可以查找按钮并直接修改按钮属性,如下所示:

 按钮buttonTwo =(按钮)alert.getDialogPane()。lookupButton(buttonTypeTwo); 
buttonTwo.setDefaultButton(true);
Button buttonThree =(Button)alert.getDialogPane()。lookupButton(buttonTypeThree);
buttonThree.setCancelButton(true);



我建议让JavaFX正确定位特定类型的按钮,而不是像上面那样执行查找。



我还建议在你的设置中至少设置一个CANCEL_CLOSE按钮或OK_DONE按钮。 JavaFX Alert,否则用户可能很难实际关闭警报,因为对话框可能无法响应用户期望的按键操作。


I have tried the below code, it works fine with mouse event, but when I use key event i.e ENTER Key on any button than its not showing the result.

Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(null);
alert.setHeaderText(null);
alert.setGraphic(null);
alert.setContentText("Choose your option.");

ButtonType buttonTypeOne = new ButtonType("One");
ButtonType buttonTypeTwo = new ButtonType("Two");
ButtonType buttonTypeThree = new ButtonType("Three");

alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree);

Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne) {
     System.out.println("One");
} else if (result.get() == buttonTypeTwo) {
     System.out.println("Two");
} else if (result.get() == buttonTypeThree) {
     System.out.println("Three");
}

解决方案

I do not recommend making all buttons respond to enter, as that is counter to how most UI dialogs work.

Normally, a button with focus will fire when you press space, not enter. There are however special buttons that will activate on specific keys: A default button will fire on enter and a cancel button will fire on esc. Usually you will have only one of each of these special types of buttons in your dialog, so that they can be fired via the special keyboard accelerator, regardless of which button currently has focus.

Additionally, different desktop OS systems have different standards on placement of default and cancel buttons in a dialog system. This is to assist the user in easily finding these special buttons in any dialog. The JavaFX dialog system implements some logic internally for locating buttons in dialogs where the user would expect to see them across different desktop operating systems.

Let's say you want the button types from your example to be defined as default or cancel buttons and placed in the correct position for such buttons for your OS, then you can do as below:

ButtonType buttonTypeTwo = new ButtonType(
    "Two",
    ButtonBar.ButtonData.OK_DONE
);
ButtonType buttonTypeThree = new ButtonType(
    "Three",
    ButtonBar.ButtonData.CANCEL_CLOSE
);

Note the JavaFX system has automatically changed the position of the buttons and some of the color highlighting. When the user presses enter, then "Two" will fire, when the user presses esc, then "Three" will fire. If you run the same code on Windows or Linux, likely the buttons will be positioned differently, according to whatever button positioning standard is used for those OSes.

If you don't want JavaFX to reposition your buttons according to OS standards, but you want them to still respond to enter and esc keys, then you can lookup the buttons and directly modify the button attributes like below:

Button buttonTwo = (Button) alert.getDialogPane().lookupButton(buttonTypeTwo);
buttonTwo.setDefaultButton(true);
Button buttonThree = (Button) alert.getDialogPane().lookupButton(buttonTypeThree);
buttonThree.setCancelButton(true);

I recommend letting JavaFX appropriately position buttons of specific types rather than performing lookups as above.

I also recommend setting at least a CANCEL_CLOSE button or OK_DONE button in your JavaFX Alert, otherwise the user may have a difficult time actually closing the alert as the dialog will probably not respond to key presses as the user expects.

这篇关于在Javafx中输入关键事件在对话框上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆