【问题标题】:JavaFX TextPane doesn't show updated textJavaFX TextPane 不显示更新的文本
【发布时间】:2018-06-18 16:32:38
【问题描述】:

我有一个窗格(以下所有代码),其中包含一个带有 2 个子窗格、VisualPane 和 TextPane 的 HBox。当有人按下 ENTERSPACE 时,我希望程序打印“按下 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 文本框。

【问题讨论】:

标签: java javafx


【解决方案1】:

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 不同吗?

这个答案来自 JavaDocs.

“键类型”事件是更高级别的,通常不依赖于 平台或键盘布局。它们是在 Unicode 时生成的 字符被输入,并且是查找的首选方式 字符输入。在最简单的情况下,会产生一个键类型事件 通过单次按键(例如,“a”)。然而,字符通常是 由一系列按键(例如,SHIFT + 'a')和映射产生 从按键事件到按键类型事件可能是多对一或 多对多。 通常不需要密钥发布来生成 键类型事件,但在某些情况下键类型事件是 在释放键之前不会生成(例如,输入 ASCII 通过 Windows 中的 Alt-Numpad 方法序列)。没有关键类型的事件 为不生成 Unicode 字符的键生成(例如, 操作键、修饰键等)。

【讨论】:

  • 奇怪的是,这行得通。您是否也知道为什么 OnKeyPressed 的行为与 OnKeyReleased 不同?
【解决方案2】:

之所以在控制台中显示一次Text,是因为TextPane构造函数中调用了push(String message)方法。

问题很可能是由于GamePane 不是场景图中的焦点Node

要解决此问题,您可以在将 GamePane 添加到 Scene 之后使 GamePane 请求成为焦点。例如:

public class GameApplication extends Application {

    @Override
    public void start(Stage primaryStage) {        
        GamePane gamePane = new GamePane();        
        Scene scene = new Scene(gamePane);
        gamePane.requestFocus();
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    相关资源
    最近更新 更多