JavaFX笔记(1).创建 Maven+JavaFX+JFoenix 项目

转载 2021-10-21 09:20 Java 29

1. 创建Java项目

创建后的项目结构

2. 添加框架支持

在项目名上右键单击,选择“Add Framework Support"

选择Maven

添加Maven后的项目结构

3. 编辑pom.xml文件

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--组织域名-->
    <groupId>cn.veddy</groupId>
    <!--项目名称-->
    <artifactId>JavaFX-Demo</artifactId>
    <!--项目版本 1.0测试版-->
    <version>1.0-SNAPSHOT</version>
    <!--打包模式 Jar或者War-->
    <packaging>jar</packaging>

    <dependencies>
        <!--Jfoenix依赖-->
        <dependency>
            <groupId>com.jfoenix</groupId>
            <artifactId>jfoenix</artifactId>
            <version>8.0.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

4. 在resources目录下创建fxml配置文件

main.fxml配置文件内容

xml 复制代码
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<GridPane fx:controller="cn.veddy.gui.controller.MyAppController"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
    <Label text="hello wolrd"/>
</GridPane>

其中通过fx:controller来定义对应的事件处理Controller

5. 创建程序入口类MyApp.java

java 复制代码
package cn.veddy.gui.app;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * 程序入口类,载入界面并显示
 * @author Veddy
 * @date 2021/10/13 10:29
 */
public class MyApp extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/main.fxml"));
        primaryStage.setTitle("maven项目");
        Scene scene = new Scene(root, 900, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

6.运行主程序MyApp.java后的运行效果图

7. 最终项目结构

点赞
收藏
关注
转发
本文作者: Veddy
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议进行许可。转载请署名作者且注明文章出处。
文章目录