joptionpane yes\u no\u选项不可理解异常

ldxq2e6h  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(371)

所以我有密码

import javax.swing.JOptionPane;
import javafx.application.Application;

public abstract class HHGUI extends Application {
    JOptionPane yesno = new JOptionPane();
    public HHGUI() {
        int reply = JOptionPane.showConfirmDialog(null,"Do you want the ground to generate from premade file?","Read Ground From File", JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION) {
            Manager.plateaucreator();
            System.exit(0);
        }
         else {
             Manager.randplateaucreator();
             System.exit(0);
         }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

我不断地得到错误

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:873)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class HHGUI
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$7(LauncherImpl.java:819)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$4(WinApplication.java:186)
    ... 1 more
Exception running application HHGUI

我查了其他问题,找不到任何我遗漏的导致这个错误的东西。我已经阅读了joptionpanes,但仍然不熟悉它们,所以帮助解决这个错误或帮助找到它的原因将不胜感激。

hxzsmxv2

hxzsmxv21#

不能调用抽象类。拆下 abstract 修改器和工具 start 方法:

import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.stage.Stage;  

public class HHGUI extends Application {

    JOptionPane yesno = new JOptionPane();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage)  {
        int reply = JOptionPane.showConfirmDialog(null,"Do you want the ground to generate from premade file?","Read Ground From File", JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION) {
            System.out.println("Yes");
            System.exit(0);
        }
        else {
            System.out.println("NO");
            System.exit(0);
        }
    }
}

尽管可以使用swing JOptionPane ,在javafx中 Alert 应使用控制:

import java.util.Optional;
import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;

public class HHGUI extends Application {

public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage)  {

        Alert yesNo = new Alert(AlertType.CONFIRMATION,
                "Do you want the ground to generate from premade file?",
                ButtonType.YES,
                ButtonType.NO);
        yesNo.setTitle("Read Ground From File");
        Optional<ButtonType> result = yesNo.showAndWait();

        if (result.get() == ButtonType.OK) {
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    }
}

相关问题