oracle JavaFX:从 main 以外的方法调用“Application.launch(args)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28062578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:42:10  来源:igfitidea点击:

JavaFX : invoking ' Application.launch(args) ' from a method other than main

javaoraclejavafxmaininvoke

提问by ReedWilliams19842004

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?

我可以调用'Application.launch(args); ' 来自 main 以外的方法?如果是这样,您能否提供一个示例并记住以下上下文?

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.

我正在构建一个学习/教学、命令/文本应用程序,向用户介绍数组。在主类结束时,在主要应用程序内容运行后,我调用' ViewSiteOrExit.viewSitePromptPuSVM(); ',这让用户反对:打开阵列上的 Oracle 页面,或退出游戏。

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

如果用户希望查看 Oracle 页面,我会调用 'OpenSite.??????????(); ',这将在 FX VBox 中打开页面。如果没有,退出。

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.

这是我第一次使用 FX,我很累,所以我的代码的任何观察和建议都会真正有帮助,因为我可能会遗漏一些东西。

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

但我的主要问题是我如何/应该如何调用'OpenSite.??????????(); ',包含 'Application.launch(args); 的方法,如果不是来自我的主要?

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 '?

如果必须从 main 调用它,我该怎么做,只有在应用程序的主要部分运行之后,并且只有在用户输入“y”时?

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

下面是提示用户查看站点或退出游戏的 .java,以及打开页面的 .jave。

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

采纳答案by ItachiUchiha

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 :

您需要调用扩展应用程序的类的静态方法。您可以从任何地方调用它,而不是强制从main( ). 使用以下内容:

OpenSite.launch(OpenSite.class);

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.

有关 JavaFX 应用程序如何工作的背景知识,请参阅Application JavaDoc。它写得非常好,对如何触发 JavaFX 应用程序提供了很多见解。

You can also go through the following answer

您也可以通过以下答案

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

从不扩展应用程序的类的 Main 方法启动 JavaFX

Notes

笔记

  • The thread which calls Application.launch() and launches the primary Stagewill not returnunless the Stage is closed.
  • Make sure you make the call to launch()just once.
  • 除非舞台关闭,否则调用 Application.launch() 并启动 的线程primary Stage将不会返回
  • 确保launch()只调用一次