简体   繁体   English

透明场景和舞台不适用于 javafx 中的按钮

[英]transparent scene and stage does not work with buttons in javafx

I am trying to make a transparent scene and stage with button but it seems works only with text .我试图用按钮制作一个透明的场景和舞台,但它似乎只适用于 text 。 here is my simple code这是我的简单代码

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class TransparentStage extends Application {

@Override
public void start(Stage stage) {
    stage.initStyle(StageStyle.TRANSPARENT);
    Text text = new Text("Transparent!");
    text.setFont(new Font(40));
    //Button button = new Button("btn");
    VBox box = new VBox();
    box.getChildren().add(text);
    //box.getChildren().add(button);
    final Scene scene = new Scene(box,300, 300);
    scene.setFill(Color.TRANSPARENT);
    stage.setScene(scene);
    stage.show();
}

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

the result结果

but if I uncomment the button the result will be here但如果我取消注释按钮,结果将在这里

It dose not look transparent any more but it is only undercoated.它看起来不再透明,但它只是底漆。 So is Transparent does not work with buttons ?那么透明不适用于按钮吗? and What about if I supposed to add a button ?如果我应该添加一个按钮呢? thank you .谢谢你 。

What's going on这是怎么回事

Text isn't a control.文本不是控件。 A simple JavaFX program which only uses Text, does not load any controls.一个仅使用 Text 的简单 JavaFX 程序,不加载任何控件。 To use controls, JavaFX needs to load the default CSS stylesheet (in Java 8, this is called modena.css ).要使用控件,JavaFX 需要加载默认的 CSS 样式表(在 Java 8 中,这称为modena.css )。 The default CSS stylesheet will set a background color for layout panes, such as the VBox you use in your example.默认的 CSS 样式表将为布局窗格设置背景颜色,例如您在示例中使用的 VBox。

How to fix it如何修复

To prevent a background color for a layout pane, you need to set its background color to null:要防止布局窗格的背景颜色,您需要将其背景颜色设置为 null:

box.setStyle("-fx-background-color: null;");

But why?但为什么?

Now, I know this is weird .现在,我知道这很奇怪。 . . . . but it is just how it is.但事情就是这样。 If you use no controls, layout panes have no background color because CSS is not loaded and applied to the scene graph (possibly for performance reasons).如果不使用控件,布局窗格将没有背景颜色,因为 CSS 未加载并应用于场景图(可能是出于性能原因)。 If you use controls, CSS is loaded and layout panes have a background color.如果您使用控件,则会加载 CSS 并且布局窗格具有背景颜色。

In modena.css, the definitions for this in the .root section are:在modena.css,这在定义.root部分是:

/* A very light grey used for the background of windows.  See also
 * -fx-text-background-color, which should be used as the -fx-text-fill
 * value for text painted on top of backgrounds colored with -fx-background.
 */
-fx-background: derive(-fx-base,26.4%);  

/* A light grey that is the base color for objects.  Instead of using
 * -fx-base directly, the sections in this file will typically use -fx-color.
 */
-fx-base: #ececec;

/***************************************************************************
 *                                                                         *
 * Set the default background color for the scene                          *
 *                                                                         *
 **************************************************************************/

-fx-background-color: -fx-background;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM