目录

  • 概述
  • 代码
  • 结果
  • 总结
  • JavaFX应用
  • 舞台和场景
  • 显示图像
  • 显示文字
  • 将文本节点作为组
  • 动画文本向上滚动


概述

JavaFX是用于构建富互联网应用程序的Java库。使用JavaFX开发的应用程序可以在各种设备上运行,如台式计算机,手机,物联网设备,平板电脑等。这一章主要是介绍如何开始JavaFX开发,并开发一个简单的JavaFX桌面入门小项目。其内容是在小窗口中进行文字循环滚动播放及图片背景。

代码

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

import javafx.animation.Interpolator;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.util.Duration;
/**
 * javaFX的Hello World例子
 */
public class HelloEarthRiseMain extends Application {
  
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage stage) {
        String message
                = "Earthrise at Christmas: "
                + "[Forty] years ago this Christmas, a turbulent world "
                + "looked to the heavens for a unique view of our home "
                + "planet. This photo of Earthrise over the lunar horizon "
                + "was taken by the Apollo 8 crew in December 1968, showing "
                + "Earth for the first time as it appears from deep space. "
                + "Astronauts Frank Borman, Jim Lovell and William Anders "
                + "had become the first humans to leave Earth orbit, "
                + "entering lunar orbit on Christmas Eve. In a historic live "
                + "broadcast that night, the crew took turns reading from "
                + "the Book of Genesis, closing with a holiday wish from "
                + "Commander Borman: \"We close with good night, good luck, "
                + "a Merry Christmas, and God bless all of you -- all of "
                + "you on the good Earth.\"";
// 定义文本对象
        Text textRef = new Text(message);
        textRef.setLayoutY(100);
        textRef.setTextOrigin(VPos.TOP);
        //设置字体布局
        textRef.setTextAlignment(TextAlignment.JUSTIFY);
        textRef.setWrappingWidth(400);
        //设置颜色
        textRef.setFill(Color.rgb(187, 195, 107));
        //设置字体样式
        textRef.setFont(Font.font("SansSerif", FontWeight.BOLD, 24));
// 为文本对象提供一个自动的滚动条
        TranslateTransition transTransition = new TranslateTransition(new Duration(75000), textRef);
        transTransition.setToY(-820);
        transTransition.setInterpolator(Interpolator.LINEAR);
        transTransition.setCycleCount(Timeline.INDEFINITE);
// 提供一个图片视图包含一个图片
        Image image = new Image ("/images/earthrise.jpg");
        ImageView imageView = new ImageView(image);
// 创建一个包含文字引用的组
        Group textGroup = new Group(textRef);
        textGroup.setLayoutX(50);
        textGroup.setLayoutY(180);
        textGroup.setClip(new Rectangle(430, 85));
//用组将图片视图和文本组集成起来
        Group root = new Group(imageView, textGroup);
        Scene scene = new Scene(root, 470, 370);
        stage.setScene(scene);
        stage.setTitle("Hello Earthrise");
        stage.show();
//启动自动滚动条
        transTransition.play();
    }
}

结果

javafx快速开发框架 javafx开源项目_JavaFX

总结

JavaFX应用

JavaFX项目一定会继承抽象类javafx.application.Application,并在主方法中调用Application类的静态方法launch(args),实现JavaFX程序的启动。

舞台和场景

不管应用部署到桌面还是嵌入式系统,或者其他设备,Stage包含JavaFX应用的用户界面。例如在桌面中,一个Stage有它自己的最高级的窗口,包含标题栏及边框。Stage的初始化是通过JavaFX运行时完成的,通过start()方法传递给我们使用,Stage类有一系列的属性和方法。

  • 一个场景Scene对象包含用户接口中的图像节点
  • 当应用发布到桌面时,在窗口的上方显示一个标题
  • 显示整个舞台

显示图像

如下列代码所示,显示图像需要使用图像视图实例与图像实例。"图像实例"可识别图像资源,并通过URL 加载图像资源。

// 提供一个图片视图包含一个图片
        Image image = new Image ("/images/earthrise.jpg");
        ImageView imageView = new ImageView(image);

显示文字

正如示例中所示,文本实例包含许多可以修改的属性。可以查阅JavaFX API文档明确属性的使用。Text实例也是一个节点,它具有节点的一切属性。

Text textRef = new Text(message);

文本对象在竖直方向上平移100像素

textRef.setLayoutY(100);

将文本节点作为组

JavaFX的强大图形特性之一就是场景图,它由一个图形数组成,然后可以将值分配给位于分层中的组的属性,以及所包含的节点组将受到影响。在demo中使用组来包含文本节点并进行剪辑

Group textGroup = new Group(textRef);
 		//指定组距离窗口左边50像素,距离上边180像素
        textGroup.setLayoutX(50);
        textGroup.setLayoutY(180);
        textGroup.setClip(new Rectangle(430, 85));

动画文本向上滚动

这个动画转换实例通过文本引用对文件节点沿着y方向从100px到-820px,历时75s,并对动画转换设置插值属性为线型,设置循环次数为无限次。

TranslateTransition transTransition = new TranslateTransition(new Duration(75000), textRef);
        transTransition.setToY(-820);
        transTransition.setInterpolator(Interpolator.LINEAR);
        transTransition.setCycleCount(Timeline.INDEFINITE);