本文介绍了使用JavaFX 2.2助记符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让JavaFX的助记符工作。我有一些场景按钮,我想要实现的是火由pressing按Ctrl + S这个按钮事件。
这里是一个code sceleton:

I'm trying to make JavaFX Mnemonic work. I have some button on scene and what I want to achieve is to fire this button event by pressing Ctrl+S.Here is a code sceleton:

@FXML
public Button btnFirst;

btnFirst.getScene().addMnemonic(new Mnemonic(btnFirst,
            new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN)));

按钮的mnemonicParsing是假的。 (当然,同时努力使这项工作,我试图把它设置为true,但没有结果)。 JavaFX的文件指出,当一个助记符注册在现场,与KeyCombination到达现场未使用,则目标节点将发送一个ActionEvent。但是,这并不工作,也许我做错了...

Button's mnemonicParsing is false. (Well, while trying to make this work I've tried to set it to true, but no result). JavaFX documentation states that when a Mnemonic is registered on a Scene, and the KeyCombination reaches the Scene unconsumed, then the target Node will be sent an ActionEvent. But this doesn't work, probably, I'm doing wrong...

我可以使用标准按钮的助记符(由mnemonicParsing设置为true和preFIX'F'信下划线)。不过这样一来用户必须使用Alt键,带来与菜单栏的浏览器一些奇怪的行为(如应用程序嵌入到网页比浏览器的菜单由pressing Alt + S键发射按钮事件之后激活)。
此外,标准的方式使得它不可能做出那样按Ctrl + Shift + F3等快捷方式。

I can use the standard button's mnemonic (by setting mnemonicParsing to true and prefix 'F' letter by underscore character). But this way user have to use Alt key, that brings some strange behaviour on browsers with menu bar (if application is embedded into web page than browser's menu activated after firing button event by pressing Alt+S).Besides, standard way makes it impossible to make shortcuts like Ctrl+Shift+F3 and so on.

所以,如果有一些方法,使这项工作?

So, if there some way to make this work?

推荐答案

有关您的使用情况下,我觉得你真的想使用加速器,而不是记忆。

For your use case, I think you actually want to use an accelerator rather than a mnemonic.

button.getScene().getAccelerators().put(
  new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN),
  new Runnable() {
    @Override public void run() {
      button.fire();
    }
  }
);

在大多数情况下,建议您使用KeyCombination.SHORTCUT_DOWN作为修改说明,如上面的code。这方面的一个很好的解释是文档:

In most cases it is recommended that you use KeyCombination.SHORTCUT_DOWN as the modifier specifier, as in the code above. A good explanation of this is in the KeyCombination documentation:

的快捷改性剂用于重新present修改键是
  在主机平台上的键盘快捷键常用。这是为了
  在Mac的Windows和元(命令键)的例子控制。通过使用
  快捷键修改开发人员可以创建独立于平台
  快捷键。因此,快捷键+ C组合键是内部处理
  在Mac在WindowsCTRL + C和元+ C。

如果你想专门code只能处理一个按Ctrl + S组合键,就可以使用:

If you wanted to specifically code to only handle a Ctrl+S key combination, they you could use:

new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN)

下面是一个可执行文件例如:

Here is an executable example:

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

示例输出:

这篇关于使用JavaFX 2.2助记符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:58
查看更多