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

时间:2015-12-06 18:48:27

标签: java exception javafx event-handling

在努力尝试使用事件驱动编程创建一些简单的按钮时,我正在查看我的教科书中的一个示例(Java编程简介第10版)。我按照本书中的一些代码尝试自己复制一个项目我正在尝试。该程序编译,但当它运行时,我得到:

    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

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您没有定义public static main(String[] args)方法。您需要定义此方法,并告诉java此方法所在的类。

答案 1 :(得分:0)

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

请注意,在任何执行模式下,您的主要课程必须public

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

public class ButtonEvent extends Application {

    // existing code ...

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

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