ScalaFX:如何拉伸 ScrollPane 以适应其 parent

ScalaFX : How to stretch ScrollPane to fit its parent

我使用Border Pane作为布局。底部 child 有一个 ScrollPane,它应该使用 Border Pane 的完整宽度(拉伸)——无论其内容如何。

val scrollPane = new ScrollPane() {
hbarPolicy = ScrollBarPolicy.ALWAYS
content = new TextFlow() {
  children.add(new Text("Hello World"))
}
}

stage = new PrimaryStage {
title = "ScalaFX Hello World"
width = 1024
height = 768
scene = new Scene {
  content = new BorderPane() {
    center = Label("This is my center text")
    bottom = new Pane {
      children.add(scrollPane)
    }
  }
}

它在运行时看起来是这样的:

我是否可以在不手动设置 ScrollPane 宽度的情况下实现这一点?

在 ScalaFX 中,除非传递父级,否则 Scene 将使用空组实例化。

class Scene(override val delegate: jfxs.Scene = new jfxs.Scene(new jfxs.Group())) 

所以,不是设置 content,而是设置场景的 root

scene = new Scene {
    root = new BorderPane() {
    center = Label("This is my center text")
    bottom = scrollPane
  }
}

您一定已经注意到,在添加 ScrollPane 之前,我什至删除了您要添加到底部的 new Pane

MCVE

import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.scene.control.ScrollPane.ScrollBarPolicy
import scalafx.scene.control.{Label, ScrollPane}
import scalafx.scene.layout.BorderPane
import scalafx.scene.text.{Text, TextFlow}

object Main extends JFXApp {

  val scrollPane = new ScrollPane() {
    hbarPolicy = ScrollBarPolicy.ALWAYS
    content = new TextFlow() {
      children.add(new Text("Hello World"))
    }
  }
  stage = new JFXApp.PrimaryStage {
    title.value = "Hello Stage"
    width = 200
    height = 150
    scene = new Scene {
        root = new BorderPane() {
        center = Label("This is my center text")
        bottom = scrollPane
      }
    }
  }
}

截图