我正在尝试将KeyCode转换为字符串,并且从其他帮助中读取后,.getKeyCode()是将KeyCode转换为String的答案。但是,添加后,错误提示它在.getKeyCode()上“找不到符号”。还有另一个KeyEvent导入,但是如果使用该导入而不是我使用的当前导入,则错误消失,但程序无法运行。

这是我的Controller类:

package keyboardrecorder;

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyEvent;

public class Controller {

    @FXML
    private TextArea consoleKeyTyped;
    private TextArea consoleKeyPressed;
    private TextArea consoleKeyReleased;

    public void outputKeyTyped(KeyEvent event) {
        consoleKeyTyped.setText(consoleKeyTyped.getText() + event.getCharacter());
    }

    public void outputKeyPressed(KeyEvent event) {
         consoleKeyPressed.setText(consoleKeyPressed.getText() + event.getKeyCode());
    }

    public void outputKeyReleased(KeyEvent event) {

    }

}


这是我的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="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="keyboardrecorder.Controller">
   <children>
      <TabPane prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab text="Key Typed">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TextArea fx:id="consoleKeyTyped" editable="false" onKeyTyped="#outputKeyTyped" prefHeight="368.0" prefWidth="600.0" wrapText="true" />
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
          <Tab text="Key Pressed">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TextArea fx:id="consoleKeyPressed" editable="false" onKeyPressed="#outputKeyPressed" prefHeight="368.0" prefWidth="600.0" wrapText="true" />
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
            <Tab text="Key Released">
              <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TextArea fx:id="consoleKeyReleased" editable="false" onKeyReleased="#outputKeyReleased" prefHeight="368.0" prefWidth="600.0" wrapText="true" />
                     </children>
                  </AnchorPane>
              </content>
            </Tab>
        </tabs>
      </TabPane>
   </children>
</AnchorPane>

最佳答案

javafx.scene.input.KeyEvent不是java.awt.event.KeyEvent

您需要getCode()而不是getKeyCode()

09-29 21:42