如何在 JavaFX 中向文本节点添加内阴影效果?
你可以使用 setEffect() 方法向 JavaFX 中的任何节点对象添加效果。此方法接受 Effect 类的对象并将其添加到当前节点。
javafx.scene.effect.InnerShadow 类表示内阴影效果。此效果利用指定参数(颜色、偏移量、半径)在其边缘内部呈现给定内容的阴影。
为文本节点添加反射效果:
实例化 Text 类,将基本 x、y 坐标(位置)和文本字符串作为构造函数的参数传入。
设置所需的属性,如字体、描边等。
通过实例化 InnerShadow 类来创建内阴影效果。
使用 setEffect() 方法将创建的效果设置为文本节点。
最后,将创建的文本节点添加到 Group 对象。
示例
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.InnerShadow;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class InnerShadowEffectExample extends Application {
public void start(Stage stage) throws FileNotFoundException {
//Creating a text object
String str = "Tutorialspoint";
Text text = new Text(30.0, 100.0, str);
//Setting the font
Font font = Font.font("Brush Script MT", FontWeight.BOLD, 110);
text.setFont(font);
//Setting color of the text
text.setFill(Color.BLUEVIOLET);
//Creating the inner shadow effect
InnerShadow shadow = new InnerShadow();
shadow.setOffsetX(8.0);
shadow.setOffsetY(8.0);
//Setting the effect to the text
text.setEffect(shadow);
//Setting the stage
Group root = new Group(text);
Scene scene = new Scene(root, 595, 150, Color.BEIGE);
stage.setTitle("Inner Shadow Effect");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP