[英]How to call javafx from a normal java class
I want to call/open gui/fxml file from a normal class, i'm really having difficulties in finding a way to do this, I've try to instantiate the controller class but i'm not getting start function, Is it even possible to do what i'm trying to do? 我想从普通的类中调用/打开gui / fxml文件,我确实很难找到一种方法来执行此操作,我尝试实例化控制器类,但是我没有启动函数,甚至可以做我想做的事吗?
normal class 普通班
public class ReadXMLFile {
public static void main(String argv[]) {
}
}
fxml document fxml文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="442.0" prefWidth="449.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="gis_map.CitizenScheduledController">
<children>
<TableView layoutX="1.0" layoutY="-1.0" prefHeight="384.0" prefWidth="447.0">
<columns>
<TableColumn prefWidth="75.0" text="CitizenId" />
<TableColumn prefWidth="75.0" text="Name" />
<TableColumn prefWidth="75.0" text="Address" />
<TableColumn prefWidth="75.0" text="Arrival" />
<TableColumn prefWidth="75.0" text="Departure" />
<TableColumn prefWidth="75.0" text="Actions" />
</columns>
</TableView>
<Button layoutX="373.0" layoutY="396.0" mnemonicParsing="false" text="Approve" />
</children>
</AnchorPane>
controllerClass controllerClass
package gis_map;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
public class CitizenScheduledController implements Initializable {
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Is it possible to do what I'm trying to do? 有可能做我想做的事吗?
No (at least, not easily, or in a way that makes sense). 否(至少,不容易,或采取有意义的方式)。
The lifecycle of a JavaFX application is controlled by an instance of the Application
class. JavaFX应用程序的生命周期由Application
类的实例控制。 Furthermore, you need to start the JavaFX toolkit and set the FX application thread running. 此外,您需要启动JavaFX工具包并设置FX应用程序线程运行。 The static Application.launch(...)
method accomplishes this, as well as creating the required instance of your Application
class and calling the appropriate lifecycle methods on it. 静态Application.launch(...)
方法可完成此操作,并创建Application
类的必需实例并在其上调用适当的生命周期方法。
So, just follow the standard pattern and make your ReadXMLFile
class into a subclass of Application
, make the main(...)
method just call launch()
, and define the start()
method to load the FXML, etc. 因此,只需遵循标准模式,将ReadXMLFile
类设置为Application
的子类,使main(...)
方法仅调用launch()
,然后定义start()
方法即可加载FXML,依此类推。
For the sake of completeness: 为了完整性:
Starting with JavaFX 9 it's possible to start up a application without relying on Application.launch
. 从JavaFX 9开始,可以在不依赖Application.launch
的情况下启动应用Application.launch
。 You can use Platform.startup
for this purpose. 您可以为此使用Platform.startup
。 Note that you're only allowed to use Platform.startup
and/or Application.launch
once in your application. 请注意,您只能在应用Application.launch
一次Platform.startup
和/或Application.launch
。
public static void main(String[] args) {
Platform.startup(() -> {
// display empty scene after startup
Scene scene = new Scene(new Pane());
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
});
}
It's usually better to use the normal application lifecycle and use a class extending Application
as entry point and use the init
/ start
methods for any initialisations you'd usually do from the main
method. 通常最好使用正常的应用程序生命周期,并使用扩展Application
的类作为入口点,并对通常要从main
方法执行的任何初始化使用init
/ start
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.