gpt4 book ai didi

java - Maven 构建后某些场景未加载

转载 作者:太空狗 更新时间:2023-10-29 22:35:44 25 4
gpt4 key购买 nike

我正在 netbeans 中开发 JavaFx 应用程序,在 netbeans 中,该项目正在构建并运行良好。

我从我的项目中创建了一个构建(mvn 包),它没有错误地完成但是当我启动程序时它没有加载所有场景并且 FXMLLoader 在这种情况下返回空值。

同一文件夹中的所有 .fxml 文件。

public class JavaFXApplication extends Application {

public static final String TOOLBAR_MAIN = "toolbarMain";
public static final String TOOLBAR_MAIN_FXML = "/fxml/ToolbarMain.fxml";
public static final String TOOLBAR_SUB = "toolbarSub";
public static final String TOOLBAR_SUB_FXML = "/fxml/ToolbarSub.fxml";

public static final String NEW_SESSION_PANEL = "newSession";
public static final String NEW_SESSION_PANEL_FXML = "/fxml/NewSessionPanel.fxml";
public static final String OPEN_SESSION_PANEL = "openSession";
public static final String OPEN_SESSION_PANEL_FXML = "/fxml/OpenSessionPanel.fxml";
public static final String CONNECTIONS_PANEL = "connections";
public static final String CONNECTIONS_PANEL_FXML = "/fxml/ConnectionsPanel.fxml";
public static final String LOGS_PANEL = "logs";
public static final String LOGS_PANEL_FXML = "/fxml/LogsPanel.fxml";
public static final String EXCEPTIONS_PANEL = "exceptions";
public static final String EXCEPTIONS_PANEL_FXML = "/fxml/ExceptionsPanel.fxml";
public static final String MESSAGES_PANEL = "messages";
public static final String MESSAGES_PANEL_FXML = "/fxml/MessagesPanel.fxml";

public static ScreensController menuContainer = new ScreensController();
public static ScreensController contentContainer = new ScreensController();

public static ServerService server = new ServerService();

public static Stage STAGE;

@Override
public void start(Stage primaryStage) throws Exception {

STAGE = primaryStage;

primaryStage.setOnCloseRequest((WindowEvent t) -> {
if (server.isRunning()) {
server.cancel();
}
});

menuContainer.loadScreen(JavaFXApplication.TOOLBAR_MAIN,
JavaFXApplication.TOOLBAR_MAIN_FXML);
menuContainer.loadScreen(JavaFXApplication.TOOLBAR_SUB,
JavaFXApplication.TOOLBAR_SUB_FXML);

contentContainer.loadScreen(JavaFXApplication.NEW_SESSION_PANEL,
JavaFXApplication.NEW_SESSION_PANEL_FXML);
contentContainer.loadScreen(JavaFXApplication.OPEN_SESSION_PANEL,
JavaFXApplication.OPEN_SESSION_PANEL_FXML);
contentContainer.loadScreen(JavaFXApplication.NEW_SESSION_PANEL,
JavaFXApplication.NEW_SESSION_PANEL_FXML);
contentContainer.loadScreen(JavaFXApplication.CONNECTIONS_PANEL,
JavaFXApplication.CONNECTIONS_PANEL_FXML);
contentContainer.loadScreen(JavaFXApplication.LOGS_PANEL,
JavaFXApplication.LOGS_PANEL_FXML);
contentContainer.loadScreen(JavaFXApplication.EXCEPTIONS_PANEL,
JavaFXApplication.EXCEPTIONS_PANEL_FXML);
contentContainer.loadScreen(JavaFXApplication.MESSAGES_PANEL,
JavaFXApplication.MESSAGES_PANEL_FXML);
menuContainer.setScreen(JavaFXApplication.TOOLBAR_MAIN);
contentContainer.setScreen(JavaFXApplication.NEW_SESSION_PANEL);

SplitPane root = new SplitPane();

root.getItems().addAll(menuContainer, contentContainer);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setResizable(false);
primaryStage.setTitle("Exam Supervisor");

}

加载屏幕的屏幕 Controller :

public class ScreensController extends StackPane {

private HashMap<String, Node> screens = new HashMap<>();

public void addScreen(String name, Node screen) {
screens.put(name, screen);
}

public boolean loadScreen(String name, String resource) {
try {
FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
System.out.println("name:" + name + " ,resource" + resource + " ,loader:" + myLoader.getLocation());
Parent loadScreen = myLoader.load();
ControlledScreen myScreenControler
= myLoader.getController();
myScreenControler.setScreenParent(this);
addScreen(name, loadScreen);
return true;
} catch (Exception e) {
System.out.println("name: " + name + ", resource" + resource + " ,exception: " + e.getMessage());
return false;
}
}

public boolean setScreen(final String name) {

if (screens.get(name) != null) {
final DoubleProperty opacity = opacityProperty();

if (!getChildren().isEmpty()) {
getChildren().remove(0);
getChildren().add(0, screens.get(name));
} else {
getChildren().add(screens.get(name));
}
return true;
} else {
System.out.println(screens.get(name) + " ,screen hasn't been loaded!\n");
return false;
}

}

public boolean unloadScreen(String name) {
if (screens.remove(name) == null) {
System.out.println("Screen didn't exist");
return false;
} else {
return true;
}
}
}

我的 github 存储库:https://github.com/eszikk/ExamSuperVisorServer

最佳答案

我查看了您的 netbeans 项目文件和 maven pom,没有发现任何异常。两种配置的类路径中均未包含 javafx 库这一事实告诉我,您正在从 Java 安装中使用它。

也许您在启动 maven 目标时使用了与 netbeans 不同的 Java 安装。然后,fxml 模块可能会丢失,或者第二次安装时没有安装 java fx,或者其他不兼容的东西。

您是否从控制台启动了 Maven 目标?例如

java -jar xyz.jar

尝试

java -version

并将该版本与 netbeans 使用的 java 版本进行比较(帮助 > 关于)。

Netbeans 构建由内部 Maven 版本完成。但也有可能外部 Maven 构建使用与内部 netbeans 构建不同版本的 java 编译器。

希望对您有所帮助!

关于java - Maven 构建后某些场景未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30144403/

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