我正在学习 JavaFx 并构建一个示例应用程序,该应用程序将以可滚动的方式显示餐厅菜单项列表。我发现最好的方法是使用controlsfx 中的GridView 控件,因为即使有大量菜单项,滚动也会很快。这是我试图使其工作的示例代码:
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
VBox root = new VBox();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
ObservableList<GridPane> list = FXCollections.<GridPane>observableArrayList();
GridView<GridPane> gridView = new GridView<>(list);
gridView.setCellFactory(new Callback<GridView<GridPane>, GridCell<GridPane>>() {
public GridCell<GridPane> call(GridView<GridPane> gridView) {
return new GridCell<GridPane>();
}
});
Label lblName1 = new Label("Name");
GridPane grid = new GridPane();
grid.addRow(0, lblName1);
Label lblName2 = new Label("Price");
grid.addRow(1, lblName2);
list.add(grid);
root.getChildren().add(gridView);
primaryStage.show();
} catch(Exception e) { …Run Code Online (Sandbox Code Playgroud)