【问题标题】:JavaFx Program throws java.lang.reflect.InvocationTargetExceptionJavaFx 程序抛出 java.lang.reflect.InvocationTargetException
【发布时间】:2016-04-12 16:25:06
【问题描述】:

这是我在这里的第一个问题,所以如果有问题不要生我的气。我目前正在JavaFx的帮助下为java编写一个类似空闲的游戏。 下面的代码是用 eclipse mars 和 JavaFx 编写的。 Eclipse 向我显示代码是正确的,但是当我想启动程序时,它会抛出以下错误消息块:

public class Main extends Application{

private static final Color color = Color.web("#464646");
Button button3 = new Button("D");
DropShadow shadow = new DropShadow();
Label label = new Label();

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

@Override
public void start(Stage primaryStage) throws Exception{
    Scene scene = new Scene(new Group());
    primaryStage.setTitle("Button Sample");
    primaryStage.setWidth(300);
    primaryStage.setHeight(190);

    label.setFont(Font.font("Times New Roman", 22));
    label.setTextFill(color);

    Image imageDecline = new Image(getClass().getResourceAsStream("../not.png"));
    Image imageAccept = new Image(getClass().getResourceAsStream("../ok.png"));

    VBox vbox = new VBox();
    vbox.setLayoutX(20);
    vbox.setLayoutY(20);
    HBox hbox1 = new HBox();
    HBox hbox2 = new HBox();

    Button button1 = new Button("Accept");
    button1.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
    button1.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            label.setText("Accepted");
        }
    });

    Button button2 = new Button("Accept");
    button2.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            label.setText("Accepted");
        }
    });

    button3.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            label.setText("Declined");
        }
    });

    button3.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent e) {
            button3.setEffect(shadow);
        }
    });

    button3.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent e) {
            button3.setEffect(null);
        }
    });

    hbox1.getChildren().add(button2);
    hbox1.getChildren().add(button3);
    hbox1.getChildren().add(label);
    hbox1.setSpacing(10);
    hbox1.setAlignment(Pos.BOTTOM_CENTER);

    Button button4 = new Button();
    button4.setGraphic(new ImageView(imageAccept));
    button4.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            label.setText("Accepted");
        }
    });

    Button button5 = new Button();
    button5.setGraphic(new ImageView(imageDecline));
    button5.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            label.setText("Declined");
        }
    });

    hbox2.getChildren().add(button4);
    hbox2.getChildren().add(button5);
    hbox2.setSpacing(25);

    vbox.getChildren().add(button1);
    vbox.getChildren().add(hbox1);
    vbox.getChildren().add(hbox2);
    vbox.setSpacing(10);
    ((Group) scene.getRoot()).getChildren().add(vbox);

    primaryStage.setScene(scene);
    primaryStage.show();
}}

我希望这里的任何人对此问题有提示或解决方案:) 会非常好。

最好的问候, 迈克

【问题讨论】:

标签: java eclipse javafx


【解决方案1】:

我认为您的问题与您用于创建 Image 对象的输入流有关:

... = new Image(getClass().getResourceAsStream("../not.png"));

当我在 Eclipse 中启动您的应用程序时,我也会收到 InvocationTargetException。这是由 NullPointerException 引起的,因为找不到 PNG 文件。

尝试在不创建 Image 对象的情况下启动您的应用程序,看看是否仍然出现任何异常。

【讨论】:

  • 感谢您的反馈,我检查了它,它可以在没有 png 的情况下工作......这是 png´s 的标准文件路径?包的根C oder src文件夹?
  • 您可以使用相对或绝对路径。如果您使用相对路径(例如“not.png”),则将相对于您的类文件所在的文件夹查找“not.png”文件。更多信息可以在here找到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-08
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多