有时候发现,关掉了窗口,但是发现进程列表里面还有这个进程,因此在关闭的时候在kill一次代码可以参考
package sample.main;import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;import java.io.IOException;
import java.lang.management.ManagementFactory;public class allManager extends Application {@Overridepublic void start(Stage primaryStage) throws Exception{Parent root = null;FXMLLoader loader = new FXMLLoader();try {loader.setLocation(getClass().getResource("/allManager.fxml"));root = loader.load();
// primaryStage.initModality(Modality.WINDOW_MODAL);Scene scene = null;scene = new Scene(root, 600, 400);String name = ManagementFactory.getRuntimeMXBean().getName();System.out.println(name);String pid = name.split("@")[0];System.out.println("Pid is:" + pid);primaryStage.getIcons().add(new Image("/uncleW.jpeg"));primaryStage.setTitle("allManager ="+pid);primaryStage.setScene(scene);primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {@Overridepublic void handle(WindowEvent event) {try {// 使用Runtime类执行taskkill命令Runtime.getRuntime().exec("taskkill /PID " + pid + " /F");System.out.println("进程已被杀死");} catch (IOException e) {System.out.println("杀死进程失败: " + e.getMessage());}}});primaryStage.show();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {launch(args);}
}