应用程序构造异常 - Java
Exception in Application Construction - Java
在尝试使用事件驱动编程制作一些简单的按钮时,我正在看教科书中的一个示例(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$launchApplication6(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$launchApplication12(LauncherImpl.java:818)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(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$null9(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
感谢您的帮助!
您没有定义 public static main(String[] args)
方法。你需要定义这个方法,并告诉java这个方法在哪个class
看起来您正在 运行在 IntelliJ 中执行此操作。虽然允许 Application
subclass 没有 main(...)
方法,但并非所有 IDE 都支持此执行模式。
请注意,在任何执行模式下,您的主要class 必须是public
。
如果您想从 IDE 内部 运行,添加一个简单调用 launch()
:
的主要方法
public class ButtonEvent extends Application {
// existing code ...
public static void main(String[] args) {
launch(args);
}
}
(或者直接从命令行 运行)。
在尝试使用事件驱动编程制作一些简单的按钮时,我正在看教科书中的一个示例(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$launchApplication6(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$launchApplication12(LauncherImpl.java:818)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(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$null9(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
感谢您的帮助!
您没有定义 public static main(String[] args)
方法。你需要定义这个方法,并告诉java这个方法在哪个class
看起来您正在 运行在 IntelliJ 中执行此操作。虽然允许 Application
subclass 没有 main(...)
方法,但并非所有 IDE 都支持此执行模式。
请注意,在任何执行模式下,您的主要class 必须是public
。
如果您想从 IDE 内部 运行,添加一个简单调用 launch()
:
public class ButtonEvent extends Application {
// existing code ...
public static void main(String[] args) {
launch(args);
}
}
(或者直接从命令行 运行)。