将dropShadow仅添加到网格面板的边框JavaFx 2.2 [英] Add dropShadow only to border of grid pane JavaFx 2.2

查看:530
本文介绍了将dropShadow仅添加到网格面板的边框JavaFx 2.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要仅将阴影添加到网格窗格的边框,而不是内部子元素




这里是显示效果的屏幕图像。

解决方案

使用StackPane并将GridPane放置在其中。



使用CSS为StackPane设计样式,以应用背景颜色,插入和阴影。



请参阅 effect。

  shadowedPane.setEffect(new DropShadow(10,Color.PURPLE)) ; 




为什么工作?


由于设置背景为应用阴影效果的节点提供了清晰的边缘。


为什么DropShadow的工作方式不同,取决于它是否应用在根节点或嵌套容器上?


在根节点或嵌套容器之间的阴影处理中没有真正不同的行为。阴影效果取决于具有应用的效果的项目是否具有透明(或无效)背景。注意,根节点通常填充一个阶段。因此,如果根节点具有不透明的背景颜色并且没有提供插入,则根节点上的阴影将不会被看到,因为它将落在舞台的可见区域之外。






我会猜测发生了什么。我认为阴影效果是做计算节点的外部形状,然后应用阴影。当该节点具有背景颜色时,您将看到节点背景的阴影,这是在提供的图像中看到的。如果节点没有背景颜色,则节点的边缘从子节点计算,所以所有的孩子都得到阴影。



现在发生的是JavaFX 2.2当问题被问及JavaFX 8,JavaFX的默认主题从Caspian移动到摩德纳。对于里海窗格默认没有任何背景颜色。然而对于摩德纳,默认情况下,窗格有一个略带灰白色的背景颜色。这意味着在Java 8中默认情况下不会在网格窗格中出现原始海报的模糊文本,因为GridPane的背景将被遮蔽,而不是内部文本被遮蔽。这可以通过运行以下程序并更改注释掉的行来设置样式表为Caspian来验证:

  import javafx。应用程序; 
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Shadowed extends Application {
public static void main(String [] args){launch(args); }

@Override
public void start(Stage Stage)throws Exception {
// setUserAgentStylesheet(STYLESHEET_CASPIAN);
Label clear = new Label(Clear,with no shadow);
GridPane shadowedPane = new GridPane();
shadowedPane.add(clear,0,0);
GridPane.setHalignment(clear,HPos.CENTER);
GridPane.setValignment(clear,VPos.CENTER);
GridPane.setHgrow(clear,Priority.ALWAYS);
GridPane.setVgrow(clear,Priority.ALWAYS);
shadowedPane.setStyle(
//-fx-background-color:transparent;+
//-fx-background-color:palegreen;+
fx-background-insets:10;+
-fx-background-radius:10;
);
shadowedPane.setPrefSize(200,50);
shadowedPane.setEffect(new DropShadow(10,Color.PURPLE));
stage.setScene(new Scene(shadowedPane));
stage.show();
}
}


Want to add drop shadow only to the border of Grid pane not to inner child elements

here is the image of screen showing the effect.

解决方案

Use a StackPane and place your GridPane in it.

Style your StackPane with CSS to apply a background color, insets and a drop shadow.

See Drop Shadow in an undecorated Pane! JAVAFX for some sample CSS.

Here is a small standalone sample app (used Java 8b120 on OS X 10.9), to demonstrate the effect:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Shadowed extends Application {
    public static void main(String[] args) { launch(args); }

    @Override
    public void start(Stage stage) throws Exception {
        Label clear = new Label("Clear, with no shadow");
        StackPane shadowedPane = new StackPane(clear);
        shadowedPane.setStyle(
                "-fx-background-color: palegreen; " +
                "-fx-background-insets: 10; " +
                "-fx-background-radius: 10; " +
                "-fx-effect: dropshadow(three-pass-box, purple, 10, 0, 0, 0);"
        );
        shadowedPane.setPrefSize(200, 50);
        stage.setScene(new Scene(shadowedPane));
        stage.show();
    }
}

Addressing additional questions

Is CSS the only option?

No, this could be done in code instead of CSS, by using the DropShadow effect.

shadowedPane.setEffect(new DropShadow(10, Color.PURPLE));

Why does it work?

Because setting a background provides a clear edge to the node to which the shadow effect is applied.

Why does DropShadow work differently depending on if it's applied on a root node or a nested container?

There is no real differing behavior in the drop shadow processing between a root node or nested container. The drop shadow effect depends on whether the item having the effect applied has a transparent (or null) background or not. Note though that a root node usually fills a stage. So, if the root node has a non-transparent background color and no insets are supplied, then the shadow on the root node will not be seen as it will fall outside the visible area of the stage.


I'll offer a guess at what is happening. I think what the drop shadow effect is doing is calculating the exterior shape of the node, then applying the shadow to it. When that node has a background color, then the you will see the shadow of the node background, which is what is seen in the image supplied. If the node has no background color, then the edges of the nodes are calculated from the child nodes, so all of the children get shadows.

Now what happened is that between JavaFX 2.2 when the question was asked and JavaFX 8, the default theme for JavaFX moved from Caspian to Modena. With Caspian panes did not have any background color by default. However for Modena, panes do have a slight off-white background color by default. This means that the original poster's blurry text inside the grid pane won't occur by default in Java 8, as the GridPane will have a background which is shadowed, instead of interior text being shadowed. This can be verified via the running the following program and varying the commented out line for setting the stylesheet to Caspian:

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Shadowed extends Application {
    public static void main(String[] args) { launch(args); }

    @Override
    public void start(Stage stage) throws Exception {
//        setUserAgentStylesheet(STYLESHEET_CASPIAN);
        Label clear = new Label("Clear, with no shadow");
        GridPane shadowedPane = new GridPane();
        shadowedPane.add(clear, 0, 0);
        GridPane.setHalignment(clear, HPos.CENTER);
        GridPane.setValignment(clear, VPos.CENTER);
        GridPane.setHgrow(clear, Priority.ALWAYS);
        GridPane.setVgrow(clear, Priority.ALWAYS);
        shadowedPane.setStyle(
//                "-fx-background-color: transparent; " +
//                "-fx-background-color: palegreen; " +
                "-fx-background-insets: 10; " +
                "-fx-background-radius: 10; "
        );
        shadowedPane.setPrefSize(200, 50);
        shadowedPane.setEffect(new DropShadow(10, Color.PURPLE));
        stage.setScene(new Scene(shadowedPane));
        stage.show();
    }
}

这篇关于将dropShadow仅添加到网格面板的边框JavaFx 2.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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