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

时间:2016-06-05 07:41:45

标签: java css javafx

我在这里尝试的是在透明舞台中的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文件包含

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

2 个答案:

答案 0 :(得分:0)

这样做

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

这会改变按钮的背景,使其不透明

答案 1 :(得分:-1)

刚刚找到了答案。我所要做的就是在场景中将填充颜色更改为null并更改css文件。

scene.setFill(null);

css文件

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