在未装饰的窗格中投下阴影! JAVAFX [英] Drop Shadow in an undecorated Pane! JAVAFX

查看:102
本文介绍了在未装饰的窗格中投下阴影! JAVAFX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视觉上,我试图使Pane更好一些,所以,我正在做的是:设置我的舞台UNDECORATED(确定)和(TRYING)以添加阴影效果(NOK OK).

I'm trying to make my Pane a little bit better, visually, so, what I'm doing is: set my stage UNDECORATED (OK) and (TRYING) to add a dropshadow effect (NOT OK).

我在Internet上搜索了很多这样的问题,发现了一些类似的情况(中创建未修饰的阶段在JavaFX中向窗口添加阴影?),但对我都不起作用.

I Searched (A LOT) questions like this over the Internet, found some similar cases (creating undecorated stage in javafx 2.0 and How to add shadow to window in JavaFX?) but neither works for me.

似乎没有设置投影!不明白为什么.

It Seems like the drop shadow JUST isn't set! Can't understand why.

看看我得到了什么:

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;
}


谢谢你们!我期待得到答复! (尽管我会继续努力).


Thank you guys! I'm looking forward for the response! (While i'll keep trying what i can).

PS :.对不起英语,这不是我的主要语言.希望你能理解.

PS:. Srry for the english, isn't my main language. Hope you can understand.

推荐答案

适用于我的先前示例

示例代码提供了

The example code supplied for the answer to How to add shadow to window in JavaFX? works fine for me (drop shadow on the dialog visible) on Java 8b96, Windows 7. When I wrote it for JavaFX 2, it also worked in that environment as well.

由于您未提供完整的可执行代码,因此我无法确切说明示例中缺少的内容.

I couldn't say exactly what you are missing in your example as you did not provide full executable code.

您的代码可能存在的问题

我的猜测是,您没有设置背景内容,因此对话框中没有用于显示阴影的空间.也就是说,您正在用内容填充对话框,而在对话框中没有在内容周围留出空间以显示效果.下面的示例使用css规则-fx-background-insets: 12;

My guess is that you are not insetting the background content so that there is space in the dialog for the shadow to be shown. That is, you are filling up the dialog with content and not leaving room in the dialog around the content for the effect to be displayed. The example below achieves the insetting with the css rule -fx-background-insets: 12;

更新的示例代码

我将示例代码的修改版本复制到此答案中,以便它不仅仅包含在另一个答案的晦涩的摘要链接中.修改仅使用标准API调用,因为自创建原始答案以来,原始答案中使用的构建器已被弃用.

I copied a modified version of the example code into this answer so that it isn't just contained in an obscure gist link off another answer. The modifications are to just use standard API calls as the builders used in the original answer have been deprecated since the original answer was created.

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());
    }
}

modal-dialog.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缺少您需要的功能(例如下拉阴影支持),则可以提交功能请求针对ControlsFX项目,并在必要时链接回此答案.

Also note that for creating dialogs I highly recommend using the ControlsFX project rather than creating your own dialog system. If ControlsFX lacks features which you require (such as drop shadow support), you can file a feature request for that against the ControlsFX project and link back to this answer if necessary.

这篇关于在未装饰的窗格中投下阴影! JAVAFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆