滚动javafx后保持元素X位置固定
keep elements X position fixed after scrolling javafx
即使在 scrollPane 中滚动后,如何使元素始终可见,这意味着在固定水平 scrolling.its 位置后节点应该不动。我试过 this 但它对我的情况不起作用,元素仍然随着滚动而移动,我正在添加一个包含所有元素的 scrollPane 到 AnchorPane。
只需使用堆栈窗格并在 ScrollPane 之后添加固定元素。
根据您不想允许的滚动方式,只需注释掉我添加的 "scrollProperty" 侦听器 - 如果您希望元素完全固定 - 将它们都删除:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
root.setAlignment(Pos.TOP_LEFT);
ScrollPane scrollPane = new ScrollPane();
Pane pane = new Pane();
pane.setMinHeight(1000);
pane.setMinWidth(1000);
scrollPane.setContent(pane);
root.getChildren().add(scrollPane);
Label fixed = new Label("Fixed");
root.getChildren().add(fixed);
// Allow vertical scrolling of fixed element:
scrollPane.hvalueProperty().addListener( (observable, oldValue, newValue) -> {
double xTranslate = newValue.doubleValue() * (scrollPane.getViewportBounds().getWidth() - fixed.getWidth());
fixed.translateXProperty().setValue(-xTranslate);
});
// Allow horizontal scrolling of fixed element:
scrollPane.vvalueProperty().addListener( (observable, oldValue, newValue) -> {
double yTranslate = newValue.doubleValue() * (scrollPane.getViewportBounds().getHeight() - fixed.getWidth());
fixed.translateYProperty().setValue(-yTranslate);
});
Scene scene = new Scene(root, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
}
即使在 scrollPane 中滚动后,如何使元素始终可见,这意味着在固定水平 scrolling.its 位置后节点应该不动。我试过 this 但它对我的情况不起作用,元素仍然随着滚动而移动,我正在添加一个包含所有元素的 scrollPane 到 AnchorPane。
只需使用堆栈窗格并在 ScrollPane 之后添加固定元素。 根据您不想允许的滚动方式,只需注释掉我添加的 "scrollProperty" 侦听器 - 如果您希望元素完全固定 - 将它们都删除:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
root.setAlignment(Pos.TOP_LEFT);
ScrollPane scrollPane = new ScrollPane();
Pane pane = new Pane();
pane.setMinHeight(1000);
pane.setMinWidth(1000);
scrollPane.setContent(pane);
root.getChildren().add(scrollPane);
Label fixed = new Label("Fixed");
root.getChildren().add(fixed);
// Allow vertical scrolling of fixed element:
scrollPane.hvalueProperty().addListener( (observable, oldValue, newValue) -> {
double xTranslate = newValue.doubleValue() * (scrollPane.getViewportBounds().getWidth() - fixed.getWidth());
fixed.translateXProperty().setValue(-xTranslate);
});
// Allow horizontal scrolling of fixed element:
scrollPane.vvalueProperty().addListener( (observable, oldValue, newValue) -> {
double yTranslate = newValue.doubleValue() * (scrollPane.getViewportBounds().getHeight() - fixed.getWidth());
fixed.translateYProperty().setValue(-yTranslate);
});
Scene scene = new Scene(root, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
}