如何在 JavaFX 项目中使用 KeyEvent?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27982895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:24:29  来源:igfitidea点击:

How to use KeyEvent in JavaFX project?

javanetbeansjavafx

提问by APro

I have searched for a long time for how to write a KeyEventto allow my Button clicks by ENTER key. Note that I'm using JavaFX and FXMLfiles.

我已经搜索了很长时间,以了解如何编写一个KeyEvent允许我的 Button 点击​​的ENTER key. 请注意,我使用的是 JavaFX 和FXML文件。

The problem is that when set in the onKeyTyped text field in the FXML file, the FXML Files doesn't accept it. It says Handle method not found. It just accept ActionEventmethod so I have tried this code:

问题是在 FXML 文件的 onKeyTyped 文本字段中设置时,FXML 文件不接受它。它说Handle method not found。它只是接受ActionEvent方法,所以我试过这个代码:

 @FXML
 private void key (KeyEvent evt) throws IOException{ 
       if (evt.getCode() == KeyEvent.VK_ENTER){
       String az = text1.getText();
       //c.1
       if(az.contains("1")){ 
          String hh = text11.getText();
          Socket socket = null;
          InetSocketAddress isa = new InetSocketAddress (hh,80);  
       } 
    }
}

So please can anybody help me?

所以请问有人可以帮助我吗?

回答by ItachiUchiha

You have few issues with your code :

您的代码几乎没有问题:

  1. You are using onKeyTypedinstead of onKeyPressed. For more information visit this link

  2. You are most probably using java.awt.event.KeyEvent, which will not work with JavaFX events. Try to use javafx.scene.input.KeyEvent.

    The reason I came to this conclusion is because JavaFX doesn't support KeyEvent.VK_ENTERbut instead have KeyCode.ENTER

  1. 您正在使用onKeyTyped而不是onKeyPressed. 有关更多信息,请访问此链接

  2. 您很可能正在使用java.awt.event.KeyEvent,而JavaFX events. 尝试使用javafx.scene.input.KeyEvent.

    我得出这个结论的原因是因为 JavaFX 不支持KeyEvent.VK_ENTER,而是有KeyCode.ENTER

A concrete example is shown below, you can use the same to convert it into FXML:

一个具体的例子如下所示,您可以使用相同的方法将其转换为 FXML:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class ButtonExample extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane pane = new BorderPane();
        Button button = new Button("Press Me!");
        pane.setCenter(button);
        Scene scene = new Scene(pane, 200, 200);
        primaryStage.setScene(scene);
        primaryStage.show();

        button.setOnKeyPressed(new EventHandler<KeyEvent>() {

            @Override
            public void handle(KeyEvent event) {
                if (event.getCode() == KeyCode.ENTER) {
                    System.out.println("Enter Pressed");
                }
            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}