首先是點擊按鈕彈出兩個窗口的實現(xiàn)
**`
try {
//點擊按鈕正常跳轉fxml頁面的邏輯
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/answer/AnswerView.fxml"));
AnchorPane fingerView = (AnchorPane) loader.load();
AnswerController controller = loader.getController();
controller.rootVbox=rootVbox;
controller.setMain(main);
//如果當前存在第二個窗口,先關閉
Stage stage2 = StageManager.STAGE.get("second");
if(stage2 !=null) {
stage2.close();
}
FXMLLoader loader2 = new FXMLLoader();
Stage stage = new Stage();
AsrUtils.getInstance().url=AsrUtils.getInstance().proMap.get("address").toString()+"/assessmentInHeart/toPsychologicalTest?id="+AsrUtils.getInstance().id+"&article_id=1&type=0&returnAddress=detailed";
loader2.setLocation(Main.class.getResource("view/answer/Guide.fxml"));
AnchorPane yindao = (AnchorPane) loader2.load();
GuideController controller2 = loader2.getController();
//將第二個窗口放入管理類中
StageManager.STAGE.put("second", stage);
//
rootVbox.getChildren().add(fingerView);
controller2.setMain(main);
stage.setScene(new Scene(yindao));
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
上文中StageManager.STAGE.put(“second”, stage);這個是我創(chuàng)建了一個stage管理類
public class StageManager {
//Stage容器存儲第二個窗口,用來點擊第一個窗口中的按鈕時方便操作第二個窗口
public static Map<String, Stage> STAGE=new HashMap<String, Stage>();
//controller管理類實際應用中其實沒用到
public static Map<String, Object> CONTROLLER=new HashMap<String, Object>();
}
現(xiàn)在已經實現(xiàn)點擊按鈕彈出兩個窗口了,第二個窗口可以手動拖動到雙屏的另外一個屏幕(雙屏機器正常都可以設置兩個屏幕顯示不同東西,百度一下,很簡單的)。
點擊第一個窗口中的按鈕,如果第一個窗口使用的fxml頁面,在后臺controller中可以直接獲取到button,我這里第一個窗口是嵌入的html頁面,所以先給html頁面?zhèn)鬟^去回調的對象
public class ArchivesController extends BaseView implements Initializable {
@FXML
private WebView webView;
private WebEngine webEngine;
private OpenAppUtil openAppUtil = new OpenAppUtil();
@Override
public void initialize(URL location, ResourceBundle resources) {
if(webView != null) {
}else {
webView= new WebView();
}
if(webEngine != null) {
}else {
webEngine = webView.getEngine();
}
webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) { // 頁面加載成功
JSObject win = (JSObject) webEngine.executeScript("window");
//把頁面回調的對象傳到html頁面
win.setMember("openAppUtil", openAppUtil);
}
}
});
//加載html頁面AsrUtils.getInstance().proMap.get("address").toString()是我再配置文件中定義的html訪問的前綴
webEngine.load(AsrUtils.getInstance().proMap.get("address").toString()+"/test/index");
}
@Override
public void setMain(Main main) {
this.main = main;
//設置頁面的高度可以寫死,可以忽略
webView.setPrefHeight(main.rootLayout.getHeight() - 70);
}
//html 頁面回調方法
public class OpenAppUtil {
public void toFxml() {
//從stage容器中取出第二個窗口,這里就可以操作第二個窗口了
Stage stage2 = StageManager.STAGE.get("second");
stage2.close();
try {
//操作第二窗口去加載一個頁面
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/answer/AnswerView.fxml"));
AnchorPane fingerView = (AnchorPane) loader.load();
rootVbox.getChildren().add(fingerView);
AnswerController controller = loader.getController();
controller.setMain(main);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
html頁面回調javafx的代碼直接使用openAppUtil.自定義的方法即可。
到這里基本上就實現(xiàn)了,在第一個窗口中操作實時刷新第二個窗口中的頁面,如果有任何問題歡迎咨詢,我就是一個小白,如果文章中有誤導之處,還請批評指正。
本站文章版權歸原作者及原出處所有 。內容為作者個人觀點, 并不代表本站贊同其觀點和對其真實性負責,本站只提供參考并不構成任何投資及應用建議。本站是一個個人學習交流的平臺,網站上部分文章為轉載,并不用于任何商業(yè)目的,我們已經盡可能的對作者和來源進行了通告,但是能力有限或疏忽,造成漏登,請及時聯(lián)系我們,我們將根據著作權人的要求,立即更正或者刪除有關內容。本站擁有對此聲明的最終解釋權。