ScalaFX:如何拉伸ScrollPane以适合其父级

时间:2015-11-15 11:26:43

标签: javafx scalafx

我使用Border Pane作为布局。底部子项有一个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)
    }
  }
}

它在运行时以下列方式查找:

During Runtime

我可以在不手动设置ScrollPane宽度的情况下实现此目的吗?

1 个答案:

答案 0 :(得分:1)

在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之前删除了您添加到底部的新窗格

<强> 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
      }
    }
  }
}

<强> 截图

enter image description here