如何在JavaFX浏览器窗口关闭时设置要关闭的java应用程序?
我有一个基于控制台的应用程序,在主要内容的末尾,询问用户是否要查看网页。如果是,请打开网页。如果不退出应用程序。
据我了解,当窗口被Xed输出时,javaFX应用程序会关闭。我的问题是,我的应用程序的部分不是FX,而是命令/文本在窗口关闭时似乎不会终止。当FX窗口关闭时,如何实现应用程序终止。
我将包括两个班级。一个提示用户查看页面并在输入n中,然后终止。另一个类打开页面。
package mrArray;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
/*
* declare class, subclass of Application.
* declare, instantiate, initialize vBoxOF,
*/
public class OpenSite extends Application
{
VBox vBoxOF = new VBox();
/*
* declare method.
* invoke OpenSite.launch(OpenSite.class);,
* launch a stand-alone application.
*/
public static void invokeLaunch()
{
OpenSite.launch(OpenSite.class);
}
/*
* declare start(Stage primaryStage),
* main entry point for all JavaFX applications,
* declare primaryStageOP,
* the top level JavaFX container.
* invoke setId("root"),
* assign identifier.
* declare, instantiate, initialize webViewBrowserOL,
* as node that manages a webEngineOL & displays its content.
*
* managing one Web page
*/
public void start(Stage primaryStage)
{
vBoxOF.setId("root");
WebView webViewBrowserOL = new WebView();
WebEngine webEngineOL = webViewBrowserOL.getEngine();
String urlSL = "http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html";
webEngineOL.load(urlSL);
vBoxOF.setPadding(new Insets(30, 50, 50, 50));
vBoxOF.setSpacing(10);
vBoxOF.setAlignment(Pos.CENTER);
vBoxOF.getChildren().addAll(webViewBrowserOL);
Scene sceneOL = new Scene(vBoxOF);
primaryStage.setScene(sceneOL);
primaryStage.show();
//ViewSiteOrExit.exitApp();
}
}
package mrArray;
public class ViewSiteOrExit
{
/*
* declare fields,
*/
private static int statePrSIF;
private static String enterYOrNPrSSOF;
/*
* declare method,
* initialize field,
* while, test(field) is passing execute,
* switch, evaluates cases with value(field),
* matching, execute statements,
* 0, first case, prompt, y drop to if, reset value, use app again,
* n drop to else, increment field, 1, second case,
* invoke method to close app, reset field value to prevent double field invocation,
* return flow to caller to prevent use of closing Scanner,
*/
public static void viewSitePromptPuSVM()
{
statePrSIF = 0;
while (statePrSIF < 2)
{
switch (statePrSIF)
{
case 0:
System.out.println();
System.out.println("[:-)] One more question?");
System.out.println("Would you like to see what Oracle has to say about me?");
System.out.println("Enter ' y ' for yes.");
System.out.println("Enter ' n ' for no.");
break;
case 1:
goodByePuSVM();
statePrSIF = 0;
return;
}
enterYOrNPrSSOF = MrArray.scannerOF.next();
if(enterYOrNPrSSOF.equalsIgnoreCase("y"))
{
statePrSIF = 0;
System.out.println("[:-)] Well bud, it's been fun.");
System.out.println("Here is that Orcale thing.");
System.out.println("See ya later!");
OpenSite.invokeLaunch();
}
else if(enterYOrNPrSSOF.equalsIgnoreCase("n"))
{
statePrSIF++;
}
}
}
/*
* declare method,
* invoke methods, display output,
* close Scanner, terminate,
*/
public static void goodByePuSVM()
{
System.out.println("[:-)] Well bud, it's been fun.");
System.out.println("See ya later!");
MrArray.scannerOF.close();
exitApp();
}
public static void exitApp()
{
System.exit(0);
}
}
答案 0 :(得分:0)
如果您无需保存,可以在System.exit(0)
请求时使用stage.setOnCloseRequest()
。
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.exit(0);
}
});
StreamLined方法
由于您从Java应用程序调用JavaFX Application,一旦关闭JavaFX Application Window,控件将返回Java Application,我建议采用以下方法。
在JavaFX应用程序类
中primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
Platform.exit();
}
});
在Java类
中OpenSite.invokeLaunch();
... // Complete your task
if(!Platform.isFxApplicationThread())
exitApp(); // or System.exit(0);
答案 1 :(得分:0)
您应该使用Lambda Expression:
primaryStage.setOnCloseRequest(
(WindowEvent we) -> {
Platform.exit();
});