[英]How to call javafx from a normal java class
我想从普通的类中调用/打开gui / fxml文件,我确实很难找到一种方法来执行此操作,我尝试实例化控制器类,但是我没有启动函数,甚至可以做我想做的事吗?
普通班
public class ReadXMLFile {
public static void main(String argv[]) {
}
}
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
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
}
}
有可能做我想做的事吗?
否(至少,不容易,或采取有意义的方式)。
JavaFX应用程序的生命周期由Application
类的实例控制。 此外,您需要启动JavaFX工具包并设置FX应用程序线程运行。 静态Application.launch(...)
方法可完成此操作,并创建Application
类的必需实例并在其上调用适当的生命周期方法。
因此,只需遵循标准模式,将ReadXMLFile
类设置为Application
的子类,使main(...)
方法仅调用launch()
,然后定义start()
方法即可加载FXML,依此类推。
为了完整性:
从JavaFX 9开始,可以在不依赖Application.launch
的情况下启动应用Application.launch
。 您可以为此使用Platform.startup
。 请注意,您只能在应用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();
});
}
通常最好使用正常的应用程序生命周期,并使用扩展Application
的类作为入口点,并对通常要从main
方法执行的任何初始化使用init
/ start
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.