原项目来源于:https://github.com/Uahh/Reborn,vue前端 python后台。
本文使用java jetty(内嵌http服务器) vue前端 webview浏览器(JavaFX组件)改成桌面应用,前端沿用原代码,后端使用java jetty改造,并使用javafx的webview浏览器组件显示,并使用graalvm把生成的java包转成原生exe应用。
投胎数据来源于“世界银行World Bank数据库”的出生率(每千人,1960-2020年数据),如果下一次投胎,会出生在哪里?
项目根据最新的人口和出生率信息,计算出了出生在各个国家的概率,以地图的形式展现出来。
世界银行收录了全球各国的经济、金融、人口、环境、教育、医疗等数据,并向社会公开,提供了超过9000个的指标文档。数据地址:https://data.worldbank.org.cn/indicator/SP.DYN.CBRT.IN
程序文件:
程序和源码下载地址:https://21doc.net/java/javagui
项目工程文件
Reborn.java源码
package com.penngo.gui;
import cn.hutool.core.io.FileUtil;
import cn.hutool.JSON.JSONArray;
import cn.hutool.json.JSONUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebErrorEvent;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.util.resource.Resource;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class Reborn extends Application {
private Server server = null;
private WebView webView = null;
private WebEngine webEngine = null;
private TextField urlField = null;
private int port = 8123;
private Stage primaryStage;
public void start(Stage primaryStage){
this.primaryStage = primaryStage;
new Thread(()-> runHttpServer()).start();
webView = new WebView();
webEngine = webView.getEngine();
webEngine.setOnalert((WebEvent<String> wEvent) -> {
System.out.println("alert========: " wEvent.getData());
});
webEngine.setOnError((WebErrorEvent wEvent) -> {
System.out.println("error========: " wEvent.getMessage());
});
webEngine.getLoadWorker().StateProperty()
.addListener(new LoadChangeListener());
new Thread(()->{
try{
TimeUnit.MILLISECONDS.sleep(500);
System.out.println("webView load======");
Platform.runLater(()->{
webEngine.load("http://localhost:" port "/index.html");
});
}
catch(Exception e){
e.printStackTrace();
}
}).start();
BorderPane borderPane = new BorderPane();
urlField = new TextField();
urlField.setOnKeyPressed(keyEvent -> {
System.out.println("load====" urlField.getText());
if(keyEvent.getCode() == KeyCode.ENTER){
webEngine.load(urlField.getText());
}
});
borderPane.setCenter(webView);
borderPane.setBottom(new Label("===="));
Scene scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX");
primaryStage.setWidth(1600);
primaryStage.setHeight(800);
//设置窗口不可拉伸
primaryStage.show();
primaryStage.setOnCloseRequest(event -> {
System.out.println("exit======");
try{
server.stop();
}
catch(Exception e){
e.printStackTrace();
}
Platform.exit();
});
System.out.println( "==============run browser!" );
}
private void runHttpServer(){
try{
System.out.println( "==============run http!" );
server = new Server(port);
//封装静态资源访问路径
ContextHandler contextHandler = new ContextHandler("/");
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setDirAllowed(true);
resourceHandler.setRedirectWelcome(true);
resourceHandler.setDirectoriesListed(true);
File path = new File("templates");
System.out.println("path.toURI().toURL()====" path.getAbsolutePath());
resourceHandler.setBaseResource(Resource.newResource(path.getAbsolutePath()));
resourceHandler.setWelcomeFiles(new String[]{"index.html"});
resourceHandler.setAcceptRanges(true);
contextHandler.setHandler(resourceHandler);
ServletHandler servletHandler = new ServletHandler();
servletHandler.addServletWithMapping(Reborn.RebornServlet.class, "/reborn/getData");
HandlerList handlerList = new HandlerList(){
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
Handler[] handlers = this.getHandlers();
if (handlers != null && this.isStarted()) {
System.out.println("handle============len:" handlers.length);
for(int i = 0; i < handlers.length; i) {
handlers[i].handle(target, baseRequest, request, response);
System.out.println("handle============" request.getRequestURI() "," handlers[i].getClass());
if (baseRequest.isHandled()) {
return;
}
}
}
}
};
handlerList.addHandler(contextHandler);
handlerList.addHandler(servletHandler);
server.setHandler(handlerList);
server.start();
server.join();
System.out.println("runHttpServer===============");
}
catch(Exception e){
System.out.println("runHttpServer error======" e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
class LoadChangeListener implements ChangeListener<Worker.State> {
public void changed(
ObservableValue<? extends Worker.State> ov,
Worker.State oldState, Worker.State newState) {
System.out.println(newState "," webEngine
.executeScript("document.title") ", history=" webEngine.getHistory().getEntries().size());
if (newState == Worker.State.SUCCEEDED) {
String title = (String) webEngine
.executeScript("document.title");
title = title.length() < 10 ? title : title.substring(0, 10);
primaryStage.setTitle(title);
String html = (String) webEngine
.executeScript("document.documentElement.outerHTML");
//System.out.println("html===========" html);
}
}
}
public static class RebornServlet extends HttpServlet
{
@Override
protected void doGet( HttpServletRequest request,
HttpServletResponse response ) throws ServletException,
IOException
{
String json = FileUtil.readUtf8String(new File("templates/data/result.json"));
JSONArray jsonObject = JSONUtil.parseArray(json);
response.setContentType("application/json");
response.getWriter().println(jsonObject);
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().flush();
}
}
}
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved