英文:
How to set pop up background Transparent Java Fx
问题
我正在尝试在我的JavaFx应用程序中显示一个成功消息的gif。
public static void successGif() {
int size = 400;
ImageView splash = new ImageView(new Image("file:src/main/resources/img/success3.gif"));
splash.setStyle("-fx-background-color: transparent;");
splash.setFitWidth(size);
splash.setFitHeight(size);
splash.setPickOnBounds(true);
Pane splashLayout = new Pane();
splashLayout.getChildren().add(splash);
final Stage initStage = new Stage();
Scene successScene = new Scene(splashLayout, size, size);
successScene.setFill(Color.TRANSPARENT);
initStage.initStyle(StageStyle.TRANSPARENT);
initStage.setWidth(size);
initStage.setHeight(size);
initStage.setScene(successScene);
initStage.show();
new Thread(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(initStage::close);
}).start();
}
以上函数可以成功地创建一个带有gif的弹出窗口。目前,我尝试通过应用程序中的按钮调用此函数,但弹出窗口的背景仍然是白色的。但当我在应用程序的start()函数中调用相同的函数时,它按预期工作。我该如何解决这个问题?
我需要在我的应用程序中多次调用此函数。
如何使背景透明。
以下是gif图像,如果需要尝试,谢谢。
英文:
I am trying to show a success message gif from my javaFx application.
public static void successGif() {
int size = 400;
ImageView splash = new ImageView(new Image("file:src/main/resources/img/success3.gif"));
splash.setStyle("-fx-background-color: transparent;");
splash.setFitWidth(size);
splash.setFitHeight(size);
splash.setPickOnBounds(true);
Pane splashLayout = new Pane();
splashLayout.getChildren().add(splash);
final Stage initStage = new Stage();
Scene successScene = new Scene(splashLayout, size, size);
successScene.setFill(Color.TRANSPARENT);
initStage.initStyle(StageStyle.TRANSPARENT);
initStage.setWidth(size);
initStage.setHeight(size);
initStage.setScene(successScene);
initStage.show();
new Thread(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(initStage::close);
}).start();
}
The above function can create a popup with a gif successfully. for now, I tried calling this function through a button in my application and the background of the popup is still white. But when I tested the same function by calling it in the start() function of the application, it works as expected. how can I fix this issue?
I need to call this function inside my application on many occasions.
How to make the background transparent.
The following is the gif in case you need to try, Thanks.
答案1
得分: 3
你可以使用 javafx.scene.Group
,而不是直接将闪屏布局添加到场景中,
你可以将你的successGif()
方法更改为:
public static Stage successGif() {
int size = 600;
ImageView splash = new ImageView(new
Image("file:src/main/resources/img/success3.gif"));
splash.setStyle("-fx-background-color: transparent;");
splash.setFitWidth(size);
splash.setFitHeight(size);
splash.setPickOnBounds(true);
Pane splashLayout = new Pane();
splashLayout.getChildren().add(splash);
final Stage initStage = new Stage();
Group group = new Group();
group.getChildren().add(splashLayout);
// group.setStyle("-fx-background-color: transparent");
Scene successScene = new Scene(group, size, size);
successScene.setFill(Color.TRANSPARENT);
initStage.initStyle(StageStyle.TRANSPARENT);
initStage.setWidth(size);
initStage.setHeight(size);
initStage.setScene(successScene);
initStage.setAlwaysOnTop(true);
initStage.show();
return initStage;
}
然后,如果你想要Stage
对象,只需要从该方法中获取返回的对象即可。
英文:
You can use a javafx.scene.Group
instead of adding splash layout directly to the scene,
You can change your void method successGif()
to,
public static Stage successGif() {
int size = 600;
ImageView splash = new ImageView(new
Image("file:src/main/resources/img/success3.gif"));
splash.setStyle("-fx-background-color: transparent;");
splash.setFitWidth(size);
splash.setFitHeight(size);
splash.setPickOnBounds(true);
Pane splashLayout = new Pane();
splashLayout.getChildren().add(splash);
final Stage initStage = new Stage();
Group group = new Group();
group.getChildren().add(splashLayout);
// group.setStyle("-fx-background-color: transparent");
Scene successScene = new Scene(group, size, size);
successScene.setFill(Color.TRANSPARENT);
initStage.initStyle(StageStyle.TRANSPARENT);
initStage.setWidth(size);
initStage.setHeight(size);
initStage.setScene(successScene);
initStage.setAlwaysOnTop(true);
initStage.show();
return initStage;
}
and then if you want the Stage object, then you can just get the returned object from the method
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论