提问者:小点点

JavaFX:神器仅显示空窗口


前言:我对JavaFX很陌生

做了一个简单的图像大小调整应用程序,需要构建应用程序的工件。使用IntelliJ和Eclipse(两者都试过),我构建了普通的JAR工件和JavaFX工件。两者都是可执行的,但只显示一个空窗口。

当从IDE启动应用程序时,没有问题;包含所有子窗格的窗口按应有的方式显示。

有什么想法可以解决这个问题吗?提前感谢!

附加应用程序初始化部分的代码和生成窗口的图片-也许会有所帮助。

public class MainApp extends Application {

private Stage primaryStage;
private BorderPane rootLayout;

private SettingsViewController settingController;
private MainViewController mainViewController;

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("SimpleImageResizer");

    this.primaryStage.getIcons().add(new Image("file:icon.png"));

    this.primaryStage.setOnCloseRequest(we -> ConfigurationCache.getInstance().pushConfig());

    targetResolutions.addAll(Arrays.asList(ResolutionPreset.values()));
    supportedFileFormats.addAll(Arrays.asList(SupportedFileFormat.values()));

    initRootLayout();

    showMainView();
    showSettingsView();
    primaryStage.setResizable(false);
}

public void initRootLayout() {
    try {
        // Load root layout from fxml file.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
        rootLayout = loader.load();

        // Show the scene containing the root layout.
        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
.
.
.
public static void main(String[] args) {
    launch(args);
}


共3个答案

匿名用户

您没有提供足够的信息来解决问题,所以这里有一个有效的示例:

创建一个包应用程序并将此类放入其中:

应用/Main.java

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/application/RootLayout.fxml"));
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

在包应用程序中创建一个“RootLayout. fxml”。

应用程序/根布局. f xml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
   <children>
      <Pane layoutX="-125.0" layoutY="-143.0" prefHeight="200.0" prefWidth="200.0">
         <children>
            <Button layoutX="134.0" layoutY="161.0" mnemonicParsing="false" text="Button" />
         </children>
      </Pane>
   </children>
</AnchorPane>

在Eclipse中选择

出口-

(当然你需要指定正确的启动配置)

生成的JAR可以执行并具有来自fxml的节点。

匿名用户

loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));

尝试与

loader.setLocation(MainApp.class.getResource("/view/RootLayout.fxml"));

NetBeans的第一个变体不会编译IDE

匿名用户

我遇到了类似的issue.ItIDE但是当我创建一个jar并尝试运行它时,它无法加载fxml文件。以下工作me.You可以尝试检查它是否得到了fxml文件。

String fxml = "/com/test/fxmls/gui.fxml";
InputStream in = MycLass.class.getResourceAsStream(fxml);
if(in!=null){
FXMLLoader loader = new FXMLLoader();
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(MycLass.class.getResource(fxml));
BorderPane rootLayout = (BorderPane)loader.load(in);
}else{
  //log or output the error
}