2017-07-13 41 views
0

下面的示例適用於純文本,但一旦我添加一個按鈕到舞臺,透明變爲無效如何使JavaFX的舞臺透明(第一階段只)

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.VBox; 
import javafx.scene.text.Font; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

public class Main extends Application { 

    @Override 
    public void start(Stage stage) { 
     stage.initStyle(StageStyle.TRANSPARENT); 
     Text text = new Text("!"); 
     text.setFont(new Font(40)); 
     VBox box = new VBox(); 
     Button btn = new Button("Test transparent"); 
     box.getChildren().addAll(text, btn); 
     //if I removed the btn, transparent works as expected. 
     final Scene scene = new Scene(box,300, 250); 
     scene.setFill(null); 
     stage.setScene(scene); 
     stage.show(); 
    } 

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

我所尋找的是隻使舞臺透明,但顯示的文字和按鈕

回答

1

您的Button在這種情況下是不是一個問題。默認VBox有灰色背景,所以你的Stage是透明的,但VBox不是。你必須通過CSS文件或內嵌或代碼來設置透明背景:

CSS:

.your-vbox { 
    -fx-background-color: transparent; 
} 

內聯:

box.setStyle("-fx-background-color: transparent;"); 

代碼:

box.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY))); 
+0

太感謝你了! ! – Moe