繁体   English   中英

JOptionPane YES_NO_OPTION 无法理解异常

[英]JOptionPane YES_NO_OPTION not understood exception

所以我有代码

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,但仍然不熟悉它们,因此帮助解决此错误或帮助查找导致它的原因将不胜感激。

你不能调用抽象类。 删除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");
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM