Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaFX控制器中按下SETONKEY_Java_User Interface_Javafx_Logic - Fatal编程技术网

在JavaFX控制器中按下SETONKEY

在JavaFX控制器中按下SETONKEY,java,user-interface,javafx,logic,Java,User Interface,Javafx,Logic,我有一个按钮,我想添加一个功能,用户可以使用键盘控制程序。例如,如果他们按键盘上的按钮0,我的计算器上的文本字段应该显示0。到目前为止我已经试过了,但不起作用。请帮忙 我将textField设置为disabled,因为如果我不这样做,它会破坏我的应用程序 这是我的FXML: `<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?im

我有一个按钮,我想添加一个功能,用户可以使用键盘控制程序。例如,如果他们按键盘上的按钮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>
`

你能详细解释一下“不工作”吗?@ThomasKilian我认为这是很明显的。当我按下所选按钮时,if语句中各个数字的“settext”不起作用。什么对您不起作用?我在一个应用程序中使用了你的应用程序,一切正常。你有数字0和数字0
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");
            }
        });

    }    

}