JAVAFX嵌入html页面
javafx嵌入html页面
使用fjavafx嵌入html页面需要先创建一个fxml页面,页面中放入一个WebView,具体嵌入的代码在后台fxml对应的controller中操作
FXML页面代码
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.web.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.sky.ht.view.setting.SettingController">
<children>
<WebView fx:id="webView" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
1
2
3
4
5
6
7
8
9
10
11
12
fx:controller=“com.sky.ht.view.setting.SettingController”>
对应的是这个fxml的后台controller
后台controller
import java.net.URL;
import java.util.ResourceBundle;
import com.sky.ht.Main;
import com.sky.ht.utils.AsrUtils;
import com.sky.ht.view.BaseView;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class SettingController extends BaseView implements Initializable {
@FXML
private WebView webView;
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
final WebEngine webengine = webView.getEngine();
webengine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) { // 页面加载成功
}
}
});
//AsrUtils.getInstance().url是我放在工具类的要嵌入的html页面路径,直接替换即可
webengine.load(AsrUtils.getInstance().url);
}
@Override
public void setMain(Main main) {
this.main = main;
//设置页面的高度
webView.setPrefHeight(636);
}
}
---------------------
【转载,仅作分享,侵删】
作者:Lele___
原文:https://blog.csdn.net/lele___/article/details/88424389
版权声明:本文为博主原创文章,转载请附上博文链接!
|
|