第四章 JavaFx 文本处理

来源:互联网 发布:网络营销策划文案 编辑:程序博客网 时间:2024/05/22 16:04

参考书籍:《Apress JavaFX.2.0 Introduction by Example Dec 2011》

画文本代码如下:

package drawingtext;import javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.paint.Color;import javafx.scene.text.Text;import javafx.stage.Stage;public class DrawingText extends Application {@Overridepublic void start(Stage stage) throws Exception {stage.setTitle("Drawing Text");Group group = new Group();Scene scene = new Scene(group,400,400,Color.WHITE);//字体起始坐标与内容Text text = new Text(200,200,"Hello JavaFx!");//字体颜色 蓝色text.setFill(Color.BLUE);//字体旋转30度text.setRotate(30);group.getChildren().add(text);stage.setScene(scene);stage.show();}/** * @param args */public static void main(String[] args) {launch(args);}}


画文本执行结果:



改变文本字体代码如下:

package changetextfont;import javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.effect.DropShadow;import javafx.scene.effect.Reflection;import javafx.scene.paint.Color;import javafx.scene.text.Font;import javafx.scene.text.Text;import javafx.stage.Stage;public class ChangeTextFont extends Application {@Overridepublic void start(Stage stage) throws Exception {stage.setTitle("Change Text Font");Group group = new Group();Scene scene = new Scene(group,400,400,Color.WHITE);//设置灰色阴影DropShadow dropShadow = new DropShadow();dropShadow.setOffsetX(2);dropShadow.setOffsetY(2);dropShadow.setColor(Color.GRAY);//设置倒影Reflection reflection = new Reflection();reflection.setFraction(0.5);//Serif字体Text serifText = new Text(50,50,"Hello JavaFx!");serifText.setFont(new Font("Serif",20));serifText.setEffect(dropShadow);group.getChildren().add(serifText);//SanSerif字体Text sanSerifText = new Text(50,100,"Hello JavaFx!");sanSerifText.setFont(new Font("SanSerif",20));sanSerifText.setFill(Color.RED);group.getChildren().add(sanSerifText);//Dialog字体Text dialogText = new Text(50,150,"Hello JavaFx!");dialogText.setFont(new Font("Dialog",20));dialogText.setEffect(reflection);group.getChildren().add(dialogText);stage.setScene(scene);stage.show();}/** * @param args */public static void main(String[] args) {launch(args);}}


改变文本字体执行结果:


原创粉丝点击