ncm*_*ist 7 macos javafx macos-catalina
这是一个简单的 JavaFX 程序。它编译并正常运行 util 按钮被点击。
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
public class Window extends Application
{
@Override
public void start(Stage primary)
{
primary.setTitle("Window");
Button b = new Button("Clickez-vous moi!");
VBox vb = new VBox();
vb.getChildren().add(b);
primary.setScene(new Scene(vb, 400,400));
b.setOnAction( e -> Platform.exit());
primary.show();
}
}
Run Code Online (Sandbox Code Playgroud)
}
这是我收到的错误消息。
Java has been detached already, but someone is still trying to use it at -[GlassViewDelegate dealloc]:/Users/jenkins/workspace/OpenJFX-mac/modules/javafx.graphics/src/main/native-glass/mac/GlassViewDelegate.m:198
0 libglass.dylib 0x000000012a1dbf92 -[GlassViewDelegate dealloc] + 290
1 libglass.dylib 0x000000012a1e1c9c -[GlassView3D dealloc] + 252
2 libobjc.A.dylib 0x00007fff645894ea _ZN19AutoreleasePoolPage12releaseUntilEPP11objc_object + 134
3 libobjc.A.dylib 0x00007fff6456f440 objc_autoreleasePoolPop + 175
4 CoreFoundation 0x00007fff2e45f66e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
5 CoreFoundation 0x00007fff2e45f594 __CFRunLoopDoObservers + 457
6 CoreFoundation 0x00007fff2e40272b __CFRunLoopRun + 1219
7 CoreFoundation 0x00007fff2e401fe3 CFRunLoopRunSpecific + 499
8 libjli.dylib 0x000000010735b619 CreateExecutionEnvironment + 399
9 libjli.dylib 0x000000010735775e JLI_Launch + 1354
10 java 0x000000010734dca5 main + 375
11 libdyld.dylib 0x00007fff658dc2e5 start + 1
MAC:Wed Dec 04:13:10:426>
Run Code Online (Sandbox Code Playgroud)
我正在运行最新版本的 MACOSX Catalina。
使用
stage.setOnCloseRequest((event) -> {
Platform.exit();
});
Run Code Online (Sandbox Code Playgroud)
并在button.onAction()方法中调用.close(),如下所示:
button.setOnAction((event) -> {
stage.close();
});
Run Code Online (Sandbox Code Playgroud)
两种事件类型的旧式匿名内部类实现如下:
button.setOnAction(new EventHandler<ActionEvent>() { // note event handler class (ACTION)
@Override
public void handle(ActionEvent event) {
stage.close();
}
});
Run Code Online (Sandbox Code Playgroud)
和
stage.setOnCloseRequest(new EventHandler<WindowEvent>() { // note event handler class (WINDOW)
@Override
public void handle(WindowEvent event) {
Platform.exit();
}
});
Run Code Online (Sandbox Code Playgroud)
奇怪的是,我可以在 Windows 上运行您的代码,并且在单击按钮后它会发出 BUILD SUCCESSFUL 消息。所以我可能完全是胡说八道。
然而,操作系统级别的事件处理之间存在差异,因此值得花时间按 CTRL C;CTRL V 一些代码。
编辑...
我还注意到您在应用程序类中缺少一个 main 方法,这对我来说有点奇怪。
我很确定我在过去的某个时刻尝试生成 2 个不同的应用程序实例,但由于各种 javaFX 原因而无法这样做,因此如果您设法成功实现它,则可能会调用 platform.close()来自错误的 PrimaryStage 的例程
寻找类似的东西
public static void main(String[] args) {
Application.launch(args); // this directly references the class it's housed in since it expects to be in xxxx extends Application{} classes
}
Run Code Online (Sandbox Code Playgroud)
或者
public static void main(String[] args) {
LauncherImpl.launchApplication(someClass.class, args); // this can be used to launch any class as long as it extends Application.class.
}
Run Code Online (Sandbox Code Playgroud)
最后,确保你没有做一些愚蠢的事情,比如
Window x = new Window(); // pretty sure you can't actually do this but fk knows.... can't be bothered to check.
x.start();
Run Code Online (Sandbox Code Playgroud)
从代码中的某个位置调用。
应用程序 =/= 应用程序窗口。
如果你想创建一个多窗口应用程序,你需要做的就是:
public class ProperMultiWindowApp extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primary) {
primary.setTitle("Window");
Button b = new Button("Clickez-vous moi!");
VBox vb = new VBox();
vb.getChildren().add(b);
primary.setScene(new Scene(vb, 400, 400));
b.setOnAction(e -> primary.close());
Stage secondary = new Stage();
secondary.setScene(new Scene(new HBox(new Label("here be content zeh 2nd"))));
Stage tertiary = new Stage();
tertiary.setScene(new Scene(new HBox(new Label("here be content le 3rd"))));
primary.show();
secondary.show();
tertiary.show();
primary.setOnCloseRequest(e -> Platform.exit());
}
@Override
public void stop() throws Exception {
System.out.println("proof that this is stopping"); // this should be called during Platform.exit();
super.stop();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,对于使用 JavaFX 的标准 JavaSE,您在其中启动应用程序的类是 javaFX JVM 认为是主类的类。
如果这一切都失败了,要么是一个错误,要么是与你的Mac有关……因为它是一台Mac。
如果是这种情况,我衷心建议您继续不断地用石头砸它,以确保应用程序被有效关闭。
| 归档时间: |
|
| 查看次数: |
582 次 |
| 最近记录: |