gpt4 book ai didi

JavaFX 从不同类启动应用程序

转载 作者:行者123 更新时间:2023-11-29 04:53:28 26 4
gpt4 key购买 nike

编辑:我现在通过将 JavaFxWindow 设为公共(public)类来修复此问题。
但是,当您公开一个类时,它必须存在于其自己命名的 *.java 源文件中。是否有针对以下两个类可以存在于同一文件中的解决方案?

我正在尝试从不同的类启动 JavaFX 应用程序。通常,我们从 Application 类本身调用 launch 来启动 JavaFX 应用程序。

但是如果我想编写一个程序让用户选择显示 JavaFX 窗口或显示 Swing 窗口怎么办?

根据我微弱的理解,这是最接近的:

package org.requiredinput.aitoopwj5e;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;

class ch2q17 {
public static void main(String[] argv) {

char userInput;

System.out.println("Please choose either Swing window [s], " +
"or JavaFX window [f] ... ");

JavaFxWindow myWindow = new JavaFxWindow();
myWindow.main(argv);
//myWindow.launch(argv);

}
}

class JavaFxWindow extends Application {
public static void main(String[] argv) {
Application.launch(argv);
}

@Override
public void start(Stage primaryStage) {
Group root = new Group();
primaryStage.setScene(new Scene(root, 1024, 768));
primaryStage.setTitle("Test Window");
//root.getChildren().add(vb);
System.out.println("Showing stage...");
primaryStage.show();

}
}

但是它抛出了很多令人兴奋的错误:

% java -cp bin/test org.requiredinput.aitoopwj5e.ch2q17
Please choose either Swing window [s], or JavaFX window [f] ...
Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class org.requiredinput.aitoopwj5e.JavaFxWindow
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$2/1595428806.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodException: org.requiredinput.aitoopwj5e.JavaFxWindow.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getConstructor(Class.java:1825)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$158(LauncherImpl.java:818)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/236498957.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$53/991256019.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$55/87321364.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$54/1460563668.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/1350641094.run(Unknown Source)
... 1 more

我是初学者,毫无疑问,答案可能让我难以理解,涉及一些晦涩难懂的神秘深奥的 Java,所以请尝试以一种刚刚了解什么是内部类的人可以理解的方式来解释这一点...

最佳答案

只需在单独的文件中定义您需要的每个类。请注意,有一个 overloaded version of Application.launch它采用一个参数来表示带有 start 方法的类。所以你可以这样做:

AppLauncher.java:

public class AppLauncher {

public static void main(String[] args) {
if (args.length < 1 ||
(! "S".equals(args[0].toUpperCase()) ||
(! "F".equals(args[0].toUpperCase())) {
System.out.println("Provide an argument of S for Swing or F for JavaFX");
System.exit(1);
}

if ("S".equals(args[0].toUpperCase())) {
SwingUtilities.invokeLater(() ->
new SwingApplication().setVisible(true));
} else {
Application.launch(FXApplication.class, args);
}
}
}

SwingApplication.java:

public class SwingApplication extends JFrame {
public SwingApplication() {
// set up UI etc...
}
}

FX应用程序.java:

public class FXApplication extends Application {

@Override
public void start(Stage primaryStage) {
// etc...
}
}

关于JavaFX 从不同类启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34597594/

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