events - 失去对我的事件处理函数中所有内容的引用
问题描述
我的想法:我想要一个用户可以输入命令的文本字段。他所有的输入都应该保存在一个列表中,作为一种历史,用户可以用他的箭头键滚动浏览它们。
我的问题:我多次调试我的代码,命令被适当地添加到历史记录(textarea)和列表中,但在我的事件处理程序中,对我的 textarea、textfield 和列表的所有引用始终为空。
我的代码:
public class Controller extends Application implements Initializable {
@FXML
private TextField commandInput;
@FXML
private TextArea commandHistory;
private LinkedList<String> commandList = new LinkedList<>();
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("layout.fxml"));
primaryStage.setTitle("Airport");
Scene scene = new Scene(root, 700, 275);
primaryStage.setScene(scene);
primaryStage.show();
scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (!commandList.isEmpty()) {
switch (event.getCode()) {
case UP:
commandInput.setText(commandList.getFirst());
commandList.addLast(commandList.getFirst());
commandList.removeFirst();
break;
case DOWN:
commandInput.setText(commandList.getLast());
commandList.addFirst(commandList.getLast());
commandList.removeLast();
break;
}
}
System.out.println("Pressed: " + event.getCode());
});
}
private boolean parseString(String input) {
//TODO: Write Parser
return true;
}
public void enterPressed(ActionEvent actionEvent) {
String input = commandInput.getText();
if (parseString(input)) {
addCommandToHistory(input);
commandList.add(input);
System.out.println(commandList.getFirst());
commandInput.setText("");
} else {
addCommandToHistory("input is no command");
}
解决方案
推荐阅读
- javascript - 根据键而不是值从数组中删除对象?
- qt - qml WebSocket如何使用Sec-Websocket-Key
- jquery - Jquery Selector [name="value"] 选择器不起作用
- python - 如何从图像中提取模糊的线条(木纹)
- xslt - 如何在 XSL-FO 中的第一次匹配之前插入表头?
- python - Plotly:如何将分类变量插入平行坐标图中?
- sql - 在 postgresql 上还原数据时,触发器(用于生成的代码)会产生错误
- c++ - 如何使用 get/set 方法在子对象中更改其父类成员变量的值?
- prometheus - Alertmanager 通知尝试失败,稍后将重试用户名和密码不被接受
- python-3.x - 在 sqlite 数据库中仅从日期开始获取月份