Maven搭建JavaFX项目

清疚 2023-10-04 14:17 34阅读 0赞

最终效果

在这里插入图片描述
点击按钮后:
在这里插入图片描述

项目结构

在这里插入图片描述

完整代码

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>cn.zxl</groupId>
  5. <artifactId>fxdemo</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>JavaFXDemo</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <maven.compiler.source>1.8</maven.compiler.source>
  13. <maven.compiler.target>1.8</maven.compiler.target>
  14. <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
  15. </properties>
  16. <build>
  17. <finalName>HelloJavaFX</finalName>
  18. <plugins>
  19. <plugin>
  20. <groupId>org.apache.maven.plugins</groupId>
  21. <artifactId>maven-compiler-plugin</artifactId>
  22. <version>3.6.1</version>
  23. <configuration>
  24. <source>${maven.compiler.source}</source>
  25. <target>${maven.compiler.target}</target>
  26. </configuration>
  27. </plugin>
  28. <plugin>
  29. <groupId>org.apache.maven.plugins</groupId>
  30. <artifactId>maven-jar-plugin</artifactId>
  31. <version>3.0.2</version>
  32. <configuration>
  33. <outputDirectory>${project.build.directory}</outputDirectory>
  34. <archive>
  35. <manifest>
  36. <addClasspath>true</addClasspath>
  37. <classpathPrefix>libs/</classpathPrefix>
  38. <mainClass>cn.zxl.MainApp</mainClass>
  39. </manifest>
  40. </archive>
  41. </configuration>
  42. </plugin>
  43. </plugins>
  44. </build>
  45. <dependencies>
  46. <dependency>
  47. <groupId>junit</groupId>
  48. <artifactId>junit</artifactId>
  49. <version>4.12</version>
  50. <scope>test</scope>
  51. </dependency>
  52. </dependencies>
  53. </project>

MainApp.java

  1. package cn.zxl;
  2. import javafx.application.Application;
  3. import javafx.fxml.FXMLLoader;
  4. import javafx.scene.Parent;
  5. import javafx.scene.Scene;
  6. import javafx.stage.Stage;
  7. /**
  8. * Hello world!
  9. */
  10. public class MainApp extends Application {
  11. public static void main(String[] args) {
  12. launch(args);
  13. }
  14. public void start(Stage primaryStage) throws Exception {
  15. Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("Main.fxml"));
  16. Scene scene = new Scene(root, 600, 500);
  17. scene.getStylesheets().add(getClass().getClassLoader().getResource("application.css").toExternalForm());
  18. primaryStage.setTitle("Simple JavaFX");
  19. primaryStage.setScene(scene);
  20. primaryStage.show();
  21. }
  22. }

MainController.java

  1. package cn.zxl;
  2. import javafx.fxml.FXML;
  3. import javafx.scene.control.Button;
  4. import javafx.event.ActionEvent;
  5. public class MainController {
  6. @FXML
  7. public void handlerBtnClick(ActionEvent event) {
  8. Button btnSource = (Button) event.getSource();
  9. btnSource.setText("I am clicked!");
  10. }
  11. }

Main.fxml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.Button?>
  3. <?import javafx.scene.layout.BorderPane?>
  4. <BorderPane xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.zxl.MainController">
  5. <center>
  6. <Button text="Click Me" BorderPane.alignment="CENTER" onAction="#handlerBtnClick" />
  7. </center>
  8. </BorderPane>

application.css

  1. /* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */
  2. .root{
  3. -fx-font-size: 1.2em;
  4. -fx-font-family: "Helvetica, Arial, sans-serif";
  5. }

发表评论

表情:
评论列表 (有 0 条评论,34人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Maven+SSM项目

    最近再做一个项目,需要用到maven 和 SSM搭建项目,这里记录一下,搭建的全过程,也踩了好多坑,做个记录。 这里是项目的地址: [https://pan.baidu.co