Is there a way to prevent a transparent Stage + Scene from showing up the first few milliseconds?

时间:2015-10-20 12:35:29

标签: java javafx-8

I already searched on google and here for a few hours but didn't find any answer for this problem. I want to create an application, which will only contain icons. It doesn't have any purpose. It's just for me to practice JavaFX. But I can't figure out why there is a visible window even though I set it to transparent.

As you can see here中提取href参数,这正是我尝试做的事情。这就是它应该像我启动应用程序时的样子(正如我所说,它只是一个没有目的的随机图标......仍在学习,这是我的实践项目)。但由于某种原因,它会加载一个阶段/场景几毫秒,这将产生一次闪烁的"影响。 This是窗口的样子。它只是舞台/场景我是如何设置的(它只是为了让它看起来更清晰而且它是半透明的,因为它太快而且我无法设置它)。创建一个更好的截图:()但没有透明设置。这是我的代码:

public class MainNotification extends Application {

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

@Override
public void start(Stage primaryStage) throws Exception {
    primaryStage = new Stage(StageStyle.TRANSPARENT);
    GridPane pane = new GridPane();
    Scene scene = new Scene(pane);
    scene.setFill(Color.TRANSPARENT);
    primaryStage.setScene(scene);
    TestPloppingWindow.build(primaryStage);
    primaryStage.show();
}

}

public class TestPloppingWindow {

private static boolean isBuild;
private static double posX = 100;
private static double posY = 100;

public static Stage build(Stage stage) {
    if (!isBuild()) {
        GridPane pane = (GridPane) stage.getScene().getRoot();
        ImageView image = new ImageView(
                new Image(MainNotification.class.getResourceAsStream("/Ressource/icon.png"), 100, 100,
                        true, true));
        image.setOnMouseClicked(e -> {
            biggerIcons();
        });
        pane.getChildren().add(image);
        pane.setAlignment(Pos.CENTER);
        stage.setScene(stage.getScene());
        pane.setPrefSize(500, 250);
        setBuild(true);
    }
    return stage;
}

public static void biggerIcons() {
    Stage stage = new Stage(StageStyle.TRANSPARENT);
    stage.setX(posX = posX + 100);
    stage.setY(posY = posY + 100);
    GridPane pane = new GridPane();
    Scene scene = new Scene(pane, Color.TRANSPARENT);
    stage.setScene(scene);
    ImageView view = new ImageView(
            new Image(TestPloppingWindow.class.getResourceAsStream("/Ressource/icon.png")));
    pane.getChildren().add(view);
    stage.show();
}

public static boolean isBuild() {
    return isBuild;
}

private static void setBuild(boolean isBuild) {
    TestPloppingWindow.isBuild = isBuild;
}

}

每当我点击小图标时,会出现一个较大的图标(greaterIcons()),在那里,我不会遇到这个问题。它平滑地消失,没有任何舞台/场景眨眼问题。有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您好先生这段代码stage.setScene(stage.getScene());,我觉得很难理解,也就是这段代码

if (!isBuild()) {

   ....

  setBuild(true);
}
return stage;

这个逻辑对我来说不是很明显(我不明白)。无需返回Stage

  

它将加载一个舞台/场景几毫秒,这将产生“一次性闪烁”效果

由于此stage.setScene(stage.getScene());将其删除,它在您的代码中无效,它只是取消设置Stage的{​​{1}}并重新设置

希望有所帮助