将 java 应用程序设置为在 JavaFX 浏览器 window 关闭时关闭?
set a java application to close when a JavaFX browser window is closed?
如何设置 java 应用程序在 JavaFX 浏览器 window 关闭时关闭?
我有一个基于控制台的应用程序,在主要内容的末尾,系统询问用户是否要查看网页。如果是,打开网页。如果没有退出应用程序。
据我了解,javaFX 应用程序会在 window 被 Xed 时关闭。我的问题是,当 window 关闭时,我的应用程序的非 FX 部分 command/text 似乎没有终止。如何在 FX window 关闭时实现应用程序完全终止。
我将包含两个 classes。一个提示用户查看页面并输入 n,然后终止。另一个 class 打开页面。
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);
}
}
如果您没有什么可保存的,可以在 stage.setOnCloseRequest()
请求上使用 System.exit(0)
。
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.exit(0);
}
});
流线型方法
由于您是从 Java 应用程序调用 JavaFX 应用程序,一旦您关闭 JavaFX 应用程序 Window,控件将 return对于 Java 应用程序,我会推荐以下方法。
在 JavaFX 应用程序中 Class
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
Platform.exit();
}
});
在JavaClass
OpenSite.invokeLaunch();
... // Complete your task
if(!Platform.isFxApplicationThread())
exitApp(); // or System.exit(0);
您应该使用 Lambda 表达式:
primaryStage.setOnCloseRequest(
(WindowEvent we) -> {
Platform.exit();
});
如何设置 java 应用程序在 JavaFX 浏览器 window 关闭时关闭?
我有一个基于控制台的应用程序,在主要内容的末尾,系统询问用户是否要查看网页。如果是,打开网页。如果没有退出应用程序。
据我了解,javaFX 应用程序会在 window 被 Xed 时关闭。我的问题是,当 window 关闭时,我的应用程序的非 FX 部分 command/text 似乎没有终止。如何在 FX window 关闭时实现应用程序完全终止。
我将包含两个 classes。一个提示用户查看页面并输入 n,然后终止。另一个 class 打开页面。
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);
}
}
如果您没有什么可保存的,可以在 stage.setOnCloseRequest()
请求上使用 System.exit(0)
。
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.exit(0);
}
});
流线型方法
由于您是从 Java 应用程序调用 JavaFX 应用程序,一旦您关闭 JavaFX 应用程序 Window,控件将 return对于 Java 应用程序,我会推荐以下方法。
在 JavaFX 应用程序中 Class
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
Platform.exit();
}
});
在JavaClass
OpenSite.invokeLaunch();
... // Complete your task
if(!Platform.isFxApplicationThread())
exitApp(); // or System.exit(0);
您应该使用 Lambda 表达式:
primaryStage.setOnCloseRequest(
(WindowEvent we) -> {
Platform.exit();
});