首页 > 解决方案 > 无法在新场景中移动 TextFlow

问题描述

在我的 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
    }
  }

我想知道的是如何在列表视图旁边定位文本?目标是使指令出现在那里。我的计划是使用

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

为此,我们可以很容易地确定在列表中单击了什么。

在此处输入图像描述

标签: javascalajavafxscalafx

解决方案


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

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

此外,我还简化了您的一些代码以使用ScalaFX的属性(与JavaFXgetters 和 setters相对)。

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()
}

推荐阅读