冻结javafx未修饰的窗口

时间:2018-03-28 15:33:29

标签: javafx

美好的一天。我一直在尝试制作一个透明的,未修饰的可调整大小的窗口,其行为与通常的窗口相同。我遇到了一个问题,当我改变窗口大小时会出现冻结并震动窗口内容和边界。

下面是我的代码显示此问题,右窗格(borderPaneR)没有立即接受更改,在更改窗口大小时冻结。你能说出它是如何解决的。

public class DemoBehaviorProblem extends Application {
double initX;
double windowX;
double windowWidth;

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

@Override
public void start(Stage stage) {
    // Create window's elements
    Pane borderPaneL = new Pane();
    borderPaneL.setStyle("-fx-background-color: rgba(85, 255, 0, 0.5); -fx-cursor: w-resize;");
    Pane borderPaneR = new Pane();
    borderPaneR.setStyle("-fx-background-color: rgba(85, 255, 0, 0.5); -fx-cursor: e-resize;");
    Pane root = new Pane(borderPaneL, borderPaneR);
    Scene scene = new Scene(root, 400, 300, new Color(0, 0, 0, 0.5));

    // Set up side panes
    final int BORDER_WIDTH = 8;
    borderPaneL.setPrefWidth(BORDER_WIDTH);
    borderPaneL.setPrefHeight(scene.getHeight());

    borderPaneR.setLayoutX(scene.getWidth() - BORDER_WIDTH);
    borderPaneR.setPrefWidth(BORDER_WIDTH);
    borderPaneR.setPrefHeight(scene.getHeight());

    //User content
    VBox content = new VBox();
    content.setStyle("-fx-background-color: rgba(255, 230, 0, 0.5); -fx-pref-width: 336; -fx-pref-height: 284;");
    content.setLayoutX(BORDER_WIDTH * 4);
    content.setLayoutY(BORDER_WIDTH);
    root.widthProperty().addListener(((observable, oldValue, newValue) -> content.setPrefWidth(newValue.doubleValue() - 8*BORDER_WIDTH)));
    root.getChildren().add(content);

    // Showing window
    stage.initStyle(StageStyle.TRANSPARENT);
    stage.setScene(scene);
    stage.show();

    // Set up resizable behavior

    // When changed width of stage, change position of right pane
    stage.widthProperty().addListener((observable, oldValue, newValue) -> {
        borderPaneR.setLayoutX(newValue.doubleValue() - BORDER_WIDTH);
    });

    // Moving a left bound
    borderPaneL.addEventFilter(MouseEvent.ANY, event -> {
        if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
            initX = event.getScreenX();
            windowX = stage.getX();
            windowWidth = windowX + stage.getWidth();
        }

        if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
            double deltaX = event.getScreenX() - initX;
            stage.setX(windowX + deltaX);
            stage.setWidth(windowWidth - windowX - deltaX);
        }
    });

    // Moving a right bound
    borderPaneR.addEventFilter(MouseEvent.ANY, event -> {
        if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
            initX = event.getSceneX();
            windowWidth = stage.getWidth();
        }

        if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
            double deltaX = event.getSceneX() - initX;
            stage.setWidth(windowWidth + deltaX);
        }
    });
}

}

0 个答案:

没有答案