【问题标题】:transparent scene and stage does not work with buttons in javafx透明场景和舞台不适用于 javafx 中的按钮
【发布时间】:2016-03-29 03:49:12
【问题描述】:

我正在尝试用按钮制作一个透明的场景和舞台,但它似乎只适用于文本。 这是我的简单代码

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

但如果我取消注释按钮,结果将是here

它看起来不再透明,但它只是底涂层。 那么透明不适用于按钮吗? 如果我应该添加一个按钮怎么办? 谢谢。

【问题讨论】:

    标签: java javafx


    【解决方案1】:

    发生了什么

    文本不是控件。一个仅使用文本的简单 JavaFX 程序,不加载任何控件。要使用控件,JavaFX 需要加载默认的 CSS 样式表(在 Java 8 中,这称为 modena.css)。默认的 CSS 样式表将为布局窗格设置背景颜色,例如您在示例中使用的 VBox。

    如何解决

    要防止布局窗格的背景颜色,您需要将其背景颜色设置为空:

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

    但是为什么呢?

    现在,我知道这很奇怪。 . .但事实就是如此。如果不使用控件,布局窗格将没有背景颜色,因为 CSS 未加载并应用于场景图(可能出于性能原因)。如果您使用控件,则会加载 CSS,并且布局窗格具有背景颜色。

    在 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;
    

    【讨论】:

      猜你喜欢
      • 2016-03-06
      • 1970-01-01
      • 2015-02-12
      • 2017-12-17
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 2016-08-02
      相关资源
      最近更新 更多