gpt4 book ai didi

java - 如何使节点在滚动时始终可见?

转载 作者:行者123 更新时间:2023-11-30 02:43:26 26 4
gpt4 key购买 nike

我的内部有一个滚动 Pane 和一个锚定 Pane 。在这个锚定 Pane 内我有一个标签。我需要这个标签在滚动时始终可见。我需要移动,以便用户始终看到它,就好像它没有改变一样。有人可以帮忙吗?

@FXML
private ScrollPane s;


public void initialize(URL location, ResourceBundle resources) {
AnchorPane p = new AnchorPane();
VBox v = new VBox();
p.getChildren().add(v);
s.setContent(p);}

最佳答案

实现此目的的最简单方法不是将 Label 放置在内容中,而是放置在包含 ScrollPane标签:

@Override
public void start(Stage primaryStage) {
Region content = new Region();
content.setPrefSize(2000, 2000);
content.setBackground(new Background(new BackgroundFill(
new LinearGradient(0, 0, 1, 1, true, CycleMethod.NO_CYCLE, new Stop(0, Color.RED), new Stop(1, Color.BLUE)),
CornerRadii.EMPTY,
Insets.EMPTY)));

Label label = new Label("Hello World!");
label.setTextFill(Color.WHITE);
StackPane.setAlignment(label, Pos.TOP_LEFT);
StackPane.setMargin(label, new Insets(10));

ScrollPane scrollPane = new ScrollPane(content);

StackPane root = new StackPane();
root.getChildren().addAll(scrollPane, label);

Scene scene = new Scene(root, 200, 200);

primaryStage.setScene(scene);
primaryStage.show();
}

或者您也可以使用翻译属性调整位置:

@Override
public void start(Stage primaryStage) {
Label label = new Label("Hello World!");
label.setTextFill(Color.WHITE);

Pane content = new Pane(label);
content.setPrefSize(2000, 2000);
content.setBackground(new Background(new BackgroundFill(
new LinearGradient(0, 0, 1, 1, true, CycleMethod.NO_CYCLE, new Stop(0, Color.RED), new Stop(1, Color.BLUE)),
CornerRadii.EMPTY,
Insets.EMPTY)));

ScrollPane scrollPane = new ScrollPane(content);

double targetX = 10;
double targetY = 10;

InvalidationListener listener = o -> {
Bounds viewportBounds = scrollPane.getViewportBounds();
Bounds contentBounds = content.getBoundsInLocal();
Bounds labelBounds = label.getBoundsInLocal();

double factorX = Math.max(contentBounds.getWidth() - viewportBounds.getWidth(), 0);
double factorY = Math.max(contentBounds.getHeight() - viewportBounds.getHeight(), 0);

label.setTranslateX(targetX + scrollPane.getHvalue() * factorX - labelBounds.getMinX());
label.setTranslateY(targetY + scrollPane.getVvalue() * factorY - labelBounds.getMinY());
};

scrollPane.viewportBoundsProperty().addListener(listener);
scrollPane.hvalueProperty().addListener(listener);
scrollPane.vvalueProperty().addListener(listener);
label.boundsInLocalProperty().addListener(listener);

Scene scene = new Scene(scrollPane, 200, 200);

primaryStage.setScene(scene);
primaryStage.show();

listener.invalidated(null);
}

关于java - 如何使节点在滚动时始终可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40945561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com