基本 Java(FX) 项目的 InvocationTargetException 错误
InvocationTargetException Error with Basic Java(FX) Project
我正在尝试使用 Java 和 JavaFX 制作一个简单的掷骰子游戏。
我已经创建了场景,并制作了 Main java class 和一个 Java 控制器,但是无论我尝试多少,我都会不断遇到相同的错误 - 我已经尝试通过多种方式解决这个问题,所以只是想看看是否有人可以给我一些正确的指导方向!下面是我的 classes 和 FXML 代码!
main.java
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class main extends Application {
public void start(Stage primaryStage) throws IOException{
FXMLLoader loader = new FXMLLoader(getClass().getResource("welcome.fxml"));
Parent root = (Parent) loader.load();
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
welcomecontroller.java
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
public class welcomecontroller {
@FXML
private ImageView p1_icon;
@FXML
private ImageView p2_icon;
@FXML
private ImageView end_icon;
@FXML
private ImageView die_animation;
@FXML
private Button btn_help;
@FXML
private Button btn_exit;
@FXML
private Button btn_start;
@FXML
void leftClick(ActionEvent event) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
try {
Parent root=(Parent) loader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
}
catch(IOException e)
{
e.printStackTrace();
}
}
@FXML
private TextField field_player1;
//field_player1.getText(p2.setName());
@FXML
private TextField field_player2;
@FXML
private TextField field_endscore;
}
welcome.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="welcomecontroller.java">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="272.0" minWidth="0.0" prefWidth="57.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="486.0" minWidth="10.0" prefWidth="278.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="305.0" minWidth="0.0" prefWidth="87.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="305.0" minWidth="10.0" prefWidth="179.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="221.0" minWidth="0.0" prefWidth="43.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="221.0" minWidth="10.0" prefWidth="26.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="10.0" prefHeight="47.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="53.0" minHeight="10.0" prefHeight="53.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="DICE GAME" textAlignment="CENTER" translateX="75.0" translateY="5.0" GridPane.columnIndex="1">
<font>
<Font name="Arial Black" size="29.0" />
</font>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Text opacity="0.37" strokeType="OUTSIDE" strokeWidth="0.0" text="Made by emm35" translateX="265.0" translateY="6.0" wrappingWidth="92.23500061035156" GridPane.columnIndex="1" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Player 1 Name:" translateX="65.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font name="System Bold Italic" size="17.0" />
</font>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Player 2 Name:" translateX="65.0" GridPane.columnIndex="1" GridPane.rowIndex="4">
<font>
<Font name="System Bold" size="17.0" />
</font>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="End Game score:" translateX="65.0" GridPane.columnIndex="1" GridPane.rowIndex="6">
<font>
<Font name="System Bold" size="17.0" />
</font>
</Text>
<Button fx:id="btn_exit" mnemonicParsing="false" text="EXIT GAME" translateX="60.0" GridPane.columnIndex="3" GridPane.rowIndex="3" />
<Button fx:id="btn_help" mnemonicParsing="false" text="HELP/TUTORIAL" translateX="45.0" GridPane.columnIndex="3" GridPane.rowIndex="5" />
<Button fx:id="btn_start" mnemonicParsing="false" text="START GAME" translateX="55.0" GridPane.columnIndex="3" GridPane.rowIndex="7" />
<ImageView fitHeight="40.0" fitWidth="40.0" translateX="5.0" GridPane.rowIndex="7">
<image>
<Image url="@end.png" />
</image>
</ImageView>
<ImageView fitHeight="40.0" fitWidth="40.0" translateX="5.0" GridPane.rowIndex="5">
<image>
<Image url="@player2.png" />
</image>
</ImageView>
<ImageView fitHeight="40.0" fitWidth="40.0" translateX="5.0" GridPane.rowIndex="3">
<image>
<Image url="@player1.png" />
</image>
</ImageView>
<ImageView fitHeight="109.0" fitWidth="103.0" translateX="45.0" GridPane.columnIndex="3" GridPane.rowIndex="1">
<image>
<Image url="@welcome_die.gif" />
</image>
</ImageView>
<TextField fx:id="field_player1" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<TextField fx:id="field_player2" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<TextField fx:id="field_endscore" GridPane.columnIndex="1" GridPane.rowIndex="7" />
</children>
</GridPane>
它们都在 src/same 目录中,如下面的屏幕截图所示!非常感谢任何建议来解决这个问题,因为我已经尝试了很多东西!从 here.
开始使用场景生成器创建场景
我的结构图片
提前致谢!
问题似乎是控制器路径。
将fx:controller="welcomecontroller.java"
替换为fx:controller="emm35_dicegame.welcomecontroller"
它对我有用。
我正在尝试使用 Java 和 JavaFX 制作一个简单的掷骰子游戏。
我已经创建了场景,并制作了 Main java class 和一个 Java 控制器,但是无论我尝试多少,我都会不断遇到相同的错误 - 我已经尝试通过多种方式解决这个问题,所以只是想看看是否有人可以给我一些正确的指导方向!下面是我的 classes 和 FXML 代码!
main.java
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class main extends Application {
public void start(Stage primaryStage) throws IOException{
FXMLLoader loader = new FXMLLoader(getClass().getResource("welcome.fxml"));
Parent root = (Parent) loader.load();
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
welcomecontroller.java
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
public class welcomecontroller {
@FXML
private ImageView p1_icon;
@FXML
private ImageView p2_icon;
@FXML
private ImageView end_icon;
@FXML
private ImageView die_animation;
@FXML
private Button btn_help;
@FXML
private Button btn_exit;
@FXML
private Button btn_start;
@FXML
void leftClick(ActionEvent event) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
try {
Parent root=(Parent) loader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
}
catch(IOException e)
{
e.printStackTrace();
}
}
@FXML
private TextField field_player1;
//field_player1.getText(p2.setName());
@FXML
private TextField field_player2;
@FXML
private TextField field_endscore;
}
welcome.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="welcomecontroller.java">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="272.0" minWidth="0.0" prefWidth="57.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="486.0" minWidth="10.0" prefWidth="278.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="305.0" minWidth="0.0" prefWidth="87.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="305.0" minWidth="10.0" prefWidth="179.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="221.0" minWidth="0.0" prefWidth="43.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="221.0" minWidth="10.0" prefWidth="26.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="10.0" prefHeight="47.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="53.0" minHeight="10.0" prefHeight="53.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="DICE GAME" textAlignment="CENTER" translateX="75.0" translateY="5.0" GridPane.columnIndex="1">
<font>
<Font name="Arial Black" size="29.0" />
</font>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Text opacity="0.37" strokeType="OUTSIDE" strokeWidth="0.0" text="Made by emm35" translateX="265.0" translateY="6.0" wrappingWidth="92.23500061035156" GridPane.columnIndex="1" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Player 1 Name:" translateX="65.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font name="System Bold Italic" size="17.0" />
</font>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Player 2 Name:" translateX="65.0" GridPane.columnIndex="1" GridPane.rowIndex="4">
<font>
<Font name="System Bold" size="17.0" />
</font>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="End Game score:" translateX="65.0" GridPane.columnIndex="1" GridPane.rowIndex="6">
<font>
<Font name="System Bold" size="17.0" />
</font>
</Text>
<Button fx:id="btn_exit" mnemonicParsing="false" text="EXIT GAME" translateX="60.0" GridPane.columnIndex="3" GridPane.rowIndex="3" />
<Button fx:id="btn_help" mnemonicParsing="false" text="HELP/TUTORIAL" translateX="45.0" GridPane.columnIndex="3" GridPane.rowIndex="5" />
<Button fx:id="btn_start" mnemonicParsing="false" text="START GAME" translateX="55.0" GridPane.columnIndex="3" GridPane.rowIndex="7" />
<ImageView fitHeight="40.0" fitWidth="40.0" translateX="5.0" GridPane.rowIndex="7">
<image>
<Image url="@end.png" />
</image>
</ImageView>
<ImageView fitHeight="40.0" fitWidth="40.0" translateX="5.0" GridPane.rowIndex="5">
<image>
<Image url="@player2.png" />
</image>
</ImageView>
<ImageView fitHeight="40.0" fitWidth="40.0" translateX="5.0" GridPane.rowIndex="3">
<image>
<Image url="@player1.png" />
</image>
</ImageView>
<ImageView fitHeight="109.0" fitWidth="103.0" translateX="45.0" GridPane.columnIndex="3" GridPane.rowIndex="1">
<image>
<Image url="@welcome_die.gif" />
</image>
</ImageView>
<TextField fx:id="field_player1" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<TextField fx:id="field_player2" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<TextField fx:id="field_endscore" GridPane.columnIndex="1" GridPane.rowIndex="7" />
</children>
</GridPane>
它们都在 src/same 目录中,如下面的屏幕截图所示!非常感谢任何建议来解决这个问题,因为我已经尝试了很多东西!从 here.
开始使用场景生成器创建场景我的结构图片
提前致谢!
问题似乎是控制器路径。
将fx:controller="welcomecontroller.java"
替换为fx:controller="emm35_dicegame.welcomecontroller"
它对我有用。