简体   繁体   English

Java FX Scene Builder如何按下事件键

[英]Java FX Scene Builder How to make event key pressed

I'm new in Java and Java FX and I'm trying to make a panel with buttons using scene builder. 我是Java和Java FX的新手,我正在尝试使用场景生成器制作一个带有按钮的面板。 I want my application to respond only on arrow key pressed. 我希望我的应用程序仅在按下箭头键时做出响应。 I made the following method in my Controller class: 我在Controller类中做了以下方法:

public void keyPressed(KeyEvent key) {
    switch(key.getCode()) {
        ...some code here
    }
} 

After that I selected this method in scene builder, but when I run my application nothing happens when I press an arrow key. 之后,我在场景生成器中选择了此方法,但是当我运行我的应用程序时,按箭头键没有任何反应。 Can somebody help me? 有人可以帮我吗?

Without seeing the rest of your code and FXML it is difficult to tell, here is full example 没有看到其余的代码和FXML,很难说,这里是完整的示例

Possible things you missed 您错过的可能事情

  • Adding keyPress as an action in the FXML 在FXML中添加keyPress作为动作
  • Adding the @FXML annotation to the keyPressed() method 将@FXML批注添加到keyPressed()方法

Code

public class Main extends Application {

    private class Controller {
        @FXML  // <== perhaps you had this missing??
        void keyPressed(KeyEvent event) {
            switch (event.getCode()) {
            case LEFT:
            case KP_LEFT:
                System.out.println("to the left");
                break;
            case RIGHT:
            case KP_RIGHT:
                System.out.println("to the right");
                break;
            default:
                break;
            }
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/foo.fxml"));
        loader.setController(new Controller());
        primaryStage.setScene(new Scene(loader.load()));
        primaryStage.show();
    }

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

FXML XML文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane onKeyPressed="#keyPressed" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
   <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
   </rowConstraints>
   <children>
      <Button mnemonicParsing="false" text="Button" />
   </children>
</GridPane>

KeyCode also allows to compare with a specific key KeyCode还允许与特定键进行比较

@FXML
private void keyPressed(KeyEvent keyEvent)
    if (keyEvent.getCode() == KeyCode.ENTER) {
        // do some actions 
    }
}

You can get all the key code from here . 您可以从此处获取所有关键代码。 Without using switch it's one of the good way. 不使用开关是一种好方法。

KeyCode has a method isArrowKey() , so if you're calling your keyPressed method from your event handler, you could do: KeyCode具有isArrowKey()方法,因此,如果您从事件处理程序中调用keyPressed方法,则可以执行以下操作:

public void keyPressed(KeyEvent key){
    if(key.getCode().isArrowKey()){
        ...some code here
    }
}

If you need to do different things based on which arrow key is pressed, make sure your switch cases are comparing to KeyCode.UP / DOWN / LEFT / RIGHT . 如果需要根据按下的箭头键做不同的事情,请确保将开关盒与KeyCode.UP / DOWN / LEFT / RIGHT进行比较。 If they are, it's likely that either you didn't set the event handler properly or your GUI is hung because of a threading problem. 如果是这样,则可能是由于线程问题导致您未正确设置事件处理程序或GUI挂起。 Post where your handling the event if you need more help. 如果需要更多帮助,请张贴您在哪里处理活动。

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

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