如何在不将Java库文件移至系统的情况下使程序可运行

时间:2019-07-07 15:27:21

标签: java javafx java-11

我想知道为什么Swing中的程序可以在装有Java的每台机器上运行,而JavaFX程序却不能。如果要在JavaFX中运行程序,则必须将一些JavaFX库文件复制到here之类的JDK文件夹中。问题是:我不想强迫人们将某些文件移动到任何文件夹中!我希望他们能够通过单击该程序来运行该程序,而无需执行其他任何操作!我想简单点。 Java中的其他程序不会强迫我这样做,那么如何使我的程序像其他人一样?我该怎么办?这是一些代码示例。

Main.java

package fxTutorial;

public class Main {
    public static void main(String[] args){
        Demo.main(args);
    }
}

Demo.java

package fxTutorial;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Demo extends Application implements EventHandler {

    Button button = new Button();
    public static void main(String[] args){
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Application");
        button.setText("Button");
        button.setOnAction(this);

        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(button);
        Scene scene = new Scene(stackPane,400,500);
        primaryStage.setScene(scene);
        primaryStage.show();

    }


    @Override
    public void handle(Event event) {
        if(event.getSource()==button)
            //Do something, like background changing - Whatever
    }
}

module-info.java

module Demo {
    requires javafx.fxml;
    requires javafx.controls;

    opens fxTutorial;
}

0 个答案:

没有答案