滚动窗口提供UI元素的可滚动视图。
我们使用可滚动面板,当需要显示有限的空间大内容。可滚动窗格视口,其将显示内容的一部分,并且在必要时提供滚动条。
import javafx.application.Application
import javafx.scene.Group
import javafx.scene.Scene
import javafx.scene.control.ScrollPane
import javafx.scene.layout.VBox
import javafx.scene.web.WebEngine
import javafx.scene.web.WebView
import javafx.stage.Stage
public class Main extends Application {
@Override
public void start(Stage stage) {
stage.setWidth(500)
stage.setHeight(500)
Scene scene = new Scene(new Group())
VBox root = new VBox()
final WebView browser = new WebView()
final WebEngine webEngine = browser.getEngine()
ScrollPane scrollPane = new ScrollPane()
scrollPane.setContent(browser)
webEngine.loadContent("<b>yes? this is default content load.</b>")
root.getChildren().addAll(scrollPane)
scene.setRoot(root)
stage.setScene(scene)
stage.show()
}
public static void main(String[] args) {
launch(args)
}
}
上面的代码生成以下结果。
创建滚动窗格
以下代码使用jpg
文件创建一个图像,并将该图像添加到滚动窗格。如果图像较大,滚动窗格将显示滚动条,我们可以使用它来查看隐藏的部分。
Image img = new Image(getClass().getResourceAsStream("yourImage.jpg"))
ScrollPane sp = new ScrollPane()
sp.setContent(new ImageView(img))
可滚动ScrollPane
调用setPannable(true)
方法通过单击并移动鼠标光标来预览图像。
import javafx.application.Application
import javafx.scene.Group
import javafx.scene.Scene
import javafx.scene.control.ScrollPane
import javafx.scene.paint.Color
import javafx.scene.shape.Rectangle
import javafx.stage.Stage
public class Main extends Application {
@Override
public void start(Stage stage) {
Group root = new Group()
Scene scene = new Scene(root, 500, 200)
stage.setScene(scene)// => w w W .Y I I B A I .c O M
Rectangle rect = new Rectangle(200, 200, Color.RED)
ScrollPane s1 = new ScrollPane()
s1.setPannable(true)
s1.setPrefSize(120, 120)
s1.setContent(rect)
root.getChildren().add(s1)
stage.show()
}
public static void main(String[] args) {
launch(args)
}
}
上面的代码生成以下结果。
滚动条策略
我们可以控制何时显示滚动条的策略:
- 总是
- 决不
- 必要时
setHbar策略和setar策略方法分别为水平和垂直滚动条指定滚动条策略。
sp.setHbarPolicy(ScrollBarPolicy.NEVER)
sp.setVbarPolicy(ScrollBarPolicy.ALWAYS)
调整滚动窗格中的组件大小
将setFitToWidth
或setFitToHeight
方法设置为true
以匹配特定维度。
默认情况下,FIT_TO_WIDTH
和FIT_TO_HEIGHT
属性都为false
,可调整大小的内容保持其原始大小。
以下代码显示如何设置JScrollPane
以适合宽度。
import javafx.application.Application
import javafx.scene.Group
import javafx.scene.Scene
import javafx.scene.control.ScrollPane
import javafx.scene.layout.VBox
import javafx.scene.web.WebEngine
import javafx.scene.web.WebView
import javafx.stage.Stage
// from => Ww w . yI i baI.C O m
public class Main extends Application {
@Override
public void start(Stage stage) {
stage.setWidth(500)
stage.setHeight(500)
Scene scene = new Scene(new Group())
VBox root = new VBox()
final WebView browser = new WebView()
final WebEngine webEngine = browser.getEngine()
ScrollPane scrollPane = new ScrollPane()
scrollPane.setFitToWidth(true)
scrollPane.setContent(browser)
webEngine.loadContent("<b>asdf</b>")
root.getChildren().addAll(scrollPane)
scene.setRoot(root)
stage.setScene(scene)
stage.show()
}
public static void main(String[] args) {
launch(args)
}
}
上面的代码生成以下结果。
滚动操作
ScrollPane
类允许检索和设置水平和垂直方向上的内容的当前值,最小值和最大值。
以下代码显示如何处理JScrollPane
垂直值和水平值更改事件。