JavaFX - java.lang.reflect.InvocationTargetException

时间:2017-10-07 17:02:04

标签: javafx scenebuilder

上午/下午

Main.java

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application{
    public static void main(String[] args) {

        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {


        Parent root = FXMLLoader.load(getClass().getResource("Splash.fxml"));
        Scene scene = new Scene(root);
        primaryStage.setTitle("My Title");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Splash.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import com.gluonhq.charm.glisten.control.TextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
   <top>
      <ImageView fitHeight="96.0" fitWidth="600.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER">
         <image>
            <Image url="@Hangman.png" />
         </image>
         <BorderPane.margin>
            <Insets left="20.0" top="50.0" />
         </BorderPane.margin>
      </ImageView>
   </top>
   <center>
      <VBox alignment="CENTER" spacing="15.0">
         <children>
            <TextField maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" promptText="Username" />
            <TextField layoutX="236.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" promptText="Password" />
            <Button mnemonicParsing="false" onAction="#testClick" text="Enter" />
         </children>
         <BorderPane.margin>
            <Insets top="20.0" />
         </BorderPane.margin>
      </VBox>
   </center>
   <bottom>
      <Pane BorderPane.alignment="CENTER">
         <children>
            <Label alignment="CENTER" contentDisplay="CENTER" layoutX="279.0" text="Sign Up">
               <padding>
                  <Insets bottom="20.0" />
               </padding>
            </Label>
         </children>
      </Pane>
   </bottom>
</BorderPane>

控制器

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import java.util.Random;

public class Controller {

    public void testClick() {

        System.out.println("test");
    }



}

我收到错误“java.lang.reflect.InvocationTargetException”。花了几个小时在论坛上拖网。产生FXML代码我使用了Scene Builder,所有SEEMS都处于正常工作状态。问题来自哪里?我怎么能在将来阻止它?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

如果您检查FXML文件中的导入列表,您有:

<?import com.gluonhq.charm.glisten.control.TextField?>
<?import javafx.geometry.Insets?>
...

因此,除非您正在创建Gluon Mobile项目,否则您的JavaFX应用程序将不包含Gluon Mobile库,并且无法找到com.gluonhq.charm.glisten.control.TextField控件,抛出InvocationTargetException

您只需使用“控件”选项卡中的JavaFX内置控件: