如何在Java FXML中创建KeyEvent?

时间:2017-04-05 21:47:19

标签: javafx fxml onkeypress key-events

我创建了这个小项目来测试关键事件。但是当我按下按键时,它的表现并不像我想要的那样。实际上我需要计算器项目的关键事件。我创建了一个Calculator项目,除了鼠标点击之外,我想添加一个功能,可以从键盘输入数字或运算符。任何人都可以检查这个并帮助它更有用吗?

public class FXMLDocumentController implements Initializable{

@FXML
private Label label;

@FXML
private Button backSpace;

@FXML
private Button spaceBar;

@FXML
private Button enter;

@FXML
void typedBS(KeyEvent event) {
    if (event.getCode() == KeyCode.BACK_SPACE) {
        label.setText(event.getText() + " typed.");
    }
}

@FXML
void typedE(KeyEvent event) {
    if (event.getCode()==KeyCode.ENTER) {
        label.setText(event.getText() + " typed");
    }
}

@FXML
void typedSB(KeyEvent event) {
    if (event.getCode()==KeyCode.SPACE) {
        label.setText(event.getText()+" typed");
    }
}

@FXML
void PressBackSpace(KeyEvent event) {
    if (event.getCode() == KeyCode.BACK_SPACE) {
        label.setText("You pressed Back Space key!");
    }
}

@FXML
void clickBackSpace(ActionEvent event) {
    label.setText("You clicked Back Space key!");
}

@FXML
void clickEnter(ActionEvent event) {
    label.setText("You clicked Enter key!");
}

@FXML
void clickSpaceBar(ActionEvent event) {
    label.setText("You clicked SpaceBar key!");
}

@FXML
void pressEnter(KeyEvent event) {
    if (event.getCode() == KeyCode.ENTER) {
        label.setText("You pressed Enter key!");
    }
}

@FXML
void pressSpaceBar(KeyEvent event) {
    if (event.getCode() == KeyCode.SPACE) {
        label.setText("You pressed SpaceBar key!");
    }
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}

}

2 个答案:

答案 0 :(得分:0)

在同一件事中定义事件和处理。 尝试这样的事情: 如果你使用边框作为例子

borderPane.setOnKeyPressed(this::handleKeyPress);

private void handleKeyPress(KeyEvent event) {
    if (event.getCode() == KeyCode.BACK_SPACE) {
        label.setText(event.getText() + " typed.");
    }
    if (event.getCode() == KeyCode.SPACE) {
        label.setText("You pressed SpaceBar key!");
    }
     //TODO
}

希望有所帮助

答案 1 :(得分:0)

  

如果你看一下这个FXML文件,你可以看到我的根节点(The AnchorPane)有一个onKeyPressed和onKeyReleased。

<AnchorPane id="AnchorPane" onKeyPressed="#handleOnKeyPressed" onKeyReleased="#handleOnKeyReleased" prefHeight="650.0" prefWidth="855.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="atmfx.FXMLDocumentController">
   <children>
      <BorderPane layoutX="263.0" layoutY="94.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <left>
            <AnchorPane fx:id="apLeftDisplay" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
               <children>
  

在Controller中我处理这两个KeyEvent的方法是

@FXML
private void handleOnKeyReleased(KeyEvent event)
{
    System.out.println();
    Button tempButton = buttons.get(event.getText());
    System.out.println("Released key text: " + event.getText());
    System.out.println("Released key code: " + event.getCode());

    if (tempButton != null) {
        tempButton.disarm();
        tempButton.setStyle("");
    }
    else if (event.getCode().equals(KeyCode.ENTER)) {
        tempButton = buttons.get("enter");
        tempButton.disarm();
        tempButton.setStyle("");
    }
    else if (event.getCode().equals(KeyCode.BACK_SPACE)) {
        tempButton = buttons.get("backspace");
        tempButton.disarm();
        tempButton.setStyle("");
    }
    else if (event.getCode().equals(KeyCode.SPACE)) {
        tempButton = buttons.get("space");
        tempButton.disarm();
        tempButton.setStyle("");
    }
}

@FXML
private void handleOnKeyPressed(KeyEvent event)
{
    Button tempButton = buttons.get(event.getText());
    System.out.println("Pressed key text: " + event.getText());
    System.out.println("Pressed key code: " + event.getCode());
    if (tempButton != null) {
        tempButton.arm();
        tempButton.setStyle("-fx-background-color: blue");
    }
    else if (event.getCode().equals(KeyCode.ENTER)) {
        tempButton = buttons.get("enter");
        tempButton.arm();
        tempButton.setStyle("-fx-background-color: blue");
    }
    else if (event.getCode().equals(KeyCode.BACK_SPACE)) {
        tempButton = buttons.get("backspace");
        tempButton.arm();
        tempButton.setStyle("-fx-background-color: blue");
    }
    else if (event.getCode().equals(KeyCode.SPACE)) {
        tempButton = buttons.get("space");
        tempButton.arm();
        tempButton.setStyle("-fx-background-color: blue");
    }
}
  

在这段代码中,我将所有按钮添加到HasMap中。在我的keyevent处理程序中,我能够根据我按下的键盘键来检索按钮。

Map<String, Button> buttons = new HashMap<>();
TheButtonsParentNode.getChildren().stream().filter((tempNode)
            -> (tempNode instanceof Button)).map((
            tempNode) -> (Button) tempNode).forEachOrdered((tempButton) -> {
        buttons.put(tempButton.getText().toLowerCase(), tempButton);//adding button text and the button to hashmap
    });