2013-12-18 47 views
0

我正在做一個excersice我JavaFX與NetBeans,我試圖做一個彈出時,我clic一個按鈕,但彈出有白色背景,我怎麼能做到透明?在javafx中使可拖動的彈出式背景透明嗎?

http://i.stack.imgur.com/tkjdI.png

謝謝。

下面是代碼:

主:

package test; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 
import javafx.scene.image.Image; 

    public class Test extends Application { 

    @Override 

    public void start(Stage stage) throws Exception { 

     stage.initStyle(StageStyle.TRANSPARENT); 
     Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

     Scene scene = new Scene(root, Color.TRANSPARENT); 

     stage.setScene(scene); 
     stage.show(); 
     stage.setTitle("integrIX"); 
     stage.getIcons().add(new Image("test/integrix_icon.png")); 
    } 

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

FXMLController:

package test; 

import java.io.IOException; 
import java.net.URL; 
import java.util.ResourceBundle; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javafx.application.Platform; 
import javafx.event.Event; 
import javafx.event.EventHandler; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Cursor; 
import javafx.scene.Node; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.Pane; 
import javafx.stage.Modality; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

public class FXMLDocumentController implements Initializable { 

    @FXML 
    private Button menu1; 
    @FXML 
    private Button menu2; 
    @FXML 
    private Button menu3; 
    @FXML 
    private Button menu4; 

    @FXML 
    private Button closeButton; 
    @FXML 
    private Button minimizeButton; 
    @FXML 
    private Pane paneMain; 

    private double xOffset; 
    private double yOffset; 

    private Stage popup; 

@Override 
    public void initialize(URL url, ResourceBundle rb) { 

     Platform.runLater(new Runnable(){ 
      private StageStyle Color; 
      @Override 
      public void run(){ 
       Stage stage = Stage.class.cast(paneMain.getScene().getWindow()); 
       makeDraggable(stage, paneMain); 
       Parent popupLoaded = null; 
       try { 
        popupLoaded = FXMLLoader.load(getClass().getResource("popup.fxml")); 
       } catch (IOException ex) { 
        Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); 
       } 
       Scene popupScene = new Scene(popupLoaded); 

       popup = new Stage(); 
       popup.initStyle(StageStyle.TRANSPARENT); 
       popup.initModality(Modality.WINDOW_MODAL); 
       popup.initOwner(stage); 
       popup.setScene(popupScene); 

      } 
     }); 
    } 

    @FXML 
    public void diagnosticButton(){ 
     assert popup != null; 
     popup.show(); 
    } 

    @FXML 
    public void handleClick(MouseEvent event) { 
     //System.err.println("BOUND OFFSET"); 

     //xOffset = event.getSceneX(); 
       //yOffset = event.getSceneY(); 

       //xOffset = event.getSceneX(); 
       //yOffset = event.getSceneY(); 
    } 
    @FXML 
    public void handleDrag(MouseEvent event) { 
     //System.err.println("DRAGGING"); 
     Stage stage = Stage.class.cast(paneMain.getScene().getWindow()); 
       stage.setX(event.getScreenX() - xOffset); 
       stage.setY(event.getScreenY() - yOffset); 
    } 

    public void makeDraggable(final Stage stage, final Node byNode) {  
    byNode.setOnMousePressed(new EventHandler<MouseEvent>() {  
    @Override public void handle(MouseEvent mouseEvent) {  
    // record a delta distance for the drag and drop operation.   
    xOffset = stage.getX() - mouseEvent.getScreenX();   
    yOffset = stage.getY() - mouseEvent.getScreenY();   
    byNode.setCursor(Cursor.MOVE);  
    }  
    });  
    byNode.setOnMouseReleased(new EventHandler<MouseEvent>() {  
     @Override public void handle(MouseEvent mouseEvent) {  
     byNode.setCursor(Cursor.DEFAULT);  
     }  
    });  
    byNode.setOnMouseDragged(new EventHandler<MouseEvent>() {  
    @Override public void handle(MouseEvent mouseEvent) {   
    stage.setX(mouseEvent.getScreenX() + xOffset);   
    stage.setY(mouseEvent.getScreenY() + yOffset);  
    } });  
byNode.setOnMouseEntered(new EventHandler<MouseEvent>() {  
@Override public void handle(MouseEvent mouseEvent) {   
if (!mouseEvent.isPrimaryButtonDown()) {   
byNode.setCursor(Cursor.DEFAULT);  
}  } });  
byNode.setOnMouseExited(new EventHandler<MouseEvent>() {  
@Override public void handle(MouseEvent mouseEvent) {   
if (!mouseEvent.isPrimaryButtonDown()) {   
byNode.setCursor(Cursor.DEFAULT);  
}  } }); 
} 

    @FXML 
    public void changeButtonOnOver(Event e){ 
    menu1.getStyleClass().add("boton_over"); 
    } 

    @FXML 
    public void changeButtonOnExited(){ 
      menu1.getStyleClass().remove("boton_over"); 
    } 

    @FXML 
    public void changeButtonOnOver2(Event e){ 
    menu2.getStyleClass().add("boton_over"); 
    } 

    @FXML 
    public void changeButtonOnExited2(){ 
      menu2.getStyleClass().remove("boton_over"); 
    } 


    @FXML 
    public void changeButtonOnOver3(Event e){ 
    menu3.getStyleClass().add("boton_over"); 
    } 

    @FXML 
    public void changeButtonOnExited3(){ 
      menu3.getStyleClass().remove("boton_over"); 
    } 


    @FXML 
    public void changeButtonOnOver4(Event e){ 
    menu4.getStyleClass().add("boton_over"); 
    } 

    @FXML 
    public void changeButtonOnExited4(){ 
      menu4.getStyleClass().remove("boton_over"); 
    } 



    @FXML 
    public void changeButtonOnOverClose(){ 
     closeButton.getStyleClass().add("btn_cerrar_over"); 
    } 

    @FXML 
    public void changeButtonOnExitedClose(){ 
     closeButton.getStyleClass().remove("btn_cerrar_over"); 
    } 


    @FXML 
    public void changeButtonOnOverMinimize(){ 
     minimizeButton.getStyleClass().add("btn_cerrar_over"); 
    } 


    @FXML 
    public void changeButtonOnExitedMinimize(){ 
     minimizeButton.getStyleClass().remove("btn_cerrar_over"); 
    } 

    @FXML 
    public void exit(){ 
     Stage stage = Stage.class.cast(closeButton.getScene().getWindow()); 
     stage.close(); 
    } 

    @FXML 
    public void minimize(){ 
     Stage stage = Stage.class.cast(minimizeButton.getScene().getWindow()); 
     stage.setIconified(true); 
    } 

} 

彈出控制器:

package test; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.application.Platform; 
import javafx.event.EventHandler; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.Cursor; 
import javafx.scene.Node; 
import javafx.scene.Parent; 
import javafx.scene.control.Button; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.Pane; 
import javafx.stage.Stage; 

public class PopupController implements Initializable { 

    @FXML 
    private Pane rect; 

    private double xOffset; 
    private double yOffset; 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     Platform.runLater(new Runnable(){ 
      private Parent root; 
      @Override 
      public void run(){ 

       Stage stage = Stage.class.cast(rect.getScene().getWindow()); 

       makeDraggable(stage, rect); 

       }}); 
    } 

    @FXML 
    public void initialize(){ 
     System.err.println("init popup called"); 
     assert rect != null; 
       Stage stage = Stage.class.cast(rect.getScene().getWindow()); 

       makeDraggable(stage, rect); 

    } 

    public void makeDraggable(final Stage stage, final Node byNode) {  
    byNode.setOnMousePressed(new EventHandler<MouseEvent>() {  
    @Override public void handle(MouseEvent mouseEvent) {  
    // record a delta distance for the drag and drop operation.   
    xOffset = stage.getX() - mouseEvent.getScreenX();   
    yOffset = stage.getY() - mouseEvent.getScreenY();   
    byNode.setCursor(Cursor.MOVE);  
    }  
    });  
    byNode.setOnMouseReleased(new EventHandler<MouseEvent>() {  
     @Override public void handle(MouseEvent mouseEvent) {  
     byNode.setCursor(Cursor.DEFAULT);  
     }  
    });  
    byNode.setOnMouseDragged(new EventHandler<MouseEvent>() {  
    @Override public void handle(MouseEvent mouseEvent) {   
    stage.setX(mouseEvent.getScreenX() + xOffset);   
    stage.setY(mouseEvent.getScreenY() + yOffset);  
    } });  
byNode.setOnMouseEntered(new EventHandler<MouseEvent>() {  
@Override public void handle(MouseEvent mouseEvent) {   
if (!mouseEvent.isPrimaryButtonDown()) {   
byNode.setCursor(Cursor.DEFAULT);  
}  } });  
byNode.setOnMouseExited(new EventHandler<MouseEvent>() {  
@Override public void handle(MouseEvent mouseEvent) {   
if (!mouseEvent.isPrimaryButtonDown()) {   
byNode.setCursor(Cursor.DEFAULT);  
}  } }); 
} 


    @FXML 
    private Button closeButton; 

    @FXML 
    public void changeButtonOnOverClose(){ 
     closeButton.getStyleClass().add("btn_cerrar_over"); 
    } 

    @FXML 
    public void changeButtonOnExitedClose(){ 
     closeButton.getStyleClass().remove("btn_cerrar_over"); 
    } 

    @FXML 
    public void exit(){ 
     Stage stage = Stage.class.cast(closeButton.getScene().getWindow()); 
     stage.close(); 
    } 
} 
+0

在未裝飾面板的[陰影可能重複! JAVAFX](http://stackoverflow.com/questions/17571593/drop-shadow-in-an-undecorated-pane-javafx) – jewelsea

+0

查看controlFx中的PopOver組件http://fxexperience.com/controlsfx/features/你可以拖動彈出框,設置是不透明的,設置是內容...這是一個非常酷的組件 –

+0

[javafx.stage.Popup](http://docs.oracle.com/javafx/2/api/javafx/stage/ Popup.html)也適用於此。它具有透明的背景,因此非常適合上圖所示的工具提示和彈出窗口(爲未來的讀者提到這一點)。 – BlueWizard

回答

0

在彈出的控制器初始化嘗試:

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