简体   繁体   中英

JavaFX - scene import from another .java file

I am trying to call a popup windows from "jfxmessagebox".jar and it is working well!

The only problem if I move this code to another .java file, it is missing my scene. So I need to import the scene somehow and get the rights to use this, but I don't know the exact statement to do that.

My code is:

    public void start(Stage Main) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
    Scene scene = new Scene(root);

    String css = program.class.getResource("Style.css").toExternalForm();   
    scene.getStylesheets().add(css);

    main.setScene(scene);
    main.setTitle("Application");
    main.setWidth(800);
    main.setHeight(600);

          main.show();

       MessageBox.show(main,"welcome to my application!");

}

So it is working fine, but if I copy this line:

MessageBox.show(main,"welcome to my application!");

to another .java file at the same package, the "main" word is underlined with red, and says "cannot find symbol"

How should I implement?

One solution is to write it as a STATIC but do not use it, because it is the easiest but the bad way.

The other part is to call a method from the .java file and give them the rights. It is not so difficult either so I recommend this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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