gpt4 book ai didi

java - 找不到符号 event.getKeyCode()

转载 作者:行者123 更新时间:2023-11-30 10:27:24 24 4
gpt4 key购买 nike

我正在尝试将 KeyCode 转换为字符串,在阅读其他帮助后,.getKeyCode() 是将 KeyCode 转换为字符串的答案。但是在添加它之后,出现一条错误消息,指出它在 .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()

关于java - 找不到符号 event.getKeyCode(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45321118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com