让窗口在 JavaFX 中向下滚动

Boz*_*III 0 java netbeans javafx

所以我现在正在尝试学习 Java,因为我知道 JavaScript 和 PHP。我正在使用 JavaFX 在 Netbeans 中工作,我正在尝试创建一个创建 5 个按钮的程序。(这是在创建新的 JavaFX 应用程序时修改 Netbeans 附带的代码。)如果我将场景的 y 参数更改为小于所有按钮的 y,它将不会显示剩余的按钮,它将无法向下滚动。这是我到目前为止。如何启用它向下滚动以便可以看到所有按钮?我知道我可以将场景改回原来的高度,但我想了解使用 JavaFX 滚动。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javax.swing.JScrollPane;

public class JavaFXApplication1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane root = new GridPane();
        Button[] btn=new Button[5];
        for(int i=0;i<5;i++){
            btn[i] = new Button();
            btn[i].setText(i+"");
            GridPane.setRowIndex(btn[i],i); 
            root.getChildren().addAll(btn[i]);
            btn[i].setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("This is a button");
            }
        });

        }

        Scene scene = new Scene(root, 300, 50);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }

}
Run Code Online (Sandbox Code Playgroud)

Key*_*eri 5

使用 ScrollPane 将 root 设置为:

ScrollPane sp = new ScrollPane();
sp.setContent(root);

Scene scene = new Scene(sp, 300, 50);
Run Code Online (Sandbox Code Playgroud)