javafx-2 – 在未修饰的窗格中投下阴影! JavaFX的

前端之家收集整理的这篇文章主要介绍了javafx-2 – 在未修饰的窗格中投下阴影! JavaFX的前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是怎么回事?

好吧,我试图让我的Pane在视觉上更好一点,所以,我正在做的是:设置我的舞台UNDECORATED(OK)和(TRYING)添加一个drophadow效果(不行).

我通过互联网搜索了这样的问题(很多),发现了一些类似的案例(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:. Srry for the English,不是我的主要语言.希望你能理解.

解决方法

先前示例适用于我

How to add shadow to window in JavaFX?的答案提供的example code对我来说很好(在可见对话框上显示阴影)在Java 8b96,Windows 7上.当我为JavaFX 2编写它时,它也在该环境中工作.

由于您没有提供完整的可执行代码,我无法准确地说出您在示例中缺少的内容.

您的代码可能出现问题

我的猜测是你没有插入背景内容,因此对话框中有空间可以显示阴影.也就是说,您正在填充内容对话框,而不是在内容对话框周围留出空间以显示效果.下面的例子实现了css规则的插入-fx-background-insets:12;

更新了示例代码

我将示例代码修改版本复制到了这个答案中,这样它就不会只包含在另一个答案中的一个模糊的gist链接中.修改只是使用标准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());
    }
}

莫代尔,dialog.css

.root {
  -fx-opacity: 0.9;
}

.modal-dialog-glass {
  -fx-effect: dropshadow(three-pass-Box,derive(cadetblue,-20%),10,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,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缺少您需要的功能(例如投影支持),则可以针对ControlsFX项目执行file a feature request,并在必要时链接回此答案.

原文链接:https://www.f2er.com/java/126576.html

猜你在找的Java相关文章