• javafx例子笔记



    javafx是java gui工具。 一般会转换为exe,成为可交互的应用。

    那么来个简单的例子吧。
    先说明一点,javafx不用任何依赖。用maven项目也可以。

    创建过程

    创建一个空pom.xml,内容:

    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0modelVersion>
    	<parent>
    		<groupId>org.springframework.bootgroupId>
    		<artifactId>spring-boot-starter-parentartifactId>
    		<version>2.3.11.RELEASEversion>
    		<relativePath/> 
    	parent>
    	<groupId>com.examplegroupId>
    	<artifactId>javafx-demoartifactId>
    	<version>0.0.1-SNAPSHOTversion>
    	<name>javafx-demoname>
    	<description>Demo project for Spring Bootdescription>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    		<maven.compiler.source>1.8maven.compiler.source>
    		<maven.compiler.target>1.8maven.compiler.target>
    	properties>
    
    	<dependencies>
    		
    		<dependency>
    			<groupId>org.apache.poigroupId>
    			<artifactId>poi-ooxmlartifactId>
    			<version>4.1.2version>
    		dependency>
    
    		
    		<dependency>
    			<groupId>org.apache.commonsgroupId>
    			<artifactId>commons-lang3artifactId>
    			<version>3.12.0version>
    		dependency>
    
    		
    		<dependency>
    			<groupId>com.alibabagroupId>
    			<artifactId>fastjsonartifactId>
    			<version>1.2.75version>
    		dependency>
    
    		
    		<dependency>
    			<groupId>cn.hutoolgroupId>
    			<artifactId>hutool-allartifactId>
    			<version>5.8.14version>
    		dependency>
    	dependencies>
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.bootgroupId>
    				<artifactId>spring-boot-maven-pluginartifactId>
    				<configuration>
    					<finalName>javafx-demofinalName>
    					<outputDirectory>../targetoutputDirectory>
    					<fork>truefork>
    					<executable>trueexecutable>
    				configuration>
    			plugin>
    		plugins>
    	build>
    
    project>
    
    

    创建类:

    public class Main extends Application {
        public static void main(String[] args) {
            launch(args);
        }
        public void start(Stage primaryStage) throws Exception {
            BorderPane root = new BorderPane();
            Label label = new Label("Hello World");
            root.setCenter(label);
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.setWidth(800);
            primaryStage.setHeight(600);
            primaryStage.setTitle("JavaFx Demo");
            primaryStage.show();
        }
    }
    

    在该类右键运行,弹框 hello world 表示成功。
    然后打包,再用exe4j转换为exe,就可以供人使用了。

    javafx独立版

    java8是支持javafx的,但是不太智能。java8之后javafx独立出来,需要单独引入。

    报错 Exception in thread “WindowsNativeRunloopThread” java.lang.NoSuchMethodError:

    at javafx.graphics/com.sun.glass.ui.win.WinApplication.staticScreen_getScreens
    

    详细报错:
    Exception in thread “WindowsNativeRunloopThread” java.lang.NoSuchMethodError:
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.staticScreen_getScreens(Native Method)

    这个报错可费了劲了。最终也没解决,整理下做的尝试。
    1、jdk版本不对,JAVA_HOME设置为jdk11对应路径。
    2、有说修改init方法的
    覆盖类重写方法 com.sun.javafx.application.Application,(报错了,还没到改代码那层)
    在init里面添加内容:

    if (osdetector.os == 'windows') {
    systemProperty "java.library.path", "D:\Program Files\Java\jdk-11.0.21"
    }
    }
    

    3、发现jre不存在
    bin\jlink.exe --module-path jmods --add-modules java.desktop --output jre

    4、javafx maven插件添加内容(实测也无效)

    -Djava.library.path=D:\Program Files\Java\jdk-11.0.21

    注:网上有人说解决掉了,是电脑只能安装一个jdk版本。个人电脑都是用的解压版,应该只用切换java_home即可。卸载代价太大了,还有很多其他项目。

  • 相关阅读:
    力扣 895. 最大频率栈
    微服务技术栈简单介绍,Eureka和Ribbon的引入和使用
    前端的基本介绍
    【MindSpore】CPU可以正常运行的,但是GPU下报错
    网络传输用户层协议详解
    CorelDRAW2024有哪些新功能?如何下载
    FFmpeg转码参数说明及视频转码示例
    【大体思路】rv1126 跑通 yolov5
    WebDAV之π-Disk派盘 + 天天
    Vue基础知识测试
  • 原文地址:https://blog.csdn.net/enthan809882/article/details/139806677