2011-12-29 78 views
7

我試圖在javafx 2.0中創建一個自定義的舞臺。我想,隨着下降被其他窗口我的舞臺滴在屏幕上的影子......我試着用下面的代碼:在javafx 2.0中創建未裝飾的舞臺

public class ChatWindow { 
final private Stage stage = new Stage(StageStyle.UNDECORATED); 
private Scene scene; 
private Group rg; 
private Text t = new Text(); 
private double initx = 0, inity = 0; 

public ChatWindow() { 

    rg = new Group(); 
    scene = new Scene(rg, 320, 240); 
    //scene.setFill(null); 
    scene.setFill(new Color(0, 0, 0, 0)); 
    stage.setScene(scene); 
    stage.show(); 
    setupStage(); 
} 

private void setupStage() { 
    Rectangle r = new Rectangle(5, 5, stage.getWidth() - 10, stage.getHeight() - 10); 
    r.setFill(Color.STEELBLUE); 
    r.setEffect(new DropShadow()); 
    rg.setOnMousePressed(new EventHandler<MouseEvent>() { 
     public void handle(MouseEvent me) { 
      initx = me.getScreenX() - stage.getX();// - me.getSceneX(); 
      inity = me.getScreenY() - stage.getY(); 
     } 
    }); 
    rg.setOnMouseDragged(new EventHandler<MouseEvent>() { 

     public void handle(MouseEvent me) { 
      stage.setX(me.getScreenX() - initx); 
      stage.setY(me.getScreenY() - inity); 
     } 
    }); 
    rg.getChildren().add(r); 
    rg.getChildren().add(t); 
} 

public void setVisible() { 
    stage.show(); 
} 
} 

我可以看到陰影秋季,但實際上他們是在其上的下降在白色背景。 所以,它的無用的,因爲彩色屏幕上的缺陷將是可見的,將使它看起來難看..

這是截屏白屏: enter image description here

而這種彩色屏幕上: enter image description here

若要解決此問題??請幫忙。

回答

10

你應該設置樣式StageStyle.TRANSPARENT,看下代碼:

public class ChatWindow extends Application { 

    @Override 
    public void start(final Stage stage) throws Exception { 
     stage.initStyle(StageStyle.TRANSPARENT); // here it is 

     Group rg = new Group(); 
     Scene scene = new Scene(rg, 320, 240, Color.TRANSPARENT); 
     stage.setScene(scene); 
     stage.show(); 

     Rectangle r = new Rectangle(5, 5, stage.getWidth() - 10, stage.getHeight() - 10); 
     r.setFill(Color.STEELBLUE); 
     r.setEffect(new DropShadow()); 
     rg.getChildren().add(r); 
    } 

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