简体   繁体   中英

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.

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

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

scene.setFill(null);

css file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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