【问题标题】:How to make JavaFX stage transparent (Stage Only)如何使 JavaFX 舞台透明(仅限舞台)
【发布时间】:2017-12-17 14:28:18
【问题描述】:

下面的例子只适用于文本,但是一旦我在舞台上添加了一个按钮,透明就变得不活跃了

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        stage.initStyle(StageStyle.TRANSPARENT);
        Text text = new Text("!");
        text.setFont(new Font(40));
        VBox box = new VBox();
        Button btn = new Button("Test transparent");
        box.getChildren().addAll(text, btn);
        //if I removed the btn, transparent works as expected. 
        final Scene scene = new Scene(box,300, 250);
        scene.setFill(null);
        stage.setScene(scene);
        stage.show();
    }

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

我正在寻找的只是使舞台透明但显示文本和按钮

【问题讨论】:

    标签: javafx transparent


    【解决方案1】:

    在这种情况下,您的 Button 不是问题。默认情况下 VBox 有灰色背景,所以你的 Stage 是透明的,但 VBox 不是。您必须通过 CSS 文件或内联或代码设置透明背景:

    CSS:

    .your-vbox {
        -fx-background-color: transparent;
    }
    

    内联:

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

    代码:

    box.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));
    

    【讨论】:

    • 非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 2012-04-10
    • 2011-06-07
    • 2014-10-21
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    相关资源
    最近更新 更多