简体   繁体   English

javafx:如何将 Enter 键绑定到按钮并在单击时触发事件?

[英]javafx: How to bind the Enter key to a button and fire off an event when it is clicked?

Basically, I have a okayButton that sits in a stage and when it is clicked , it performs a list of tasks.基本上,我有一个位于stageokayButton ,当它被点击时,它会执行一系列任务。 Now I want to bind the Enter key to this button such that when it is clicked OR the ENTER key is pressed , it performs a list of tasks.现在我想将Enter键绑定到这个按钮,这样当它被点击或 ENTER 键被按下时,它会执行一个任务列表。

    okayButton.setOnAction(e -> {       
           .........
        }
    });

How can I do that ?我怎样才能做到这一点 ? I have read the following post already.我已经阅读了以下帖子 However, it did not help me to achieve what I want to do.然而,它并没有帮助我实现我想做的事情。

First, set a hanlder on your button :首先,在您的按钮上设置一个处理程序:

okayButton.setOnAction(e -> {       
       ......
});

If the button has the focus, pressing Enter will automatically call this handler.如果按钮具有焦点,按Enter 键将自动调用此处理程序。 Otherwise, you can do this in your start method :否则,您可以在start方法中执行此操作:

@Override
public void start(Stage primaryStage) {
      // ...
      Node root = ...;
      setGlobalEventHandler(root);

      Scene scene = new Scene(root, 0, 0);
      primaryStage.setScene(scene);
      primaryStage.show();
}

private void setGlobalEventHandler(Node root) {
    root.addEventHandler(KeyEvent.KEY_PRESSED, ev -> {
        if (ev.getCode() == KeyCode.ENTER) {
           okayButton.fire();
           ev.consume(); 
        }
    });
}

If you have only one button of this kind, you can use the following method instead.如果你只有一个这样的按钮,你可以用下面的方法代替。

okayButton.setDefaultButton(true);

您可以使用绑定动态更改当前聚焦按钮的默认按钮属性

btn.defaultButtonProperty().bind(btn.focusedProperty());

I've had the same problem like mynameisJEFF.我遇到了与 mynameisJEFF 相同的问题。 (I'm using Windows and as I read here: http://mail.openjdk.java.net/pipermail/openjfx-dev/2016-June/019234.html it is the SPACE_BAR and not ENTER, which fires a Button in JavaFX) I didn't want to add a listener to every Button, so I registered a Listener to the root node and asked the scene, which node is focused to fire that one. (我正在使用 Windows,正如我在此处阅读的: http ://mail.openjdk.java.net/pipermail/openjfx-dev/2016-June/019234.html 它是 SPACE_BAR 而不是 ENTER,它会在JavaFX)我不想为每个按钮添加一个监听器,所以我向根节点注册了一个监听器并询问场景,哪个节点专注于触发那个。 Here is my code (it is xtend, but I think it very easy to understand):这是我的代码(它是 xtend,但我认为它很容易理解):

override start(Stage primaryStage) throws Exception {
    val root = FXTable.createRoot
    val mainScene = new Scene(root)
    root.addEventHandler(KeyEvent.KEY_RELEASED, [event|
        if(event.code === KeyCode.ENTER){
            switch(focusedNode : mainScene.focusOwnerProperty.get){
                Button:{
                    focusedNode.fire
                    event.consume
                }
                default:{
                }
            }
        }
    ])
    primaryStage.scene = mainScene
    primaryStage.show
    primaryStage.maximized = true
}

There is a much more simple a standard way to do that using setOnKeyPressed使用setOnKeyPressed有一种更简单的标准方法来做到这一点

okayButton.setOnKeyPressed(event -> {
        if (event.getCode().equals(KeyCode.ENTER)) {
            okayButton.fire();
        }
    }
    );

And don't forget that you should define SetOnAction too, other way it's work but it's doing nothing.并且不要忘记您也应该定义 SetOnAction ,否则它可以工作但它什么都不做。

okayButton.setOnAction(event -> {
        // Do what ever you want to your button do. Like :
        System.Out.Print("Okay Button Fired (Clicked or Pressed");
    }
    );

This should work:这应该有效:

okayButton.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            if(evt.getKeyCode() == KeyEvent.VK_ENTER){
               System.out.print("Your function call or code can go here");
            }
        }
    });  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM