gpt4 book ai didi

JavaFX:工件仅显示空窗口

转载 作者:搜寻专家 更新时间:2023-11-01 01:16:26 25 4
gpt4 key购买 nike

前言:我是 JavaFX 的新手

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

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

有什么办法解决这个问题吗?提前致谢!

应用程序初始化部分的附加代码和结果窗口的图片 - 可能有帮助。

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);
}

Artifact output

IDE output

最佳答案

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

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

application/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”。

application/RootLayout.fxml

<?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 中选择

Export -> Runnable JAR file -> Extract required libraries into generated JAR

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

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

关于JavaFX:工件仅显示空窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30803248/

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