【发布时间】:2018-06-18 16:32:38
【问题描述】:
我有一个窗格(以下所有代码),其中包含一个带有 2 个子窗格、VisualPane 和 TextPane 的 HBox。当有人按下 ENTER 或 SPACE 时,我希望程序打印“按下 Enter”。和“按下空间”。分别。当按下ESCAPE 时,我希望应用程序关闭。
TextPane 由一个带有私有 TextArea 的窗格组成。使用 push(String message) 方法清除 TextArea,然后填充新消息。
但是,当您按 Enter 或 Space 时,没有任何反应。无论您按多少次按钮,“文本”都会在控制台中打印一次。但是,无论何时按下 Escape,程序仍然会退出。
为什么push() 方法会“冻结”TextArea?
代码:
public class GamePane extends Pane {
private VBox content;
private HBox textBox;
private VisualPane visualPane;
private TextPane textPane;
public GamePane(){
initialize();
}
private void initialize(){
this.content = new VBox();
getChildren().add(content);
this.textBox = new HBox();
this.visualPane = new VisualPane();
this.textPane = new TextPane();
content.getChildren().add(visualPane);
content.getChildren().add(textBox);
textBox.getChildren().add(textPane);
this.setOnKeyPressed(e -> {
switch (e.getCode()){
case ENTER:
this.textPane.push("Button pressed.");
break;
case SPACE:
this.textPane.push("Button pressed.");
break;
case ESCAPE:
Platform.exit();
break;
}
});
}
}
public class TextPane extends Pane {
private TextArea textArea;
public TextPane(){
initialize();
push("Text");
}
private void initialize(){
this.textArea = new TextArea();
textArea.setMinWidth(Constants.width);
textArea.setMinHeight(Constants.height * 4 / 11);
textArea.setEditable(false);
textArea.setStyle("-fx-font-size: 3em;");
getChildren().add(textArea);
}
public void push(String message){
textArea.clear();
textArea.setText(message);
System.out.println("Text");
}
}
PS:
GamePane 有一个用于添加内容的 HBox 文本框。
【问题讨论】:
-
请创建并发布minimal reproducible example。 (否则基本上大家都被逼猜了。)