应用程序构造中的异常-Java



当我试图用事件驱动编程制作一些简单的按钮时,我在课本上看了一个例子(Java编程入门第10版edi。)

    Exception in Application constructor
    Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class ButtonPackage.ButtonEvent
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.NoSuchMethodException: ButtonPackage.ButtonEvent.<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$162(LauncherImpl.java:818)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$149(WinApplication.java:191)
        ... 1 more

从答案中得到建议后,我得以使其发挥作用。以下是更正后的压缩代码:

    package ButtonPackage;
    import javafx.application.Application;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    /**
     * Created by Brandon on 12/5/2015.
     */
    public class ButtonEvent extends Application {
        public static void main(String[] args) {
            Application.launch(args);
        }
        @Override //Override start method Application Class
        public void start(Stage primaryStage) {
            //create pane set properties
            HBox pane = new HBox(10);
            pane.setAlignment(Pos.CENTER);
            Button btEnter = new Button("ENTER NUMBER");
            Button btCheck = new Button("CHECK IF WINNER");
            EnterNumber handler1 = new EnterNumber();
            btEnter.setOnAction(handler1);
            CheckWinner handler2 = new CheckWinner();
            btCheck.setOnAction(handler2);
            pane.getChildren().addAll(btEnter, btCheck);
            //Create scene place on stage
            Scene scene = new Scene(pane);
            primaryStage.setTitle("HandleEvent"); //Set Stage Title
            primaryStage.setScene(scene); //Place scene in stage
            primaryStage.show(); //Display stage
        }//end start
    }//end ButtonEvent
    class EnterNumber implements EventHandler<ActionEvent> {
        @Override
        public void handle(ActionEvent e) {
            System.out.println("ENTER NUMBER button clicked");
        }//end handle
    }//end EnterNumber
    class CheckWinner implements EventHandler<ActionEvent> {
        @Override
        public void handle(ActionEvent e) {
            System.out.println("CHECK IF WINNER button clicked");
            //eventLottery.main();
        }//end handle
    }//end CheckWinner

谢谢你的帮助!

看起来您正在IntelliJ中运行此程序。虽然允许Application子类不具有main(...)方法,但并非所有IDE都支持这种执行模式。

请注意,在任何执行模式中,主类必须public

如果您想从IDE内部运行,请添加一个只调用launch():的主方法

public class ButtonEvent extends Application {
    // existing code ...
    public static void main(String[] args) {
        launch(args);
    }
}

(或者直接从命令行运行)。

您没有定义public static main(String[] args)方法。您需要定义这个方法,并告诉java这个方法在哪个类中。

相关内容

  • 没有找到相关文章

最新更新