当前位置: 首页 > 知识库问答 >
问题:

使用JavaFX2.2助记符(和加速器)

季城
2023-03-14

我正在努力使JavaFX助记功能发挥作用。我在现场有一些按钮,我想要实现的是通过按Ctrl+S来激发这个按钮事件。以下是代码Sceleton:

@FXML
public Button btnFirst;

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

共有1个答案

左丘弘致
2023-03-14

对于您的用例,我认为您实际上希望使用加速器而不是助记符。

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

在大多数情况下,建议您使用keycombination.shortcut_down作为修饰符说明符,如上面的代码所示。在键组合文档中有一个很好的解释:

快捷键修改器用于表示主机平台上键盘快捷键中常用的修改键。例如,这是Windows上的控件和Mac上的meta(命令键)。通过使用快捷键修改器,开发人员可以创建与平台无关的快捷方式。因此,“Shortcut+C”组合键在Windows上被内部处理为“Ctrl+C”,在Mac上被处理为“Meta+C”。

new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN)
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
    null

链接的问题报告包括一个解决方法,您可以使用该方法在应用程序的多个位置定义和使用相同的加速器(例如,在不同上下文菜单中的两个不同菜单项上)。

请注意,这只适用于尝试在应用程序中的多个位置使用相同的加速器,如果您不需要尝试这样做,那么您可以忽略此信息。

 类似资料:
  • 在回答有关Python和/或逻辑的问题时,Spacetoast写道:

  • 本文向大家介绍使用CDN和AJAX加速WordPress中jQuery的加载,包括了使用CDN和AJAX加速WordPress中jQuery的加载的使用技巧和注意事项,需要的朋友参考一下 确定要放在Head部分 ? 事实上最好的情况是,js文件都不要在<head>部分进行加载,否则会影响到head部分的载入速度,直接导致网站的内容(body)载入延迟。如果你确定你不需要在head部分载入jQuer

  • 问题内容: 哪一个更好?使用如下速记: 或像这样长手: 相关: 性能(浏览器性能,文件大小) 文档(开发人员易于维护) 和别的 问题答案: 除非您已概要分析页面加载并且它已成为瓶颈,否则您不必担心CSS性能(我怀疑,它几乎总是多个HTTP请求和图像)。 任何有能力的开发人员都可以从上到下顺时针记住值的顺序。 速记意味着要发送的字节数更少,CSS缩小器并不会优化自身(我不认为)。 如果设置一个值,例

  • 下面的代码是创建(未显示)的方法的一部分,它有几个选项,如撤消、重做、剪切、复制、粘贴等。。。 类仅用于调试目的,但这是我通常放置类的地方 如您所见,我已经将“重做”动作的助记词设置为Y,将“剪切”动作设置为X。 我使用的是中的内置方法,它会自动将我的修改器设置为control,并在我想要的时候精确剪切文本。(CTRL-X) 这里的问题是:由于我的不使用DefaultEditorKit,它将修饰符

  • 我的grails应用程序中有一个配置错误,导致我的附加程序将输出发送到错误的地方,或者根本不发送。似乎有许多关于重复日志记录的问题,但我无法对我的情况应用任何答案。 下面是我的日志配置片段: 当我的程序运行时,输出将出现在、和中。直接发送到stdout的输出(绕过log4j)出现在catalina.out中 如果我设置 那么任何文件appender都不会被记录,但是stdout仍然会进入catal