gpt4 book ai didi

java - 如何从普通的 java 类调用 javafx

转载 作者:行者123 更新时间:2023-12-01 19:57:21 25 4
gpt4 key购买 nike

我想从普通类调用/打开 gui/fxml 文件,我真的很难找到一种方法来做到这一点,我尝试实例化 Controller 类,但我没有启动功能,甚至有可能做我想做的事吗?

普通类

    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>

Controller 类

    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?

不(至少,不容易,或者以一种有意义的方式)。

JavaFX 应用程序的生命周期由 Application 类的实例控制。此外,您需要启动JavaFX工具包并设置FX应用程序线程运行。静态 Application.launch(...) 方法可以完成此任务,并创建所需的 Application 类实例并在其上调用适当的生命周期方法。

因此,只需遵循标准模式,将您的 ReadXMLFile 类创建为 Application 的子类,将 main(...)方法只需调用launch(),并定义start()方法来加载FXML等。

关于java - 如何从普通的 java 类调用 javafx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49117496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com