JavaFX:从main以外的方法调用'Application.launch(args)' [英] JavaFX : invoking ' Application.launch(args) ' from a method other than main

查看:371
本文介绍了JavaFX:从main以外的方法调用'Application.launch(args)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



我可以调用'Application.launch(args); '从主要以外的方法?如果是这样,你可以提供一个例子,记住下面的背景吗?



背景



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



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



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



但我的主要问题是我怎么能/应该称之为'OpenSite。??????????? (); ',包含'Application.launch(args);的方法,如果不是来自我的主?



如果必须从main调用,我怎么能这样做,只有在应用程序的主要部分运行之后,并且仅当用户输入y?



以下是提示用户查看网站的.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;

公共类OpenSite扩展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);

场景sceneOL =新场景(vBoxOF);
primaryStage.setScene(sceneOL);
primaryStage.show();
}
}

第二类

  package mrArray; 


公共类ViewSiteOrExit
{
/ *
*声明字段,
* /
private static int statePrSIF;
private static String enterYOrNPrSSOF;

/ *
*声明方法,
*初始化字段,
* while,test(字段)正在传递执行,
*开关,评估案例with value(field),
*匹配,执行语句,
* 0,第一种情况,提示,y drop to if,重置值,再次使用app,
* n drop to else,增量字段,1,第二种情况,
*调用关闭应用程序的方法,重置字段值以防止双字段调用,
*返回流向调用者以防止使用关闭扫描程序,
* /
public static void viewSitePromptPuSVM()
{
statePrSIF = 0;
while(statePrSIF< 2)
{
switch(statePrSIF)
{
case 0:
System.out.println();
System.out.println([:-)]还有一个问题?);
System.out.println(你想看看甲骨文对我有什么看法吗?);
System.out.println(输入'y'表示是。);
System.out.println(输入'n'表示否。);
休息;
案例1:
goodByePuSVM();
statePrSIF = 0;
返回;
}

enterYOrNPrSSOF = MrArray.scannerOF.next();

if(enterYOrNPrSSOF.equalsIgnoreCase(y))
{
statePrSIF = 0;
System.out.println([:-)]好吧,它很有趣。);
System.out.println(这是Orcale的事情。);
System.out.println(稍后再见!);
OpenSite。??????????();
}
else if(enterYOrNPrSSOF.equalsIgnoreCase(n))
{
statePrSIF ++;
}
}
}

/ *
*声明方法,
*调用方法,显示输出,
*关闭扫描仪,终止,
* /
public static void goodByePuSVM()
{
System.out.println([:-)]好吧,它很有趣。) ;
System.out.println(稍后再见!);

MrArray.scannerOF.close();
}
}


解决方案

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


OpenSite.launch(OpenSite.class);


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



您还可以通过以下答案



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



注释




  • 调用Application.launch()并启动主要阶段 的线程将不会返回,除非舞台已关闭。

  • 确保拨打启动()只需一次


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

解决方案

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.

这篇关于JavaFX:从main以外的方法调用'Application.launch(args)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆