JavaFX透明舞台和场景
我是JavaFX新手,我正在尝试创建一个透明的场景和舞台。问题是,当我添加像图像和标签这样的节点时,场景不再是透明的 这是我的密码JavaFX透明舞台和场景,java,xml,javafx,fxml,scene,Java,Xml,Javafx,Fxml,Scene,我是JavaFX新手,我正在尝试创建一个透明的场景和舞台。问题是,当我添加像图像和标签这样的节点时,场景不再是透明的 这是我的密码 package application; import javafx.application.Application; import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; import javafx.stage.Stage; import javafx.stage.StageSty
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);
}
}
包应用;
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.fxml.fxmloader;
导入javafx.stage.stage;
导入javafx.stage.StageStyle;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.scene.input.MouseEvent;
公共类主扩展应用程序{
专用双xOffset=0;
专用双yOffset=0;
public void start(Stage primaryStage)引发异常{
试试{
亲本根;
root=fxmloader.load(getClass().getResource(“/View/authentication.fxml”);
initStyle(StageStyle.TRANSPARENT);
场景=新场景(根);
scene.setFill(null);
setOnMousePressed(新的EventHandler(){
@凌驾
公共无效句柄(MouseeEvent事件){
xOffset=event.getSceneX();
yOffset=event.getSceneY();
}
});
setOnMouseDrawed(新的EventHandler(){
@凌驾
公共无效句柄(MouseeEvent事件){
setX(event.getScreenX()-xOffset);
setY(event.getScreenY()-yOffset);
}
});
初级阶段。场景(场景);
primaryStage.show();}
捕获(例外e){
e、 printStackTrace();
}
}
公共静态void main(字符串[]args){
发射(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>
如果创建一个或多个控件(特别是控件的任何实例或子类),默认样式表将应用于场景。这将场景根的背景色设置为不透明的“非常浅的灰色”(基本上比#ececec
亮26.4%)
(具体而言,默认样式表包含以下内容:
.root{
/***************************************************************************
* *
*衍生其他颜色的主调色板*
* *
**************************************************************************/
/*一种浅灰色,是物体的底色。代替使用
*-fx base直接,此文件中的部分通常使用-fx颜色。
*/
-外汇基数:#ececec;
/*一种非常浅的灰色,用作窗户的背景。另见
*-fx文本背景色,应用作-fx文本填充
*使用-fx background着色的背景顶部绘制的文本的值。
*/
-外汇背景:衍生(-外汇基础,26.4%);
/* ... */
-fx背景色:-fx背景;
}
当前版本的默认样式表(在编写本文时)的源代码可以在中找到
因此,还需要使场景的根透明
您可以使用内嵌CSS在FXML中执行此操作:
或在Java中:
Parent root ;
root = FXMLLoader.load(getClass().getResource("/View/Authentification.fxml"));
root.setStyle("-fx-background-color: transparent ;");
也可以在外部样式表中执行此操作:
.root{
-背景色:透明;
}
如果创建一个或多个控件(特别是控件的任何实例或子类),默认样式表将应用于场景。这将场景根的背景色设置为不透明的“非常浅的灰色”(基本上比#ececec
亮26.4%)
(具体而言,默认样式表包含以下内容:
.root{
/***************************************************************************
* *
*衍生其他颜色的主调色板*
* *
**************************************************************************/
/*一种浅灰色,是物体的底色。代替使用
*-fx base直接,此文件中的部分通常使用-fx颜色。
*/
-外汇基数:#ececec;
/*一种非常浅的灰色,用作窗户的背景。另见
*-fx文本背景色,应用作-fx文本填充
*使用-fx background着色的背景顶部绘制的文本的值。
*/
-外汇背景:衍生(-外汇基础,26.4%);
/* ... */
-fx背景色:-fx背景;
}
当前版本的默认样式表(在编写本文时)的源代码可以在中找到
因此,还需要使场景的根透明
您可以使用内嵌CSS在FXML中执行此操作:
或在Java中:
Parent root ;
root = FXMLLoader.load(getClass().getResource("/View/Authentification.fxml"));
root.setStyle("-fx-background-color: transparent ;");
也可以在外部样式表中执行此操作:
.root{
-背景色:透明;
}
JavaFx中的每个控件都可以是透明的。我没有阅读您的代码,但我想您没有将“transparent”属性应用于所有控件(节点),JavaFx中的每个控件都可以是透明的。我没有阅读您的代码,但我想您没有对所有控件(节点)应用“透明”属性