简体   繁体   English

如何在javafx的透明阶段内创建非透明对象?

[英]how to create non-transparent objects within transparent stage in javafx?

The thing that I am trying to here is to create a opaque button in StackPane within a transparent stage, but making the stage transparent will make all the contents transparent. 我要在此处进行的操作是在透明阶段中的StackPane创建一个不透明按钮,但是使该阶段透明将使所有内容透明。

import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.StackPane; 
import javafx.stage.*;

public class HelloWorld extends Application {

   @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }  
        });

       StackPane root = new StackPane();
       root.getChildren().add(btn);

       Scene scene = new Scene(root, 300, 250);

       primaryStage.setTitle("Hello World!");
       primaryStage.initStyle(StageStyle.TRANSPARENT);
       primaryStage.setOpacity(0.4);
       primaryStage.setScene(scene);
       primaryStage.show();
       scene.getStylesheets().add("sample.css");
   }

   public static void main(String[] args) { 
       launch(args);
   }
} 

sample.css file contains sample.css文件包含

.button {
    -fx-background-color: rgba(255,0,0,1);
}

Do this 做这个

btn.setStyle("-fx-background-color: any-non-transparent-color-you-wish;");

This will change the background of the button making it non transparent 这将更改按钮的背景,使其不透明

just found out the answer. 刚刚找到答案。 All I had to do was to change the fill colour to null on scene and change the css file. 我要做的就是将场景中的填充颜色更改为null并更改css文件。

scene.setFill(null);

css file CSS文件

.root {
-fx-background-color: rgba(0,0,0,0.1);
}
.button {
-fx-background-color: rgba(255,0,0,1);
}

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

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