Maven搭建JavaFX项目
生活随笔
收集整理的這篇文章主要介紹了
Maven搭建JavaFX项目
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最終效果
點擊按鈕后:
項目結構
完整代碼
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.zxl</groupId><artifactId>fxdemo</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><name>JavaFXDemo</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion></properties><build><finalName>HelloJavaFX</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.6.1</version><configuration><source>${maven.compiler.source}</source><target>${maven.compiler.target}</target></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.0.2</version><configuration><outputDirectory>${project.build.directory}</outputDirectory><archive><manifest><addClasspath>true</addClasspath><classpathPrefix>libs/</classpathPrefix><mainClass>cn.zxl.MainApp</mainClass></manifest></archive></configuration></plugin></plugins></build><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies> </project>MainApp.java
package cn.zxl;import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage;/*** Hello world!*/ public class MainApp extends Application {public static void main(String[] args) {launch(args);}public void start(Stage primaryStage) throws Exception {Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("Main.fxml"));Scene scene = new Scene(root, 600, 500);scene.getStylesheets().add(getClass().getClassLoader().getResource("application.css").toExternalForm());primaryStage.setTitle("Simple JavaFX");primaryStage.setScene(scene);primaryStage.show();} }MainController.java
package cn.zxl;import javafx.fxml.FXML; import javafx.scene.control.Button;import javafx.event.ActionEvent;public class MainController {@FXMLpublic void handlerBtnClick(ActionEvent event) {Button btnSource = (Button) event.getSource();btnSource.setText("I am clicked!");} }Main.fxml
<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.Button?> <?import javafx.scene.layout.BorderPane?><BorderPane xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.zxl.MainController"><center><Button text="Click Me" BorderPane.alignment="CENTER" onAction="#handlerBtnClick" /></center> </BorderPane>application.css
/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */.root{-fx-font-size: 1.2em;-fx-font-family: "Helvetica, Arial, sans-serif";}總結
以上是生活随笔為你收集整理的Maven搭建JavaFX项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven pom resources标
- 下一篇: jfoenix jdk8 pom依赖