天天看點

IDEA建立JAVAFX并打包成exe

IDEA版本2017 

建立項目 

在xml頁面拖入button跟label,命名為btn1和lab1 

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

<?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>

Controller.class代碼如下

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();

    }

}

Main.class如下

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);

    }

}

運作結果 

下面開始打包成exe 

成功!!!!!!!