2020-11-05

2020-11-05  本文已影响0人  Hao_38b9

javafx

1. 简介

Sun公司已于2008年12月05日发布了JavaFX技术的正式版,它使您能利用 JavaFX 编程语言开发互联网应用程序(RIA)。该产品于2007年5月在JavaOne大会上首次对外公布。JavaFX技术主要应用于创建Rich Internet Applications(RIAs)。当前的JavaFX包括JavaFX脚本和JavaFX Mobile(一种运营于行动装置的操作系统),今后JavaFX将包括更多的产品。JavaFX Script编程语言(以下称为JavaFX)是一种declarative, statically typed(声明性的、静态类型)脚本语言。

——百度百科

2. 简述

由于最近接触到一个项目,需要做桌面应用的开发,由于目前主要使用java开发,为了能够使用java强大的类库,和高效的开发效率,于是我在java中寻找了几个GUI开发的技术例如swing,javafx。最后,我选择了javafx。

3. 为什么使用javafx

4. 使用Spring集成javafx

4.1 构建项目

<pre mdtype="fences" cid="n48" lang="xml" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>de.roskenet</groupId>
<artifactId>springboot-javafx-support</artifactId>
<version>2.1.6</version>
</dependency>

</dependencies></pre>

4.2 编写视图层

  1. 编写fxml文件

    1. 使用SceneBuilder来绘制控件
  2. 编写View类,注意fxml和view类需要放到同一个路劲下

    1. 继承 AbstractFxmlView

    2. 使用@FXMLView 注解视图类

    3. [图片上传失败...(image-77af3-1604567269119)]

image-20201105162128516.png

4.3 编写控制层代码

  1. 需要在fxml文件中声明控制类

<pre mdtype="fences" cid="n82" lang="xml" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><AnchorPane fx:id="addSubTitlePane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.medicine.voicerg.mainpj.controller.AddsubtitlesController"></pre>

  1. 编写控制类

    1. 使用@FXMLController 注解标注

    2. 编写一个initialize方法,可以在当前视图初始化的时候被自动调用

4.4 修改启动类

<pre mdtype="fences" cid="n180" lang="java" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.medicine.voicerg.mainpj;

import com.medicine.voicerg.mainpj.controller.LanguageModelDataEditorController;
import com.medicine.voicerg.mainpj.controller.MainController;
import com.medicine.voicerg.mainpj.view.*;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainpjApplication extends AbstractJavaFxApplicationSupport {

public static void main(String[] args) {
launch (MainpjApplication.class, ModelTrainView.class,args);
}

}
​</pre>

5. 控件的声明

  1. 对每个控件的id赋值,然后在控制器类中使用id来声明变量,即可自动导入改控制,来控制控件的行为

[图片上传失败...(image-9cd852-1604567269119)]

image-20201105162902009.png

6. 事件的绑定

  1. 使用fxml绑定事件

  2. 通过控件对象动态绑定事件

上一篇下一篇

猜你喜欢

热点阅读