2013-07-10 34 views
5

怎么了?我试图让我的窗格更好一些,视觉上,所以,我在做什么是:设置我的阶段UNDECORATED(OK)和(尝试)添加阴影效果(不OK) 。在未装饰的窗格中投下阴影! JAVAFX

我在网上搜索了这样一个类似的问题,发现了一些类似的案例(creating undecorated stage in javafx 2.0How to add shadow to window in JavaFX?),但都不适用于我。

它看起来像投下阴影只是不设置!不明白为什么。

看看我的了:

public static int showConfirmDialog(Window father, String title, String body, String[]  msgBtn) 
{ 
    System.out.println("La vai eu"); 
    AnchorPane ap = createPaneWithButton(2, msgBtn,body); 

    ap.setEffect(initDropShadow()); 
    Scene scene = new Scene(ap); 

    Stage stage = new Stage(); 

    stage.setTitle(title); 

    scene.setFill(null); 
    stage.initStyle(StageStyle.TRANSPARENT); 


    stage.setScene(scene); 
    stage.initStyle(StageStyle.UNDECORATED); 
    stage.show(); 

    return 1; 
} 

private static AnchorPane createPaneWithButton(int qtBtn, String[] msgsBtn, String body) { 
    AnchorPane ap = createPane(); 
    HBox laneBtn = new HBox(30); 
    VBox vbox = new VBox(20); 

    BorderPane layout = new BorderPane(); 

    Button btn; 

    for(int i = 0; i < qtBtn; i++){ 
     btn = new Button(); 
     btn.setText(msgsBtn[i]); 

     laneBtn.getChildren().add(btn); 
    } 

    vbox.getChildren().add(new Text(body)); 
    vbox.getChildren().add(laneBtn); 

    layout.setCenter(vbox); 

    ap.getChildren().add(layout); 

    return ap; 
} 

private static AnchorPane createPane() { 
    AnchorPane ap = new AnchorPane(); 

    ap.setLayoutX(250); 
    ap.setLayoutY(50); 

    return ap; 
} 

谢谢你们!我期待着回应! (虽然我会继续尝试我能做的)。

PS :.对于英语来说,不是我的主要语言。希望你能理解。

+1

向我们展示initDropShadow()。 –

回答

11

前面的例子对我的作品

的答案How to add shadow to window in JavaFX?工作正常,我提供的example code Java的8b96,Windows 7的。当我写的JavaFX的2(删除对话框上的阴影可见),它也在该环境中工作。

因为您没有提供完整的可执行代码,所以我无法确切地说出您在示例中缺少的内容。与您的代码

transparent-dialog

可能发行我的猜测是这样的,有对话的影子在空间中显示你是不是insetting背景内容。也就是说,您正在填充含有内容的对话框,并且不会在围绕内容的对话框中留出空间来显示效果。下面的示例实现与CSS规则的insetting -fx-background-insets: 12;

我复制的示例代码修改后的版本为这个答案,以便它不只是包含在一个不起眼的要点链接关闭更新的样本代码另一个答案。这些修改只是使用标准API调用,因为原始答案中使用的构建器已被弃用,因为原始答案已创建。

ModalConfirmExample.java

import javafx.application.Application; 
import javafx.beans.value.*; 
import javafx.concurrent.Worker; 
import javafx.event.*; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.effect.BoxBlur; 
import javafx.scene.effect.Effect; 
import javafx.scene.layout.*; 
import javafx.scene.paint.Color; 
import javafx.scene.web.WebView; 
import javafx.stage.Modality; 
import javafx.stage.*; 

/** 
* Application modal dialog with the following properties: 
* translucent background 
* drop-shadowed border 
* non-rectangular shape 
* blur effect applied to parent when dialog is showing 
* configurable message text 
* configurable yes and no event handlers 
*/ 
class ModalDialog extends Stage { 
    private static final Effect parentEffect = new BoxBlur(); 

    private final String messageText; 
    private final EventHandler<ActionEvent> yesEventHandler; 
    private final EventHandler<ActionEvent> noEventHandler; 

    public ModalDialog(
      Stage parent, 
      String messageText, 
      EventHandler<ActionEvent> yesEventHandler, 
      EventHandler<ActionEvent> noEventHandler) { 
     super(StageStyle.TRANSPARENT); 

     this.messageText = messageText; 
     this.yesEventHandler = yesEventHandler; 
     this.noEventHandler = noEventHandler; 

     // initialize the dialog 
     initOwner(parent); 
     initParentEffects(parent); 
     initModality(Modality.APPLICATION_MODAL); 
     setScene(createScene(createLayout())); 
    } 

    private StackPane createLayout() { 
     StackPane layout = new StackPane(); 
     layout.getChildren().setAll(
       createGlassPane(), 
       createContentPane() 
     ); 

     return layout; 
    } 

    private Pane createGlassPane() { 
     final Pane glassPane = new Pane(); 
     glassPane.getStyleClass().add(
       "modal-dialog-glass" 
     ); 

     return glassPane; 
    } 

    private Pane createContentPane() { 
     final HBox contentPane = new HBox(); 
     contentPane.getStyleClass().add(
       "modal-dialog-content" 
     ); 
     contentPane.getChildren().setAll(
       new Label(messageText), 
       createYesButton(), 
       createNoButton() 
     ); 

     return contentPane; 
    } 

    private Button createYesButton() { 
     final Button yesButton = new Button("Yes"); 
     yesButton.setDefaultButton(true); 
     yesButton.setOnAction(yesEventHandler); 

     return yesButton; 
    } 

    private Button createNoButton() { 
     final Button noButton = new Button("No"); 
     noButton.setOnAction(noEventHandler); 

     return noButton; 
    } 

    private Scene createScene(StackPane layout) { 
     Scene scene = new Scene(layout, Color.TRANSPARENT); 
     scene.getStylesheets().add(
       getClass().getResource(
         "modal-dialog.css" 
       ).toExternalForm() 
     ); 

     return scene; 
    } 

    private void initParentEffects(final Stage parent) { 
     this.showingProperty().addListener(new ChangeListener<Boolean>() { 
      @Override public void changed(ObservableValue<? extends Boolean> observableValue, Boolean wasShowing, Boolean isShowing) { 
       parent.getScene().getRoot().setEffect(
         isShowing ? parentEffect : null 
       ); 
      } 
     }); 
    } 
} 

/** 
* Demonstrates a modal confirm box in JavaFX. 
* Dialog is rendered upon a blurred background. 
* Dialog is translucent. 
*/ 
public class ModalConfirmExample extends Application { 
    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(final Stage primaryStage) { 
     final WebView webView = new WebView(); 

     final ModalDialog dialog = createWebViewPreferenceDialog(primaryStage, webView); 

     // show the preference dialog each time a new page is loaded. 
     webView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() { 
      @Override 
      public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State state, Worker.State newState) { 
       if (newState.equals(Worker.State.SUCCEEDED)) { 
        dialog.show(); 
        dialog.toFront(); 
       } 
      } 
     }); 
     webView.getEngine().load("http://docs.oracle.com/javafx/"); 

     // initialize the stage 
     primaryStage.setTitle("Modal Confirm Example"); 
     primaryStage.setScene(new Scene(webView)); 
     primaryStage.show(); 
    } 

    private ModalDialog createWebViewPreferenceDialog(final Stage primaryStage, final WebView webView) { 
     final EventHandler<ActionEvent> yesEventHandler = 
       new EventHandler<ActionEvent>() { 
        @Override public void handle(ActionEvent actionEvent) { 
         System.out.println("Liked: " + webView.getEngine().getTitle()); 
         primaryStage.getScene().getRoot().setEffect(null); 
         Stage dialogStage = getTargetStage(actionEvent); 
         dialogStage.close(); 
        } 
       }; 

     final EventHandler<ActionEvent> noEventHandler = 
       new EventHandler<ActionEvent>() { 
        @Override public void handle(ActionEvent actionEvent) { 
         System.out.println("Disliked: " + webView.getEngine().getTitle()); 
         primaryStage.getScene().getRoot().setEffect(null); 
         Stage dialogStage = getTargetStage(actionEvent); 
         dialogStage.close(); 
        } 
       }; 

     return new ModalDialog(primaryStage, "Will you like this Page?", yesEventHandler, noEventHandler); 
    } 

    private Stage getTargetStage(ActionEvent actionEvent) { 
     Node target = (Node) actionEvent.getTarget(); 
     return ((Stage) target.getScene().getWindow()); 
    } 
} 

模式-对话框。CSS

.root { 
    -fx-opacity: 0.9; 
} 

.modal-dialog-glass { 
    -fx-effect: dropshadow(three-pass-box, derive(cadetblue, -20%), 10, 0, 4, 4); 
    -fx-background-color: derive(cadetblue, -20%); 
    -fx-background-insets: 12; 
    -fx-background-radius: 6; 
} 

.modal-dialog-content { 
    -fx-padding: 20; 
    -fx-spacing: 10; 
    -fx-alignment: center; 
    -fx-font-size: 20; 
    -fx-background-color: linear-gradient(to bottom, derive(cadetblue, 20%), cadetblue); 
    -fx-border-color: derive(cadetblue, -20%); 
    -fx-border-width: 5; 
    -fx-background-insets: 12; 
    -fx-border-insets: 10; 
    -fx-border-radius: 6; 
    -fx-background-radius: 6; 
} 

使用库,而不是

另请注意,创建对话框我强烈建议使用ControlsFX项目,而不是创建自己的对话系统。如果ControlsFX缺少您需要的功能(例如投影支持),则您可以使用file a feature request针对ControlsFX项目,并在必要时链接回此答案。

+1

MAN,I FC!* @&#爱你!哈哈!! 我刚加了-fx-background-insets!它解决了问题! O my gosh ..只是想着花了我的时间...谢谢你! 可能奥丁保佑你! –

+1

@ GabrielLopes是的,当我阅读他的答案时,我会得到同样的反应。 – GGrec

0

那么,我发现很简单的解决方案。也许这在早期版本中不被支持? 但是..代码:

iconPane.setEffect(new DropShadow(2d, 0d, +2d, Color.BLACK)); 

图为结果 - 我的目的是要显示的FAB

enter image description here

4

简单工作例如外观的图标。

First result exampleSecond result example

这里是fxml结构:

AnchorPane(200,200) // pane for space for shadow 
    \- AnchorPane(center) // appliction pane 
     \- Label // application content 

真实screen.fxml

<AnchorPane fx:id="shadowPane" prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <AnchorPane fx:id="rootPane" layoutX="56.0" layoutY="62.0"> 
     <children> 
      <Label fx:id="someLabel" layoutX="30.0" layoutY="30.0" text="Label" textFill="#f20000" /> 
     </children> 
     <padding> 
      <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" /> 
     </padding> 
     </AnchorPane> 
    </children> 
</AnchorPane> 

Main.java

@Override 
public void start(Stage stage) throws Exception { 
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/sample/screen.fxml")); 
    AnchorPane shadowPane = loader.load(); 
    AnchorPane rootPane = (AnchorPane) shadowPane.lookup("#rootPane"); 
    rootPane.setStyle("-fx-effect: dropshadow(gaussian, rgba(0, 0, 0, 0.4), 10, 0.5, 0.0, 0.0);" + 
         "-fx-background-color: white;"); // Shadow effect 

    Scene scene = new Scene(shadowPane); 
    stage.setScene(scene); 

    shadowPane.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, null, null))); // Some borders for for clarity 

    shadowPane.setStyle("-fx-background-color: transparent;"); // Makes shadowPane transparent 
    scene.setFill(Color.TRANSPARENT); // Fill our scene with nothing 
    stage.initStyle(StageStyle.TRANSPARENT); // Important one! 
    stage.show(); 
} 
+0

我会很好的提到,应该将-fx-padding添加到rootPane –