java - 无法在新场景中移动 TextFlow

标签 java scala javafx scalafx

在我的 ScalaFX 项目中,我想为用户创建一个单独的帮助页面,他/她可以在其中单击一个主题,该主题会导致说明出现在可单击主题的旁边。

问题是,当我为帮助页面创建新场景时,无论我做什么,我都无法移动 TextFlow。它位于 ListView 顶部的相同位置。请参阅图片以供引用。

 helpMenu.onAction = (ae: ActionEvent) => {
    val helpStage = new Stage()
    helpStage.setTitle("A wild help stage appears!")
    val helpScene = new Scene(scene.width.get * 0.6, scene.height.get * 0.8) {
      val listView        = new ListView(List.tabulate(3) { n => "Option " + (n + 1) })
      val text1           = new Text("This is fine\n*zips coffee*\nThis is all fine.")
      val textFlow        = new TextFlow(text1)
      /*
       * Nothing below changes where the text appears in the new scene.
       */
      textFlow.layoutX  <== listView.width.get
      textFlow.alignmentInParent_=(Pos.TOP_RIGHT)
      textFlow.alignmentInParent_=(Pos.BASELINE_CENTER)
      text1.alignmentInParent_=(Pos.BASELINE_CENTER)

      listView.prefHeight <== scene.height.get * 0.4
      content.addAll(listView, textFlow)

    }
    helpStage.setScene(helpScene)
    helpStage.show()
    helpScene.listView.onMouseClicked = (le: MouseEvent) => {
      val item = helpScene.listView.selectionModel.apply.getSelectedItem
    }
  }

我想知道的是如何在 ListView 旁边定位文本?目标是使说明出现在那里。我的计划是使用

helpScene.listView.onMouseClicked = (le: MouseEvent) => { val item = helpScene.listView.selectionModel.apply.getSelectedItem }

为了这个目的,因为我们可以很容易地确定在列表中点击了什么。

enter image description here

最佳答案

我认为这里的问题是您需要将 ListViewTextFlow 元素添加到一个容器中,该容器将控制它们的布局方式。在下面的代码中,我为此目的使用了一个 HBox,它将两个并排放置。 (在scalafx.scene.layout包中还有其他选项,比如VBoxBorderPaneThis tutorial,对于旧的JavaFX 2 版本,可能有助于您更好地理解该主题。)

在那之后,我有点不清楚你想要实现什么,但我认为你想要做的是根据中的哪个项目在 TextFlow 中显示不同的文本ListView 被选中。如果是这种情况,则以下示例通过使用更改选择来触发 TextFlow 内容的更改来解决该问题。 (如果这不是您想要的,您能否详细说明您的要求?)

此外,我还简化了您的一些代码以使用 ScalaFX 的属性(相对于 JavaFXgetter 和 setter ).

onAction = (ae: ActionEvent) =>  {
  val helpStage = new Stage() {

    // Make room for the list view and the text flow.
    width = 400

    // Set the help text to the default.
    val defaultText = "Please make\na selection"
    val helpText = new Text(defaultText)

    // Help for each option in the list view. If a key doesn't match, then the default
    // text is displayed.
    val optionText = Map(
      "Option 1" -> "This is\nsome help for\nOption 1",
      "Option 2" -> "And this is\nsome help for\nOption 2",
      "Option 3" -> "Finally, this is\nsome help for\nOption 3",
    ).withDefaultValue(defaultText)

    // Title the stage and set the scene...
    title = "A wild help stage appears!"
    scene = new Scene {

      val listView = new ListView(List.tabulate(3) { n => "Option " + (n + 1) }) {

        // Change the help text when the selection changes.
        selectionModel().selectedItem.onChange {(_, _, selected) =>
          helpText.text = optionText(selected);
        }
      }

      // Add the help text to the text flow.
      val textFlow = new TextFlow(helpText)

      // Put list view and text flow elements side-by-side.
      content = new HBox(listView, textFlow)
    }
  }
  helpStage.show()
}

关于java - 无法在新场景中移动 TextFlow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55078589/

相关文章:

java - 重新启动 java 线程

java - 将 CodeStar Spring MVC 项目改为 Spring Boot

scala - 使用 Maven 打包并运行 Scala Spark 项目

java - 将 JavaFX jmod 添加到 java --list-modules

当 .fxml 不在场景中时,JavaFX 按钮消失

java - couchbase lite 数据库名称无效

java - 如何使用带有 Gson 的 retrofit2 将空字符串视为 null

scala - Spark : Using mapPartition with Scala

scala - 无法使用 Scala 在 Apache Spark 中执行用户定义的函数

JavaFX - 强制 GridPane 的 child 适应单元格的大小