bes*_*s67 9 javafx shortcut button javafx-2
我正在努力使JavaFX Mnemonic工作.我在场景上有一些按钮,我想要实现的是按Ctrl + S来触发此按钮事件.这是一个代码方案:
@FXML
public Button btnFirst;
btnFirst.getScene().addMnemonic(new Mnemonic(btnFirst,
new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN)));
Run Code Online (Sandbox Code Playgroud)
Button的mnemonicParsing是错误的.(好吧,在尝试完成这项工作时,我试图将其设置为true,但没有结果).JavaFX文档指出,当在场景上注册助记符,并且KeyCombination到达未使用的场景时,目标节点将被发送一个ActionEvent.但这不起作用,可能,我做错了......
我可以使用标准按钮的助记符(通过将mnemonicParsing设置为true并使用下划线字符设置前缀'F'字母).但是这样用户必须使用Alt键,这会在带有菜单栏的浏览器上带来一些奇怪的行为(如果应用程序嵌入到网页中,而不是通过按Alt + S触发按钮事件后激活的浏览器菜单).此外,标准方式使得无法像Ctrl + Shift + F3等那样制作快捷方式.
那么,如果有某种方法可以使这项工作?
jew*_*sea 23
对于您的用例,我认为您实际上想要使用加速器而不是助记符.
button.getScene().getAccelerators().put(
new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN),
new Runnable() {
@Override public void run() {
button.fire();
}
}
);
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,建议您使用KeyCombination.SHORTCUT_DOWN作为修饰符说明符,如上面的代码所示.KeyCombination文档中有一个很好的解释:
快捷键修饰符用于表示主机平台上键盘快捷键中常用的修饰键.这是例如Windows上的控件和Mac上的元(命令键).通过使用快捷键修饰符,开发人员可以创建独立于平台的快捷方式.因此,"Shortcut + C"组合键在Windows内部处理为"Ctrl + C",在Mac上处理为"Meta + C".
如果你想专门编写只处理Ctrl + S组合键的代码,你可以使用它们:
new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN)
Run Code Online (Sandbox Code Playgroud)
这是一个可执行的例子:
import javafx.animation.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class SaveMe extends Application {
@Override public void start(final Stage stage) throws Exception {
final Label response = new Label();
final ImageView imageView = new ImageView(
new Image("http://icons.iconarchive.com/icons/gianni-polito/colobrush/128/software-emule-icon.png")
);
final Button button = new Button("Save Me", imageView);
button.setStyle("-fx-base: burlywood;");
button.setContentDisplay(ContentDisplay.TOP);
displayFlashMessageOnAction(button, response, "You have been saved!");
layoutScene(button, response, stage);
stage.show();
setSaveAccelerator(button);
}
// sets the save accelerator for a button to the Ctrl+S key combination.
private void setSaveAccelerator(final Button button) {
Scene scene = button.getScene();
if (scene == null) {
throw new IllegalArgumentException("setSaveAccelerator must be called when a button is attached to a scene");
}
scene.getAccelerators().put(
new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN),
new Runnable() {
@Override public void run() {
fireButton(button);
}
}
);
}
// fires a button from code, providing visual feedback that the button is firing.
private void fireButton(final Button button) {
button.arm();
PauseTransition pt = new PauseTransition(Duration.millis(300));
pt.setOnFinished(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
button.fire();
button.disarm();
}
});
pt.play();
}
// displays a temporary message in a label when a button is pressed,
// and gradually fades the label away after the message has been displayed.
private void displayFlashMessageOnAction(final Button button, final Label label, final String message) {
final FadeTransition ft = new FadeTransition(Duration.seconds(3), label);
ft.setInterpolator(Interpolator.EASE_BOTH);
ft.setFromValue(1);
ft.setToValue(0);
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
label.setText(message);
label.setStyle("-fx-text-fill: forestgreen;");
ft.playFromStart();
}
});
}
private void layoutScene(final Button button, final Label response, final Stage stage) {
final VBox layout = new VBox(10);
layout.setPrefWidth(300);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(button, response);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 20; -fx-font-size: 20;");
stage.setScene(new Scene(layout));
}
public static void main(String[] args) { launch(args); }
}
// icon license: (creative commons with attribution) http://creativecommons.org/licenses/by-nc-nd/3.0/
// icon artist attribution page: (eponas-deeway) http://eponas-deeway.deviantart.com/gallery/#/d1s7uih
Run Code Online (Sandbox Code Playgroud)
样本输出: