为什么我不能启动 JavaFX 程序?

Why cant I start a JavaFX program?

我的程序有点问题。 我可以正常 运行 程序,直到我给我的元素标签和操作,如 "onClick"... 有一些我无法修复的异常,所以我请求你的帮助。 是我电脑的问题还是代码错误?

这就是您所需要的:

异常:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: javafx.fxml.LoadException: 
/E:/programovanie/netgame/out/production/netgame/sample/sample.fxml:26

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
    at sample.Main.start(Main.java:13)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:417)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:175)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Can not set org.w3c.dom.Text field sample.Controller.minuty to javafx.scene.text.Text
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.base/java.lang.reflect.Field.set(Field.java:780)
    at javafx.fxml/javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1174)
    at javafx.fxml/javafx.fxml.FXMLLoader.access00(FXMLLoader.java:105)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:865)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:759)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    ... 17 more
Exception running application sample.Main

sample.fxml :

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.AnchorPane?>
<?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?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <children>
        <GridPane minHeight="100.0" minWidth="900.0" style="-fx-background-color: #CDE8C8; -fx-border-color: black;">
            <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            </columnConstraints>
            <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            </rowConstraints>
            <children>
                <Text fx:id="minuty" strokeType="OUTSIDE" strokeWidth="0.0" text="00" wrappingWidth="225.125">
                    <font>
                        <Font size="40.0" />
                    </font>
                    <GridPane.margin>
                        <Insets left="50.0" />
                    </GridPane.margin>
                </Text>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text=":" textAlignment="CENTER" wrappingWidth="224.671875">
                    <font>
                        <Font size="40.0" />
                    </font>
                </Text>
                <Text fx:id="sekundy" strokeType="OUTSIDE" strokeWidth="0.0" text="00" textAlignment="RIGHT" wrappingWidth="176.9375">
                    <font>
                        <Font size="40.0" />
                    </font>
                    <GridPane.margin>
                        <Insets />
                    </GridPane.margin>
                </Text>
                <Text fx:id="stavcerveny" fill="RED" strokeType="OUTSIDE" strokeWidth="0.0" text="0" wrappingWidth="225.5625" GridPane.columnIndex="1">
                    <font>
                        <Font size="40.0" />
                    </font>
                    <GridPane.margin>
                        <Insets left="50.0" />
                    </GridPane.margin>
                </Text>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text=":" textAlignment="CENTER" wrappingWidth="225.671875" GridPane.columnIndex="1">
                    <font>
                        <Font size="40.0" />
                    </font>
                </Text>
                <Text fx:id="stavmodry" fill="#0073ff" strokeType="OUTSIDE" strokeWidth="0.0" text="0" textAlignment="RIGHT" wrappingWidth="157.5625" GridPane.columnIndex="1">
                    <font>
                        <Font size="40.0" />
                    </font>
                </Text>
                <Button alignment="CENTER" mnemonicParsing="false" onMouseClicked="#zapnutie" prefHeight="25.0" prefWidth="226.0" text="PLAY" GridPane.columnIndex="2">
                    <GridPane.margin>
                        <Insets bottom="40.0" left="40.0" right="40.0" />
                    </GridPane.margin>
                </Button>
                <Button mnemonicParsing="false" onMouseClicked="#restart" prefHeight="25.0" prefWidth="227.0" text="RESTART" textAlignment="CENTER" GridPane.columnIndex="2">
                    <opaqueInsets>
                        <Insets />
                    </opaqueInsets>
                    <GridPane.margin>
                        <Insets left="40.0" right="40.0" top="50.0" />
                    </GridPane.margin>
                </Button>
                <Slider fx:id="obtiazne" prefHeight="14.0" prefWidth="126.0" GridPane.columnIndex="3">
                    <GridPane.margin>
                        <Insets left="20.0" right="20.0" />
                    </GridPane.margin>
                </Slider>
            </children>
        </GridPane>
      <AnchorPane fx:id="hra" layoutY="100.0" prefHeight="500.0" prefWidth="900.0" />
    </children>
</AnchorPane>

Controller.java :

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import org.w3c.dom.Text;

public class Controller {
    @FXML
    Slider obtiazne;
    @FXML
    Text minuty;
    @FXML
    Text sekundy;
    @FXML
    Text stavcerveny;
    @FXML
    Text stavmodry;
    @FXML
    AnchorPane hra;

    public void zapnutie(MouseEvent mouseEvent) {
        System.out.println(obtiazne.getValue());
    }

    public void restart(MouseEvent mouseEvent) {
    }
}

Main.java :

package sample;

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 {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


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

我认为正确的行是:

"at sample.Main.start(Main.java:13)"

那是:“Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));”

我不明白剩下的部分,所以我希望你能轻松解决这个问题:)

相关的堆栈跟踪部分是

Can not set org.w3c.dom.Text field sample.Controller.minuty to javafx.scene.text.Text

这意味着你的导入在控制器中是错误的class

import org.w3c.dom.Text;更改为import javafx.scene.text.Text;