【发布时间】:2015-01-21 10:00:54
【问题描述】:
当 JavaFX 浏览器窗口关闭时,如何设置 Java 应用程序关闭?
我有一个基于控制台的应用程序,在主要内容的末尾,系统会询问用户是否要查看网页。如果是,请打开网页。如果不退出应用程序。
据我了解,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);
}
}
【问题讨论】:
标签: java javafx window exit terminate