简体   繁体   English

无法在另一个 class 程序中使用 JavaFX 程序的 static 方法

[英]Not able to use the static method of a JavaFX program in another class

I have created a method to show a dialogue box with suitable information when called.我创建了一种方法来在调用时显示带有适当信息的对话框。 I have created another test class to call and test the method which I have previously mentioned.我创建了另一个测试 class 来调用和测试我之前提到的方法。 When I call the method using this test class it shows some errors which I am not able to solve.当我使用此测试 class 调用该方法时,它显示了一些我无法解决的错误。 Thanks in advance...提前致谢...

    package my_classes;

    import javafx.stage.*;
    import javafx.geometry.Pos;
    import javafx.scene.*;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;

    public class MessageBox
    {
        public static void show(String label, String button, String title, int xaxis, int yaxis)
        {
            Stage window=new Stage();
            Scene sc;
            Button btn=new Button(button);
            btn.setOnAction(e -> window.close());
            Label lbl=new Label(label);
            window.setTitle(title);
            VBox vb=new VBox(50);
            vb.getChildren().addAll(lbl,btn);
            vb.setAlignment(Pos.CENTER);
            sc=new Scene(vb,xaxis,yaxis);
            window.setMaxHeight(yaxis+100);
            window.setMaxWidth(xaxis+100);
            window.setMinHeight(yaxis-100);
            window.setMinWidth(xaxis-100);
            window.initModality(Modality.APPLICATION_MODAL);
            window.setScene(sc);
            window.showAndWait();
        }
    }

    package my_classes;

    public class test
    {
        public static void main(String args[])
        {
            MessageBox.show("This is a piece of text", "OK", "Test", 500, 300);
        }
    }

"image of the error message" “错误消息的图像”

You need to use the normal JavaFX application startup procedure.您需要使用正常的 JavaFX 应用程序启动程序。 That means inheriting from the Application class and using the launch() method in order to properly initialize the JavaFX runtime.这意味着从应用程序class 继承并使用launch()方法来正确初始化 JavaFX 运行时。

For example:例如:

public class MyApplication extends Application {
    /**
     * Initialize the application
     *
     * @param args The application arguments
     */
    public static void main(final String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage primaryStage) {
       // Code here
    }
}

Please take your time to read the JavaFX documentation:请花点时间阅读 JavaFX 文档:
https://openjfx.io/javadoc/15/ https://openjfx.io/javadoc/15/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM