Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 透明舞台/场景在加载另一个FXML文件后失去透明度_Java_Javafx_Controller_Fxml_Stage - Fatal编程技术网

Java 透明舞台/场景在加载另一个FXML文件后失去透明度

Java 透明舞台/场景在加载另一个FXML文件后失去透明度,java,javafx,controller,fxml,stage,Java,Javafx,Controller,Fxml,Stage,当我启动我的应用程序时,我的透明javafx.stage.stage会像预期的那样显示一个半透明的图像。但在加载第二阶段后,第一阶段将失去透明度 奇怪的是,如果第二个fxml文件(“MainScreen.fxml”)不包含任何组件,如按钮或文本字段,则背景保持透明 我正在macOS Sierra上的EclipseNeon.2中使用JavaFX和JavaSE-1.8 主类 控制器类 FXML文件(“LaunchScreen.FXML”) 到外部样式表,并将stylesheets=“@applic

当我启动我的应用程序时,我的透明
javafx.stage.stage
会像预期的那样显示一个半透明的图像。但在加载第二阶段后,第一阶段将失去透明度

奇怪的是,如果第二个fxml文件(“MainScreen.fxml”)不包含任何组件,如按钮或文本字段,则背景保持透明

我正在macOS Sierra上的EclipseNeon.2中使用JavaFX和JavaSE-1.8

主类 控制器类 FXML文件(“LaunchScreen.FXML”)
到外部样式表,并将
stylesheets=“@application.css”
添加到FXML文件中的根节点。

JavaFX、modena的默认css样式表将不透明颜色应用到根节点的背景。这就是显示主视图时看到的颜色

您在第一个屏幕中看不到这一点的原因,或者如果从主屏幕中删除按钮,是因为默认样式表仅在
控件
类(或其子类之一)第一次实例化时加载。这样做是为了避免不需要CSS的应用程序的CSS处理开销

要修复此问题,请将
style=“-fx background color:transparent;”“
添加到FXML文件的根节点,或者添加规则

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

到外部样式表

谢谢,我将外部样式表添加到FMXL文件中,现在它可以工作了!
package customPackage.main;

import java.io.IOException;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.AnchorPane;

public class LaunchController extends AnchorPane {

    @FXML
    private ProgressBar bar;

    public LaunchController(String filename) {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(filename));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.effect.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<fx:root fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="326.0" prefWidth="883.0" type="AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <ImageView fitHeight="326.0" fitWidth="883.0" layoutX="7.0" layoutY="134.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <image>
            <Image url="@http://www.lunapic.com/editor/premade/transparent.gif" />
         </image>
      </ImageView>
   </children>
   <cursor>
      <Cursor fx:constant="DEFAULT" />
   </cursor>
</fx:root>
    <?xml version="1.0" encoding="UTF-8"?>

<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<fx:root fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" type="AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <ImageView fitHeight="720.0" fitWidth="1280.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <image>
            <Image url="@http://i.telegraph.co.uk/multimedia/archive/03589/Wellcome_Image_Awa_3589699k.jpg" />
         </image>
      </ImageView>
      <HBox spacing="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
         <children>
            <Button fx:id="button1" mnemonicParsing="false" onAction="#button1Press" prefHeight="45.0" prefWidth="1000.0" text="Singleplayer" />
            <Button fx:id="button2" mnemonicParsing="false" onAction="#button2Press" prefHeight="45.0" prefWidth="1000.0" text="Multiplayer" />
            <Button fx:id="button3" mnemonicParsing="false" onAction="#button3Press" prefHeight="45.0" prefWidth="1000.0" text="Settings" />
            <Button fx:id="button4" mnemonicParsing="false" onAction="#button4Press" prefHeight="45.0" prefWidth="1000.0" text="Quit" />
         </children>
         <padding>
            <Insets bottom="30.0" left="100.0" right="100.0" top="20.0" />
         </padding>
      </HBox>
      <Region fx:id="region" prefHeight="15.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
   </children>
</fx:root>
.root {
    -fx-background-color: transparent;
}
.root {
    -fx-background-color: transparent ;
}