JavaFX学习笔记(一)
一、简介√\color{#FF7D00}{√}√ 用 Java 语言开发桌面应用,官网√\color{#FF7D00}{√}√ JDK8 中自带 JavaFX 开发包,JDK11 需要手动导入二、创建一个基础应用GUIpublic class LoginWindow extends Application {public void start(Stage primaryStage) throw
·
一、简介
√ \color{#FF7D00}{√} √ 用 Java 语言开发桌面应用,官网
√ \color{#FF7D00}{√} √ JDK8 中自带 JavaFX 开发包,JDK11 需要手动导入
二、创建一个基础应用
- GUI
public class LoginWindow extends Application {
public void start(Stage primaryStage) throws IOException {
Parent parent = FXMLLoader.load(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("ui/login.fxml")));
Scene scene = new Scene(parent);
primaryStage.setScene(scene);
primaryStage.setTitle("测试title");
primaryStage.setHeight(350);
primaryStage.setWidth(425);
// 窗体缩放
primaryStage.setResizable(true);
// 关闭窗体,正常退出jvm
// primaryStage.setOnCloseRequest(event -> {
// System.exit(0);
// });
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
- ui
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<!--- 登录页面 -->
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100" prefWidth="100" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zilong.fx.controller.TestController">
</Pane>
- Controller
public class TestController {
}
- 运行效果
三、打包成exe文件(需要go环境)
- 修改启动类
@SpringBootApplication
public class FxApplication extends Application implements ApplicationRunner {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(FxApplication.class);
application.setHeadless(false);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
launch();
}
@Override
public void start(Stage primaryStage) throws Exception {
Platform.runLater(() -> {
try {
new LoginWindow().start(new Stage());
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
- maven插件
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>go</executable>
<workingDirectory>./packaging</workingDirectory>
<arguments>
<argument>build</argument>
<argument>-o</argument>
<argument>./distribution/${project.build.finalName}.exe</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
- go脚本
package main
import (
"github.com/gonutz/ide/w32"
"os/exec"
)
func main() {
HideConsole()
cmd := exec.Command("./jre/bin/java.exe", "-jar", "F:\\java\\projects\\fx\\target\\fx-0.0.1-SNAPSHOT.jar")
buf, _ := cmd.Output()
println(buf)
}
func HideConsole() {
ShowConsoleAsync(w32.SW_HIDE)
}
func ShowConsole() {
ShowConsoleAsync(w32.SW_SHOW)
}
func ShowConsoleAsync(commandShow uintptr) {
console := w32.GetConsoleWindow()
if console != 0 {
_, consoleProcID := w32.GetWindowThreadProcessId(console)
if w32.GetCurrentProcessId() == consoleProcID {
w32.ShowWindowAsync(console, commandShow)
}
}
}
- install 后生成 .exe 文件,双击运行
代 码 地 址 : \color{red}{代码地址:} 代码地址:https://gitee.com/zilong123666/fx.git
更多推荐
所有评论(0)