IDEA创建JAVAFX并打包成exe

wylc123 1年前 ⋅ 878 阅读

1.用idea新建JavaFx项目

2.在sample.xml sence Builder模式下拖入button跟label,命名为btn1和lab1

3.sample.fxml配置如下一定注意加上fx:controller=”sample.Controller”

代码如下:

1)sampe.xml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>

<AnchorPane fx:controller="sample.Controller" prefHeight="338.0" prefWidth="633.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1">

    <children>
        <Pane layoutX="14.0" layoutY="14.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="216.0" prefWidth="480.0">
            <children>
                <Button fx:id="btn1" layoutX="36.0" layoutY="79.0" mnemonicParsing="false" onAction="#onButtonClick" rotate="16.7" text="Button1" />
                <Label fx:id="lab1" layoutX="178.0" layoutY="110.0" text="Label" />
            </children>
        </Pane>
    </children>
</AnchorPane>

2)Controller.java

package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.scene.control.Label;
import javafx.scene.control.Alert;


public class Controller {


    @FXML
    private Button btn1;
    @FXML
    private Label lab1;

    @FXML
    public void onButtonClick(ActionEvent event) {
        lab1.setText("HelloWorld");
        Alert _alert = new Alert(Alert.AlertType.INFORMATION);
        _alert.setTitle("信息");
        _alert.setHeaderText("11111111");
        _alert.setContentText("aaaaaaaa");

        _alert.show();
    }
}

3)Main.java

package sample;

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

import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {

        launch(args);
    }
}

4)在Main.java中运行:

问题:ant.xml文件提前结束

解决:删除.idea中的ant.xml再次运行

5)打包生成.exe

file->project structure->Artifacts->add->JavaFx Application->Form module 'autorun'

设置Application class:  sample.Main

Native bundle:  all

Build->Build Artifacts...

autorn->Rebuild

生成后exe文件在autorun/out/Artifacts/autorun/bundles/autorun/autorun.exe

注意:

要把涉及到的所有jar包添加进来。

运行时出现问题,托盘菜单中文乱码

解决方法:

设置Run/Debug Configurations 

VM options: -Dfile.encoding=GB18030

或:-Dfile.encoding=GBK

参考:

使用Maven构建JavaFX程序(HelloWorld示例)

W3C School JavaFx相关

JavaFx系列教程之一:JavaFx+Springboot+Maven 开发打包教程

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: