在JavaFX中,是否有可能具有透明的场景,阶段和控件? [英] Is it possible to, in JavaFX, have a scene, stage, and control that are all transparent?

查看:1207
本文介绍了在JavaFX中,是否有可能具有透明的场景,阶段和控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义控件,它是3个简单按钮。该控件处理与其绑定的应用程序的操作,但它必须驻留在外部阶段,原因。

I have a custom control which is 3 simple buttons. 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:


  • 初始StageStyle为透明

  • 使用透明背景声明场景:新场景(Node,X,Y,Color.TRANSPARENT);

  • 使用NULL背景声明场景:新场景(节点,X,Y,null);

  • 让控件(扩展HBox) .setStyle( - fx-background-color:rgba(0,0,0,0););

  • Init StageStyle to Transparent
  • Declare Scene with a transparent background : new Scene(Node, X, Y, Color.TRANSPARENT);
  • Declare Scene with a NULL background : new Scene(Node, X, Y, null);
  • Have the control (which extends an HBox) .setStyle("-fx-background-color : rgba(0, 0, 0, 0);");

所有这些都无法消除背景的外观。
我用Windows Forms做过这个,但可以用JavaFX吗?

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?

推荐答案

你需要设置透明度


  • 舞台

  • 场景

  • 根类(或者在我的情况下是hbox,我更喜欢root)

这对我有用:

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);
    }
}

这是直接解决方案。当然,不是直接设置样式,最好使用样式表application.css:

That's the direct solution. Of course instead of setting the style directly it's better practice to use a stylesheet application.css:

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

以下是应用程序覆盖代码的屏幕截图:

Here's a screenshot with the application being over the code:

这篇关于在JavaFX中,是否有可能具有透明的场景,阶段和控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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