2017-10-07 78 views
0

上午/下午的JavaFX - java.lang.reflect.InvocationTargetException

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

你應該張貼例外擺在首位。但我懷疑你的問題可能來自使用Gluon'TextField'? –

+0

看來是對的!刪除它,並加載,爲什麼這個錯誤發生? – BenjaminBarnes

+0

由於您添加了錯誤的「TextField」:除非您正在創建Gluon移動項目,否則您的JavaFX桌面應用程序不包含Gluon Mobile庫。 –

回答

1

如果您在您的FXML文件導入列表中你有:

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

所以,除非你正在創建一個膠子Mobile項目,JavaFX應用程序將不包括膠子的流動圖書館,它會失敗找到com.gluonhq.charm.glisten.control.TextField控制權,投擲InvocationTargetException

你只需要使用JavaFX的內置控制,從控制選項卡:

相關問題