JavaFX : invoking ' Application.launch(args) &

2019-05-21 12:57发布

问题:

This question already has an answer here:

  • Starting JavaFX from Main method of class which doesn't extend Application 3 answers

Question

Can I call ' Application.launch(args); ' from a method other than main? If so could you provide an example keeping in mind the below context?

Background

I'm build a learning/teaching, command/text application, that teaches the user about arrays. At the end of the main class, after the major application content has ran, I call ' ViewSiteOrExit.viewSitePromptPuSVM(); ', which gives the user the opposition to either: open the Oracle page on arrays, or exit the game.

If the user wishes to view the Oracle page I invoke ' OpenSite.??????????(); ', which will open the page in an FX VBox. If not, exit.

This is the first time I've ever used FX, and I'm tired, so any observations and suggestion, of and for, my code would really help, because I may be missing something.

But my main question is how can/should I call ' OpenSite.??????????(); ', the method containing 'Application.launch(args);, if not from my main?

If it must be called from main, how can I do so, only after the primary parts of the app has ran, and only if the user has input ' y '?

Below is the .java that prompts the user to view the site or exit the game, and the .jave that opens the page.

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;

public class OpenSite extends Application 
{
    VBox vBoxOF = new VBox();

  public static void main(String[] args) 
  {
    Application.launch(args);
  }

  @Override
  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();
  }
}

Second Class

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.??????????();
             }
             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();
     }
}

回答1:

You need to call the static method of the class which extends Application. You can call it from anywhere, not mandatory to call it from main( ) . Use the following :

OpenSite.launch(OpenSite.class);

For background knowledge on how JavaFX Application works, please go through Application JavaDoc. It is very well written and throws a lot of insight on how JavaFX Application is triggered.

You can also go through the following answer

Starting JavaFX from Main method of class which doesn't extend Application

Notes

  • The thread which calls Application.launch() and launches the primary Stage will not return unless the Stage is closed.
  • Make sure you make the call to launch() just once.