JavaFx“锁定”Scrollpane位置

时间:2014-09-12 15:02:33

标签: java javafx

我的应用程序中有一个包含大量内容的滚动窗口。

该应用还有一个全屏切换按钮。

当我在全屏模式和窗口模式之间切换时,我正试图让ScrollPane的内容不会移动

我首先尝试进行一些计算来翻译视口..但这感觉有点矫枉过正。

所以我认为我应该以某种方式“锁定”ScrollPane,同时我切换到全屏/窗口,并再次“解锁”。

有什么想法吗?

编辑:添加了问题的示例。

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class PositionExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        Pane pane = new Pane();
        pane.setMinSize(3000, 3000);
        pane.setMaxSize(3000, 3000);
        pane.setPrefSize(3000, 3000);

        Label label = new Label("Double-click anywhere, and due to \nScrollPane's panning \"side-effect\" ,\nI don't move a bit (though I flicker once).\nTHIS is what I am after.\n\nHowever, press F11, and I will move!");
        label.setMouseTransparent(true);
        pane.getChildren().add(label);

        ScrollPane scroller = new ScrollPane(pane);

        pane.setOnMousePressed(e -> {
            if (e.getClickCount() == 2) {
                primaryStage.setFullScreen(!primaryStage.isFullScreen());
            }
        });

        // This is a curious side-effect! If I set the scroller pannable,
        // and do not consume the pressed-event, double-clicking 
        // positions the scrollpane so that the label does not move at all.
        // But how do I achieve this simple effect without setPannable?
        scroller.setPannable(true);

        Platform.runLater(() -> {
            label.setLayoutX(pane.getWidth() / 2 - label.getWidth() / 2);
            label.setLayoutY(pane.getHeight() / 2 - label.getHeight() / 2);
            scroller.setVvalue(0.5);
            scroller.setHvalue(0.5);
        });

        Scene scene = new Scene(scroller, 400, 400);
        scene.setOnKeyPressed(e -> {
            if (KeyCode.F11.equals(e.getCode())) {
                primaryStage.setFullScreen(!primaryStage.isFullScreen());
            }
        });
        primaryStage.setScene(scene);
        primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
        primaryStage.show();
    }

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

0 个答案:

没有答案