0%

JavaFX常用操作

运行

Maven

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.openjfx</groupId>
<artifactId>hellofx</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
<javafx.version>13</javafx.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>${maven.compiler.release}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>org.openjfx.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

使用HelloFX -> Plugins -> compiler -> compiler:compile 编译项目, 使用 HelloFX -> Plugins -> javafx -> javafx:run 执行项目.

Gradle

build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}

repositories {
mavenCentral()
}

dependencies {
}

javafx {
version = "13"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}

mainClassName = 'org.openjfx.MainApp'

使用 hellofx -> Tasks -> build -> build 构建项目

使用 hellofx -> Tasks -> application -> run 运行项目

Jfoenix

jfoenix提供了许多漂亮的组件,与JavaFX本身提供的组件完全兼容。

依赖项:

1
2
3
dependencies {
compile 'com.jfoenix:jfoenix:9.0.10'
}

将jfoenix-components.css放在Resources/css目录下,给Scene设置:

1
scene.getStylesheets().add(Draw.class.getResource("/css/jfoenix-components.css").toExternalForm());

按钮样例

1
2
3
JFXButton githubBtn = new JFXButton("Github");
githubBtn.setMaxWidth(90);
githubBtn.getStyleClass().add("button-raised");

更换窗口图标

将图标置于resources目录下,并:

1
primaryStage.getIcons().add(new Image(Draw.class.getResourceAsStream("/xxx.png")));

为按钮添加图标

将图标置于resources目录下,并:

1
2
3
4
var githubIcon = new ImageView(new Image(Draw.class.getResourceAsStream("/github.png")));
githubIcon.setFitWidth(20);
githubIcon.setFitHeight(20);
btn.setGraphic(githubIcon);