JavaFX Controller中的setOnKeyPressed

时间:2015-02-18 12:08:00

标签: java user-interface javafx logic

我有一个按钮,我想添加一个功能,用户可以使用他们的键盘来控制程序。例如,如果他们按下键盘上的按钮0,我的计算器上的文本字段应显示0.我到目前为止尝试过,但它无法正常工作。请帮忙。

我将textField设置为disabled,因为如果我不这样做会破坏我的应用程序。

这是我的FXML:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="241.0" prefWidth="362.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"  fx:controller="calculator.FXMLDocumentController">
    <children>
      <GridPane prefHeight="241.0" prefWidth="362.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>


              <Button fx:id="buttonZero" minHeight="30.0" minWidth="50.0" onAction="#digitAction" text="0" GridPane.columnSpan="2147483647" GridPane.rowIndex="4" GridPane.rowSpan="2147483647" />
            <TextField fx:id="textField" alignment="CENTER_RIGHT" GridPane.columnSpan="2147483647" />
            <Button fx:id="decimalButton" minHeight="30.0" minWidth="50.0" text="." GridPane.columnIndex="1" GridPane.rowIndex="4" />
            <Button fx:id="equalsButton" minHeight="30.0" minWidth="50.0" onAction="#equalAction" text="=" GridPane.columnIndex="2" GridPane.rowIndex="4" />
            <Button fx:id="addButton" minHeight="30.0" minWidth="50.0" onAction="#addAction" text="+" GridPane.columnIndex="3" GridPane.rowIndex="4" />
            <Button fx:id="piButton" minHeight="30.0" minWidth="50.0" onAction="#digitAction" text="π" GridPane.columnIndex="4" GridPane.rowIndex="4" />
            <Button fx:id="subtractButton" layoutX="154.0" layoutY="212.0" minHeight="30.0" minWidth="50.0" text="-" GridPane.columnIndex="3" GridPane.rowIndex="3" />
            <Button fx:id="multiplyButton" minHeight="30.0" minWidth="50.0" text="*" GridPane.columnIndex="3" GridPane.rowIndex="2" />
            <Button fx:id="divideButton" minHeight="30.0" minWidth="50.0" text="/" GridPane.columnIndex="3" GridPane.rowIndex="1" />
            <Button fx:id="buttonOne" minHeight="30.0" minWidth="50.0" onAction="#digitAction" text="1" GridPane.rowIndex="3" />
            <Button fx:id="buttonTwo" minHeight="30.0" minWidth="50.0" onAction="#digitAction" text="2" GridPane.columnIndex="1" GridPane.rowIndex="3" />
            <Button fx:id="buttonThree" minHeight="30.0" minWidth="50.0" onAction="#digitAction" text="3" GridPane.columnIndex="2" GridPane.rowIndex="3" />
            <Button fx:id="buttonFour" minHeight="30.0" minWidth="50.0" onAction="#digitAction" text="4" GridPane.rowIndex="2" />
            <Button fx:id="buttonFive" minHeight="30.0" minWidth="50.0" onAction="#digitAction" text="5" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <Button fx:id="buttonSix" minHeight="30.0" minWidth="50.0" onAction="#digitAction" text="6" GridPane.columnIndex="2" GridPane.rowIndex="2" />
            <Button fx:id="buttonSeven" minHeight="30.0" minWidth="50.0" onAction="#digitAction" text="7" GridPane.rowIndex="1" />
            <Button fx:id="buttonEight" minHeight="30.0" minWidth="50.0" onAction="#digitAction" text="8" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <Button fx:id="buttonNine" minHeight="30.0" minWidth="50.0" onAction="#digitAction" text="9" GridPane.columnIndex="2" GridPane.rowIndex="1" />
            <Button fx:id="clearButton" minHeight="30.0" minWidth="50.0" onAction="#clearAction" text="C" GridPane.columnIndex="4" GridPane.rowIndex="2" />
            <Button fx:id="buttonE" minHeight="30.0" minWidth="50.0" onAction="#digitAction" text="e" GridPane.columnIndex="4" GridPane.rowIndex="3" />
         </children>
      </GridPane>
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
    </children>
</AnchorPane>
`

这是我的FXML控制器:

public class FXMLDocumentController implements Initializable{

    @FXML
    TextField textField;


    double currentNumber;




    @FXML
    private void digitAction(ActionEvent event) {



        if (((Button) event.getSource()).getText().equals("π")) {

        String pi = "3.14159265359";
        String oldText = textField.getText();
        String newText = pi + oldText;
        textField.setText(newText);

        }

        else if (((Button) event.getSource()).getText().equals("e")){
        String e = "2.71828182846";
        String oldText = textField.getText();
        String newText = e + oldText;
        textField.setText(newText);
        }
        else {
        String digit = ((Button) event.getSource()).getText();
        String oldText = textField.getText();
        String newText = oldText + digit ;
        textField.setText(newText);
        }
    }
    @FXML
    private void clearAction (ActionEvent event) {
    textField.setText("");

    }

    @FXML
    private void addAction (ActionEvent event) {

        String currentText = textField.getText();
        currentNumber = Double.parseDouble(currentText);
        textField.setText("");
        textField.requestFocus();

    }

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

      // here is my issue Can you see the problem?
      textField.setDisable(true);

        textField.setOnKeyPressed(ke-> {

        if (ke.getCode().equals(KeyCode.DIGIT0))
            {
                // textfield will not be set to x
                textField.setText("0");
            }

            else if (ke.getCode().equals(KeyCode.DIGIT1))
            {

                textField.setText("1");
            }

            else if (ke.getCode().equals(KeyCode.DIGIT2))
            {

                textField.setText("2");
            }
            else if (ke.getCode().equals(KeyCode.DIGIT3))
            {

                textField.setText("3");
            }

        else if (ke.getCode().equals(KeyCode.DIGIT4))
            {

                textField.setText("4");
            }

        else if (ke.getCode().equals(KeyCode.DIGIT5))
            {

                textField.setText("5");
            }

        else if (ke.getCode().equals(KeyCode.DIGIT6))
            {

                textField.setText("6");
            }

        else if (ke.getCode().equals(KeyCode.DIGIT7))
            {

                textField.setText("7");
            }

        else if (ke.getCode().equals(KeyCode.DIGIT8))
            {

                textField.setText("8");
            }

        else if (ke.getCode().equals(KeyCode.DIGIT9))
            {

                textField.setText("9");
            }
        });

    }    

}

0 个答案:

没有答案