我正在创建一个 JavaFX 应用程序,当我尝试冻结程序时,我不得不手动关闭它。然而,我没有在 IntelliJ 中杀死该程序,而是单击
X
来关闭它。此后,窗口拒绝打开,卡在stage.setScene()
。
我已经清除了我的构建缓存,我已经重新生成了运行任务,我已经重新启动了我的计算机,我已经切换了计算机,并且在这些尝试中它只加载了一两次,然后就再也没有加载过。我尝试从 FXML 切换到纯 Java,将场景设置为无子节点(似乎已加载),然后将该节点设置为完全配备的 Java 节点(然后它就卡住了)。
这是我所拥有的代码。
package gg.hipposgrumm.jsr_launcher;
import gg.hipposgrumm.jsr_launcher.scenes.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class JSRLauncherMain extends Application {
public static Stage popup;
@Override
public void start(Stage stage) throws IOException {
stage.setTitle("JSR Launcher");
// Everything before this point executes.
stage.setScene(new Scene(Home.create()));
// Nothing after this point executes.
stage.setMinWidth(240);
stage.setMinHeight(120);
stage.show();
// More Code
}
public static void main(final String[] args) {
launch();
}
}
package gg.hipposgrumm.jsr_launcher.scenes;
import gg.hipposgrumm.jsr_launcher.JSRLauncherMain;
import gg.hipposgrumm.jsr_launcher.util.Game;
import gg.hipposgrumm.jsr_launcher.util.saving.ServerInfo;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import java.io.IOException;
public class Home {
public final BorderPane window;
private final ComboBox<LoginInfo> accounts;
private final ComboBox<ServerInfo> servers;
private Home() {
accounts = new ComboBox<>();
accounts.setPromptText("Not Logged In");
servers = new ComboBox<>();
servers.setPromptText("No Server Selected");
Button accountButton = new Button("Manage Accounts");
accountButton.setOnAction((event) -> {
try {
JSRLauncherMain.popupLogin(false);
} catch (IOException e) {
throw new IllegalStateException("Failed to open the Login menu please report with the following stacktrace: ", e);
}
});
Button serverButton = new Button("Manage Servers");
serverButton.setOnAction((event) -> {
try {
// TODO: Make this open the server menu.
JSRLauncherMain.popupLogin(false);
} catch (IOException e) {
throw new IllegalStateException("Failed to open the Server menu please report with the following stacktrace: ", e);
}
});
BorderPane footer = new BorderPane(
null,
null,
new HBox(servers, serverButton),
null,
new HBox(accounts,accountButton));
footer.setPadding(new Insets(5));
window = new BorderPane(new TabPane(
// Tabs added here.
), null, null, footer, null);
window.setMinSize(240, 120);
window.setMaxSize(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
window.setPrefSize(800, 400);
window.setUserData(this);
}
public static Parent create() {
Home home = new Home();
home.update();
return home.window;
}
public void update() {
// This does a thing.
}
}
我希望可以包含一个存储库,但我目前希望将该项目保持私有,直到发布为止。
正如其他人所说,如果没有最小的可重现示例,就不可能给您准确的答案。 然而,“它只工作一次,之后就不再工作”是应用程序使用系统资源(文件、端口...)并且没有正确释放它的典型情况。
在JavaFX中,通常是因为十字按钮没有发送中断信号并停止JVM。它只是关闭主舞台并停止 JavaFX 框架。如果创建了其他线程,那么 JVM 将由它们保持活动状态,并且不会释放资源(您仍然应该在后台打开该进程)。
确定哪些资源可以被锁定,并在使用后或阶段关闭时正确清理它们:
public class JSRLauncherMain extends Application {
@Override
public void start(Stage stage) throws IOException {
stage.setTitle("JSR Launcher");
// More Code
}
@Override
public void stop() {
// clear ressources
}
编辑以使用
Applciation.stop()
作为评论建议。