javafx8和ajax

在javafx8使用ajax

js代码:

function getCheckDate() {
	var request=new XMLHttpRequest();
	var url=document.getElementById("ctx").value+"/user/list";
	request.open("POST",url,false);
	request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	request.send("");
	var charString;
	charString=request.responseText;
	return charString;
}
getCheckDate();

java代码:

public class QueryProductionStatus extends Application {
	
	private static final WebView browser = new WebView();  
    private static final WebEngine webEngine = browser.getEngine();
    private Proc proc;
    private static String[] parame;

	/**
     * 数组里面的参数顺序依次是id,代号,版本,登陆用户
     * @param args
     */
    public static void main(String[] args) {
    	parame=new String[]{"aa","bb","cc","dd"};
		launch(args);
		//System.out.println("main方法:"+Thread.currentThread());
	}

	@Override
	public void start(Stage stage) throws Exception {
		setProc();
		String url="http://localhost:8888/shop/user/list";
		//url=getUrl(url);
		System.out.println(url);
		
        stage.setTitle("材料窗口");
        stage.setWidth(1350);
        stage.setHeight(700);
        webEngine.load(url);
        
        webEngine.getLoadWorker().stateProperty().addListener(
   			new ChangeListener<State>() {
				public void changed(ObservableValue ov, State oldState, State newState) {
   					if(newState==ov.getValue()) {
   						closeWindow();
   					} else if(oldState==ov.getValue()) {
   					}
   				}
   			}
       	);
        Group root=new Group();
        root.getChildren().add(browser);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
	}
	
    public void closeWindow() {
    	EventListener listener = new EventListener() {
		    @Override
			public void handleEvent(Event evt) {
		    	getPlanChar();
				Platform.exit();
			}
		};
		Document doc = webEngine.getDocument();
		try {
			Element ele = doc.getElementById("close");
			((EventTarget) ele).addEventListener("click",listener,false);
		} catch(NullPointerException e) {
			e.getMessage();
			e.toString();
		}
    }
    
	public void getPlanChar() {
    	String jsCode=this.getJsCode();
    	//System.out.println(jsCode);
    	String charString= (String) webEngine.executeScript(jsCode);
    	System.out.println(charString);
    }
	
	/**
	 * 使用了反射,更加灵活的应对了Proc实体类的改变,而不用改动业务类的代码
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	 */
	private String getUrl(String url) {
		try {
			String className=proc.getClass().getName();
			Class<?> procClz=Class.forName(className);
			Method[] methods=procClz.getDeclaredMethods();
			for(Method method:methods) {
				String methodName=method.getName();
				if(!methodName.startsWith("get")) continue;
				String fieldName=methodName.substring(3);
				int firstLetterAscii=fieldName.charAt(0);
				
				//把第一个字母转换成小写
				char firstLetter;
				//大写字母的ascii码在65到90之间
				if(firstLetterAscii<=90&&firstLetterAscii>=65) {
					//大写字母+32就是小写字母
					firstLetter=(char)(firstLetterAscii+32);
					fieldName=fieldName.replace((char) firstLetterAscii,firstLetter);
				}
				Object value=method.invoke(proc);
				url+=fieldName+"="+value+"&";
			}
			url=url.substring(0,url.length()-1);
			//System.out.println(url);
			return url;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@SuppressWarnings("resource")
	private String getJsCode() {
    	String code="";
    	try {
    		String jsPath=getClass().getResource("plan.js").getPath();
    		FileReader fr=new FileReader(jsPath);
    		BufferedReader br=new BufferedReader(fr);
    		String c=null;
    		while((c=br.readLine())!=null) {
    			code+=c.trim();
    		}
    		return code;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return null;
    }
	
	private void setProc() {
		proc=new Proc();
		proc.setItemId(parame[0]);
		proc.setProcNo(parame[1]);
		proc.setProcRev(parame[2]);
		proc.setUsername(parame[3]);
	}
}



转载于:https://my.oschina.net/u/1581008/blog/617456

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用JavaFX和echarts绘制折线图,需要遵循以下步骤: 1. 首先在项目中引入echarts库。 2. 在FXML文件中添加一个WebView组件用于显示echarts图表。 3. 在Java代码中使用echarts库绘制折线图,并将图表数据传递给FXML文件中的WebView组件进行显示。 下面是一个简单的示例代码: FXML文件: ```xml <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.layout.*?> <?import javafx.scene.web.*?> <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> <WebView fx:id="webView" prefHeight="400.0" prefWidth="600.0" /> </AnchorPane> ``` Java代码: ```java package sample; import javafx.fxml.FXML; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import netscape.javascript.JSObject; public class Controller { @FXML private WebView webView; public void initialize() { WebEngine webEngine = webView.getEngine(); webEngine.load(getClass().getResource("index.html").toExternalForm()); // 加载echarts网页模板 // 绘制折线图 JSObject window = (JSObject) webEngine.executeScript("window"); window.setMember("data", new int[]{10, 20, 30, 40, 50}); webEngine.executeScript("drawLineChart(data)"); } } ``` index.html文件: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Line Chart</title> <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.2.1/echarts.min.js"></script> </head> <body> <div id="chart" style="width: 100%; height: 100%"></div> <script> function drawLineChart(data) { var chart = echarts.init(document.getElementById('chart')); var option = { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] }, yAxis: { type: 'value' }, series: [{ data: data, type: 'line' }] }; chart.setOption(option); } </script> </body> </html> ``` 在这个示例中,我们使用了echarts库绘制了一个折线图,并将图表数据传递给了FXML文件中的WebView组件进行显示。其中,index.html是一个echarts网页模板,用于显示图表;Controller类中的initialize方法用于绘制折线图并将数据传递给WebView组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值