gpt4 book ai didi

JavaFX TextPane 不显示更新的文本

转载 作者:行者123 更新时间:2023-12-02 11:46:23 25 4
gpt4 key购买 nike

我有一个 Pane (下面的所有代码),其中包含一个带有 2 个子 Pane (VisualPane 和 TextPane)的 HBox。当有人按 ENTERSPACE 时,我希望程序打印“Enter Pressed”。和“空间受压”。分别。当按下 ESCAPE 时,我希望应用程序关闭。

TextPane 由带有私有(private) TextArea 的 Pane 组成。使用 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");
}
}

附注:

GamePane 有一个 HBox 文本框,用于放置要添加的内容。

最佳答案

this.setOnkeyPressed(..) 更改为 this.setOnKeyReleased(..)

如果程序启动时您的节点没有焦点,您可能还需要添加 this.requestFocus();

import javafx.application.Platform;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;

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.setOnKeyReleased(e -> {//Change here!
switch (e.getCode()) {

case ENTER:
this.textPane.push("Enter pressed.");
break;

case SPACE:
this.textPane.push("Space pressed.");
break;

case ESCAPE:
System.out.println("escaped pressed");
Platform.exit();
break;
}
});

this.requestFocus();//Change here!
}
}

您还知道为什么 OnKeyPressed 的行为与 OnKeyReleased 不同吗?

This answer is from the JavaDocs.

"Key typed" events are higher-level and generally do not depend on the platform or keyboard layout. They are generated when a Unicode character is entered, and are the preferred way to find out about character input. In the simplest case, a key typed event is produced by a single key press (e.g., 'a'). Often, however, characters are produced by series of key presses (e.g., SHIFT + 'a'), and the mapping from key pressed events to key typed events may be many-to-one or many-to-many. Key releases are not usually necessary to generate a key typed event, but there are some cases where the key typed event is not generated until a key is released (e.g., entering ASCII sequences via the Alt-Numpad method in Windows). No key typed events are generated for keys that don't generate Unicode characters (e.g., action keys, modifier keys, etc.).

关于JavaFX TextPane 不显示更新的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48171598/

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