我正在尝试将swing UI转换为JavaFX,用于我正在编辑源代码的聊天系统,当我尝试运行该程序时,我收到错误。 这是我的主要课程:
public class Main extends Application{
private Main() {
}
private static ArgumentParser argumentParser;
private static Stage primaryStage;
private static ArgumentResponder argumentResponder;
private static UncaughtExceptionLogger uncaughtExceptionLogger;
private static Settings settings;
/**
* The main method, for starting the application.
*
* <p>See {@link Argument} for the supported arguments.</p>
*
* @param args arguments given when starting KouChat.
*/
public static void main(String[] args){
argumentParser = new ArgumentParser(args);
argumentResponder = new ArgumentResponder(argumentParser);
if (!argumentResponder.respond()) {
return;
}
new LogInitializer(argumentParser.hasArgument(Argument.DEBUG));
// Initialize as early as possible to catch all exceptions
uncaughtExceptionLogger = new UncaughtExceptionLogger();
settings = loadSettings(argumentParser);
launch(args);
}
private static Settings loadSettings(final ArgumentParser
argumentParser) {
final Settings settings = new Settings();
final ArgumentSettingsLoader argumentSettingsLoader = new
ArgumentSettingsLoader();
argumentSettingsLoader.loadSettings(argumentParser, settings);
final PropertyFileSettingsLoader propertyFileSettingsLoader = new
PropertyFileSettingsLoader();
propertyFileSettingsLoader.loadSettings(settings);
return settings;
}
@Override
public void start(Stage primaryStageObj) throws Exception{
primaryStage = primaryStageObj;
FXMLLoader loader = new FXMLLoader(getClass().getResource("ui/swing
/Chat.fxml"));
ChatController pls = new ChatController(argumentParser, settings,
uncaughtExceptionLogger);
loader.setController(pls);
pls.setStage(primaryStage);
Parent root = (Parent) loader.load();
primaryStage.setTitle("Flake");
primaryStage.setScene(new Scene(root, 959,583 ));
primaryStage.setResizable(false);
primaryStage.show();
primaryStage.setResizable(false);
primaryStage.setOnCloseRequest(e -> Platform.exit());
}
}
这是我的控制器:
public class ChatController {
@FXML
private TextArea messageBox;
@FXML
private Label onlineCountLbl;
@FXML
private ListView userList;
@FXML
private Button fileBtn;
@FXML
private Button sendBtn;
@FXML
private ScrollPane scrollPane;
@FXML
private ListView messageArea;
private final UITools uiTools = new UITools();
//private final User me;
private Stage stage;
private ListView<String> messageList;
private UIFactory Flake;
public ChatController() {
}
private boolean done;
public ChatController(final ArgumentParser argumentParser,final Settings
settings,final UncaughtExceptionLogger uncaughtExceptionLogger){
Validate.notNull(argumentParser, "Argument parser can not be null");
Validate.notNull(settings, "Settings can not be null");
Validate.notNull(uncaughtExceptionLogger, "Uncaught exception logger
can not be null");
Flake = new UIFactory(scrollPane, userList, onlineCountLbl,
messageBox, messageArea, argumentParser,settings, uncaughtExceptionLogger);
}
public void setStage(Stage stage){
this.stage = stage;
}
}
这是我的错误消息:
Exception in Application constructor
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class net.usikkert.kouchat.Main
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodException: net.usikkert.kouchat.Main.<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$160(LauncherImpl.java:818)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
我不知道如何解决这个问题有一种方法可以让使用swing看起来更好用户界面因为从我看到的在线人们都说我应该使用JavaFX因为它看起来好多了并且更容易工作而不是摆动。
答案 0 :(得分:4)
java.lang.NoSuchMethodException: package.Class.<init>()
当无法调用不带参数的构造函数时,会发生此异常。大多数情况下它都会发生,因为没有提供参数的构造函数,在你的情况下问题是构造函数是私有的。
设置此行
private Main() {
}
到
public Main() {
}
并且应该解决错误。