gpt4 book ai didi

JavaFX透明舞台和场景

转载 作者:行者123 更新时间:2023-11-29 08:30:47 25 4
gpt4 key购买 nike

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

这是我的 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>

我尝试了很多其他代码,但似乎没有任何效果这些是一些截图 with nodes

without nodes

最佳答案

如果您创建一个或多个控件(具体来说,Control 的任何实例或子类),默认样式表将应用于场景。这会将场景根部的背景颜色设置为不透明的“非常浅的灰色”(基本上比 #ececec 亮 26.4%)。

(具体来说,默认样式表包含以下内容:

.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;

}

可以在 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 找到当前版本的默认样式表(在撰写本文时)的源代码。 ).

所以你也需要让场景的根透明。

您可以在 FXML 中使用内联 CSS 执行此操作:

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

或者在 Java 中:

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

或者您可以在外部样式表中完成:

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

关于JavaFX透明舞台和场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48404440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com