如何使JavaFX场景中的内容透明? [英] How do I make content within a JavaFX Scene transparent?

查看:871
本文介绍了如何使JavaFX场景中的内容透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序是我实际尝试实现的一个例子。

The program below is an example of what I'm actually trying to achieve.

我要做的是在JavaFX中重新创建上面的图片。但是我遇到了困难,因为当我将舞台的内容设置为透明时,它实际上并不透明,它仍然是白色的。

I'm trying to do is recreate the picture above in JavaFX. However I am having difficulties because when I set the content of my stage to transparent it doesn't actually go transparent it still remains white.

@Override
public void start(Stage stage) {        
    try {
        BorderPane root = new BorderPane();

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

        root.setStyle("-fx-background-color: rgba(0,0,0,0);");

        scene.setFill(Color.TRANSPARENT);

        stage.setScene(scene);
        stage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

此代码可以看到舞台。我还将内容设置为透明,并将根窗格的默认样式更改为透明。

The stage is visible from this code. I also set the content to transparent and changed the default style of the root pane to transparent.

我不明白为什么这不起作用,我正在设置内容为透明但背景仍然不透明。

I don't understand why this doesn't work, I'm setting the content to transparent but the background is still not transparent.

我发布的代码的结果显示了这一点,因为你可以看到它不透明。

The result from the code I posted shows this, as you can see it's not transparent.

推荐答案

这个问题是差不多重复:

This question is almost a duplicate of:

  • how to make transparent scene and stage in javafx?

不同之处在于它还要求保留原生窗口装饰,这是 StageStyle.TRANSPARENT

The difference being that it is also asking to retain native window decorations, which is not possible with StageStyle.TRANSPARENT.

如果仔细观察问题中提供的示例窗口,装饰区域会略有不同(例如,一个包含左上角的舞台图标而另一个不包括) 。我的猜测是问题中描绘的透明窗口实际上根本没有使用OS窗口装饰,而是呈现自己的装饰,看起来很像OS窗口装饰(虽然这只是猜测)。

If you look closely at the sample windows provided in the question, the decoration areas differ slightly (e.g. one includes a stage icon in the upper left and the other does not). My guess is that the transparent window pictured in the question isn't actually using the OS window decorations at all, but is instead rendering its own decorations which look much like the OS window decorations (that is just a guess though).

无论如何,要实现与JavaFX类似的效果,必须使用JavaFX来渲染窗口装饰而不是OS窗口管理器。为此,请参阅以下问题:

Anyway, to achieve a similar effect with JavaFX, it is necessary that JavaFX be used to render the window decorations rather than the OS window manager. To do that, refer to the following question:

  • JavaFX entirely customized windows?

特别要看一下Undecorator库:

In particular take a look at the Undecorator library:

  • https://github.com/in-sideFX/UndecoratorBis

你永远无法将窗户装饰完全搞定匹配OS Window Manager装饰,但您可以创建看起来不同并提供类似功能的东西。如果你正在使用它,你可能能够非常接近地实现与OS窗口装饰相匹配的东西,类似于你问题中的窗口截图。虽然,如果是我,我会选择一个不同的外观,用户仍然很干净,容易理解,类似于Undecorator提供的默认外观。

You will never be able to get the window decorations to exactly match the OS Window manager decorations, but you can create something that looks different and provides similar functionality. If you work at it, you might be able to achieve something that matches the OS window decorations pretty closely, similar to the window screenshot in your question. Though, if it were me, I would just go for a different look which was still clean and easily comprehensible by the user, similar to the default look provided by Undecorator.


您是否认为可以通过创建ImageView并使用某种舞台监听器来实现这一点,因此当移动舞台时,imageview会显示应用程序背后的新图像?或者这是否过于复杂?

Do you think this could be pulled off by creating an ImageView and using some sort of stage listener so when the stage is moved the imageview displays a new image of what's behind the application? Or is that overly complicating it?

对imageView方法的背景捕获是一种合理的替代方法。

The background capture to imageView approach is a reasonable alternate approach.

如果您确实想使用imageView捕获方法,可以使用以下答案中的一些想法:

If you do want to use the imageView capture approach, you can use some of the ideas from the following answer:

  • JavaFX effect on background

请注意,上面链接的imageView捕获类型答案将无法完全达到您的要求,您仍需要做一些额外的自定义工作,以便在移动舞台时获得所需的效果。当您的窗口下的桌面或窗口发生变化时。可能你可能需要求助于JavaFX以外的逻辑来完全实现你的目标,并且讨论如何做到这一点超出了我在这里准备(或能够)讨论的范围。

Note that the imageView capture type answer linked above won't completely accomplish what you wish and you still need to do some additional custom work to get exactly the effect you need as you move the stage around or as the desktop or windows under your window change. Possibly you may need to resort to logic outside of JavaFX to fully realize your goal, and discussion of how to do that is outside the scope of what I would be prepared (or able) to discuss here.

无论你做什么都很复杂,我在undecorator或imageView捕获方法之间并没有真正的建议。

It's complicated no matter what you do, I don't really have a recommendation between the undecorator or the imageView capture approach.

也许如果你混合JavaFX和Swing / AWT,你可以通过使用 JFXPanel 和AWT PERPIXEL_TRANSPARENT 功能。我没有尝试过这个组合,但AWT的每个像素设置都在此oracle示例,如如何实现每像素半透明度

Maybe if you mix JavaFX and Swing/AWT you may be able to do this by using a JFXPanel and the AWT PERPIXEL_TRANSPARENT feature. I haven't tried this combo, but per pixel settings for AWT are demonstrated in this oracle sample as discussed in How to Implement Per-Pixel Translucency.

这篇关于如何使JavaFX场景中的内容透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆