如果HBox不会自动调整大小,可以将其放置在一个ScrollPane中,然后调整ScrollPane的大小。以下是一个示例代码:
import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.scene.control._
import scalafx.scene.layout._
object ResizingExample extends JFXApp {
stage = new JFXApp.PrimaryStage {
title.value = "Resizing Example"
width = 300
height = 200
scene = new Scene {
root = new ScrollPane {
content = new HBox {
children = Seq(
new Button("Button 1"),
new Button("Button 2"),
new Button("Button 3"),
new Button("Button 4")
)
}
}
}
}
}
在此示例代码中,我们将HBox放置在ScrollPane中,并为ScrollPane设置了默认大小。这将使HBox自动适应其父组件的大小,从而实现自适应调整大小。