简体   繁体   English

在JavaFX中,是否可以使场景,舞台和控件都透明?

[英]Is it possible to, in JavaFX, have a scene, stage, and control that are all transparent?

I have a custom control which is 3 simple buttons. 我有一个自定义控件,它是3个简单的按钮。 The control handles operations for the application to which it is tied, but it must reside on an external stage for reasons. 控件处理与其关联的应用程序的操作,但是由于某些原因,它必须驻留在外部平台上。

The thing that is annoying about it is that between all buttons is a white background. 令人讨厌的是,所有按钮之间都是白色背景。

I've tried all of the following to eliminate it: 我尝试了以下所有方法来消除它:

  • Init StageStyle to Transparent 初始化StageStyle为透明
  • Declare Scene with a transparent background : new Scene(Node, X, Y, Color.TRANSPARENT); 用透明背景声明场景: new Scene(Node, X, Y, Color.TRANSPARENT);
  • Declare Scene with a NULL background : new Scene(Node, X, Y, null); 声明背景为NULL的new Scene(Node, X, Y, null);new Scene(Node, X, Y, null);
  • Have the control (which extends an HBox) .setStyle("-fx-background-color : rgba(0, 0, 0, 0);"); 具有控件(扩展了HBox) .setStyle("-fx-background-color : rgba(0, 0, 0, 0);");

All of these have failed to eliminate the appearance of the background. 所有这些都无法消除背景的外观。 I've done this with Windows Forms, but is it possible to do with JavaFX? 我已经使用Windows窗体完成了此操作,但是可以使用JavaFX吗?

You need to set the transparency of 您需要设置透明性

  • the stage 舞台
  • the scene 现场
  • the root class (or in my case the hbox, i prefer root) 根类(或者在我的情况下是hbox,我更喜欢root)

This works for me: 这对我有用:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {

            HBox hbox = new HBox();
            hbox.setSpacing(12);

            Button button1 = new Button("Button 1");
            Button button2 = new Button("Button 2");
            Button button3 = new Button("Button 3");

            hbox.getChildren().addAll(button1, button2, button3);

            Scene scene = new Scene(hbox, Color.TRANSPARENT);

//          scene.getStylesheets().add( getClass().getResource("application.css").toExternalForm());
            scene.getRoot().setStyle("-fx-background-color: transparent");

            primaryStage.initStyle(StageStyle.TRANSPARENT);
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

That's the direct solution. 那是直接的解决方案。 Of course instead of setting the style directly it's better practice to use a stylesheet application.css: 当然,与其直接设置样式,不如使用样式表application.css:

.root {
    -fx-background-color: transparent;
}

Here's a screenshot with the application being over the code: 这是应用程序在代码上方的屏幕截图:

在此处输入图片说明

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

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