JavaFX - 分页:如何隐藏底部(控制)面板并用页面占据其区域?
JavaFX - pagination: how to hide bottom (control) panel and occupy its area with a page?
在某些情况下我需要将分页控件中的换页按钮完全隐藏,但隐藏区域可能已经被一个页面占用,即向下延伸。
在这个带有分页的window示例中,我想隐藏灰色区域并将天空色区域放大到隐藏区域。
我该怎么做?
我试过这个:
package javafxpagination;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TestPagination extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Test pagination");
Pagination pagination = new Pagination();
pagination.setPageCount(4);
pagination.setMaxPageIndicatorCount(4);
pagination.setCurrentPageIndex(0);
pagination.setPageFactory((pageIndex) -> {
Pane pagePane = new Pane();
pagePane.setPrefWidth(600);
pagePane.setPrefHeight(400);
pagePane.setStyle("-fx-background-color: #DDF1F8;"); //sky color
Button button = new Button("Hide/show page buttons");
button.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
HBox paginationHBox = (HBox)pagination.lookup(".control-box");
Node paginationControl = pagination.lookup(".pagination-control");
paginationControl.setStyle("-fx-background-color: gray;");
paginationControl.setVisible(!paginationControl.isVisible()); //switch visibility
paginationControl.setManaged(paginationControl.isVisible());
paginationControl.minHeight(0.0);
paginationControl.prefHeight(0.0);
paginationControl.maxHeight(0.0);
}
});
pagePane.getChildren().add(button);
return pagePane;
});
VBox vBox = new VBox(pagination);
Scene scene = new Scene(vBox, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
但是没有隐藏灰色区域,也没有拉伸天色页面区域
使用 VBox 布局管理器的解决方案
通过将此行添加到您的应用程序,分页控件将扩展以填充可用 space:
VBox.setVgrow(pagination, Priority.ALWAYS);
这回答了您问题的“如何占用”部分:“JavaFX - 分页:如何隐藏底部(控制)面板并用页面占用其区域?”
对于隐藏,您已经完成了,这是基于以下信息:
- JavaFX - setVisible hides the element but doesn't rearrange adjacent nodes
加上查找,
Node paginationControl = pagination.lookup(".pagination-control")
您已经在做的,再加上这些行:
paginationControl.setVisible(!paginationControl.isVisible());
paginationControl.setManaged(paginationControl.isVisible());
不需要将分页控件的 minHeight、prefHeight、maxHeight 设置为 0 的行,我不建议保留它们。
布局管理器的背景知识
布局由布局管理器管理。这些布局管理器有一些默认行为,但有时您需要配置它们或给它们提示以实现您想要的布局行为。
您的 Pagination
控件位于 VBox
中。如果您查看 VBox documentation,它表示:
If a vbox is resized larger than its preferred height, by default it will keep children to their preferred heights, leaving the extra space unused. If an application wishes to have one or more children be allocated that extra space it may optionally set a vgrow constraint on the child.
因此,您正在使用的默认行为是不扩展托管内容以填充额外的可用内容space(这就是您所看到的)。
替代方案——替换分页控件
另一种实现方式是,在隐藏时,将分页控件替换为要显示的面板,而不是隐藏分页控件。
例如,从父布局容器(vbox)中删除分页控件,并在其位置上将要显示的面板直接添加到父布局容器中。
要再次显示分页,您可以执行相反的操作。
这可能是我更喜欢这种工作的解决方案。
对于这样的解决方案,您需要注意一个节点在任何时候都只能是单个父节点的子节点,并在您的实施中考虑到这一点。此外,之前关于布局管理器和在节点上设置 Vgrow 约束的信息仍然适用于此解决方案。
在某些情况下我需要将分页控件中的换页按钮完全隐藏,但隐藏区域可能已经被一个页面占用,即向下延伸。
在这个带有分页的window示例中,我想隐藏灰色区域并将天空色区域放大到隐藏区域。
我该怎么做?
我试过这个:
package javafxpagination;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TestPagination extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Test pagination");
Pagination pagination = new Pagination();
pagination.setPageCount(4);
pagination.setMaxPageIndicatorCount(4);
pagination.setCurrentPageIndex(0);
pagination.setPageFactory((pageIndex) -> {
Pane pagePane = new Pane();
pagePane.setPrefWidth(600);
pagePane.setPrefHeight(400);
pagePane.setStyle("-fx-background-color: #DDF1F8;"); //sky color
Button button = new Button("Hide/show page buttons");
button.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
HBox paginationHBox = (HBox)pagination.lookup(".control-box");
Node paginationControl = pagination.lookup(".pagination-control");
paginationControl.setStyle("-fx-background-color: gray;");
paginationControl.setVisible(!paginationControl.isVisible()); //switch visibility
paginationControl.setManaged(paginationControl.isVisible());
paginationControl.minHeight(0.0);
paginationControl.prefHeight(0.0);
paginationControl.maxHeight(0.0);
}
});
pagePane.getChildren().add(button);
return pagePane;
});
VBox vBox = new VBox(pagination);
Scene scene = new Scene(vBox, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
但是没有隐藏灰色区域,也没有拉伸天色页面区域
使用 VBox 布局管理器的解决方案
通过将此行添加到您的应用程序,分页控件将扩展以填充可用 space:
VBox.setVgrow(pagination, Priority.ALWAYS);
这回答了您问题的“如何占用”部分:“JavaFX - 分页:如何隐藏底部(控制)面板并用页面占用其区域?”
对于隐藏,您已经完成了,这是基于以下信息:
- JavaFX - setVisible hides the element but doesn't rearrange adjacent nodes
加上查找,
Node paginationControl = pagination.lookup(".pagination-control")
您已经在做的,再加上这些行:
paginationControl.setVisible(!paginationControl.isVisible());
paginationControl.setManaged(paginationControl.isVisible());
不需要将分页控件的 minHeight、prefHeight、maxHeight 设置为 0 的行,我不建议保留它们。
布局管理器的背景知识
布局由布局管理器管理。这些布局管理器有一些默认行为,但有时您需要配置它们或给它们提示以实现您想要的布局行为。
您的 Pagination
控件位于 VBox
中。如果您查看 VBox documentation,它表示:
If a vbox is resized larger than its preferred height, by default it will keep children to their preferred heights, leaving the extra space unused. If an application wishes to have one or more children be allocated that extra space it may optionally set a vgrow constraint on the child.
因此,您正在使用的默认行为是不扩展托管内容以填充额外的可用内容space(这就是您所看到的)。
替代方案——替换分页控件
另一种实现方式是,在隐藏时,将分页控件替换为要显示的面板,而不是隐藏分页控件。
例如,从父布局容器(vbox)中删除分页控件,并在其位置上将要显示的面板直接添加到父布局容器中。
要再次显示分页,您可以执行相反的操作。
这可能是我更喜欢这种工作的解决方案。
对于这样的解决方案,您需要注意一个节点在任何时候都只能是单个父节点的子节点,并在您的实施中考虑到这一点。此外,之前关于布局管理器和在节点上设置 Vgrow 约束的信息仍然适用于此解决方案。