简体   繁体   English

JavaFX透明舞台和场景

[英]JavaFX transparent stage and scene

I'm new to JavaFX , i'm trying to create a transparent scene and stage the problem is when I add nodes like images and labels , the scene is no longer transparent this is my code 我是JavaFX的新手,我试图创建一个透明的场景并进行阶段操作,问题是当我添加诸如图像和标签之类的节点时,该场景不再透明了,这是我的代码

package application;




import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;



public class Main extends Application { 

    private double xOffset = 0;
    private double yOffset = 0;
    public void start(Stage primaryStage) throws Exception {
    try{ 

        Parent root ;
        root = FXMLLoader.load(getClass().getResource("/View/Authentification.fxml"));

        primaryStage.initStyle(StageStyle.TRANSPARENT);
        Scene scene = new Scene(root);
        scene.setFill(null);

         root.setOnMousePressed(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    xOffset = event.getSceneX();
                    yOffset = event.getSceneY();
                }
            });
            root.setOnMouseDragged(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    primaryStage.setX(event.getScreenX() - xOffset);
                    primaryStage.setY(event.getScreenY() - yOffset);
                }
            });


        primaryStage.setScene(scene);

    primaryStage.show();}
     catch(Exception e) {
            e.printStackTrace();
        }  
    }
    public static void main(String[] args) {
        launch(args);
    }
}

and this is my xml code : 这是我的xml代码:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">

      <AnchorPane layoutY="94.0" prefHeight="417.0" prefWidth="363.0" style="-fx-background-color: #3D4966;"/>
      <AnchorPane layoutX="-1.0" prefHeight="82.0" prefWidth="363.0" style="-fx-background-color: #3D4966;">
      <children>
      <Label layoutX="280.0" layoutY="70.0" text="Fermer" textFill="#eee5e5">
         <font>
            <Font name="System Bold" size="12.0" />
         </font>
      </Label>
      <ImageView layoutX="288.0" layoutY="22.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../images/Shutdown.png" />
         </image>
      </ImageView>
      <Label layoutX="146.0" layoutY="61.0" prefHeight="17.0" prefWidth="92.0" text="crée un compte" textFill="#eee5e5">
         <font>
            <Font name="System Bold" size="12.0" />
         </font>
      </Label>
      <ImageView layoutX="167.0" layoutY="21.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../images/Add_User.png" />
         </image>
      </ImageView>
      <ImageView layoutX="50.0" layoutY="21.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../images/User.png" />
         </image>
      </ImageView>
      <Label layoutX="50.0" layoutY="62.0" text="Ce connecter" textFill="#eee5e5">
         <font>
            <Font name="System Bold" size="12.0" />
         </font>
      </Label>
   </children>
   </AnchorPane>


</AnchorPane>

i tried a lot of other codes but nothing seems to work these are some screenshots with nodes 我尝试了很多其他代码,但似乎无济于事,这些是一些带有节点的屏幕截图

without nodes 没有节点

If you create one or more controls (specifically, any instance of Control or a subclass), the default stylesheet is applied to the scene. 如果创建一个或多个控件(特别是Control任何实例或子类),则默认样式表将应用于场景。 This sets the background color of the root of the scene to a non-transparent "very light grey" (basically 26.4% lighter than #ececec ). #ececec场景根的背景色设置为非透明的“非常浅灰色”(基本上比#ececec浅26.4%)。

(Specifically, the default stylesheet contains the following: (具体来说,默认样式表包含以下内容:

.root {
    /***************************************************************************
     *                                                                         *
     * The main color palette from which the rest of the colors are derived.   *
     *                                                                         *
     **************************************************************************/

    /* 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;

    /* 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%);

    /* ... */

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

}

The source for the current version of the default stylesheet (at the time of writing) can be found at http://hg.openjdk.java.net/openjfx/9/rt/file/c734b008e3e8/modules/javafx.controls/src/main/resources/com/sun/javafx/scene/control/skin/modena/modena.css ). 可以在http://hg.openjdk.java.net/openjfx/9/rt/file/c734b008e3e8/modules/javafx.controls/src中找到默认样式表(在撰写本文时)的当前版本的源代码。 /main/resources/com/sun/javafx/scene/control/skin/modena/modena.css )。

So you need to make the root of the scene transparent too. 因此,您还需要使场景的根部透明。

You can do this using inline CSS, either in the FXML: 您可以在FXML中使用内联CSS来执行此操作:

<AnchorPane style="-fx-background-color: transparent ;" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">

or in Java: 或使用Java:

    Parent root ;
    root = FXMLLoader.load(getClass().getResource("/View/Authentification.fxml"));
    root.setStyle("-fx-background-color: transparent ;");

or you can do it in an external style sheet: 或者您可以在外部样式表中执行此操作:

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

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

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