You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

此前正常的FXML项目现报javafx.fxml.LoadException,AnchorPane xmlns异常

JavaFX FXML加载失败:AnchorPane xmlns属性报错问题

环境配置

  • IntelliJ 2022
  • JDK 17.0.4.1
  • JavaFX 19(Maven管理)
  • Scene Builder 19

问题描述

运行FXML项目时无法加载FXML文件,IntelliJ持续提示AnchorPane标签存在问题——即使是Scene Builder生成或IntelliJ自带模板创建的FXML文件也会触发该错误,编译器明确指向AnchorPane标签中的xmlns="http://javafx.com/javafx/19"属性。

已排除文件路径问题:调试中getClass().getResource("/views/login.fxml")返回的URL非空。此前曾遇到"uri未注册"错误,删除Maven集成的JavaFX依赖后改用本地JavaFX19库并配置了正确的JVM参数,随后出现当前问题。

注:项目resources目录下应为views而非controllers(旧版本截图有误)

FXML代码

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.LoginController">
    <children>
        <VBox prefHeight="400.0" prefWidth="600.0" />
        <TextField id="Username" layoutX="115.0" layoutY="60.0" prefHeight="25.0" prefWidth="126.0" promptText="user@account.com" />
        <Label layoutX="39.0" layoutY="64.0" prefHeight="17.0" prefWidth="63.0" text="Username" />
        <Label layoutX="39.0" layoutY="116.0" text="Password" />
        <TextField id="password" layoutX="115.0" layoutY="112.0" prefHeight="25.0" prefWidth="126.0" />
        <Button id="login" layoutX="132.0" layoutY="210.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="85.0" text="Login" />
        <Label layoutX="39.0" layoutY="153.0" prefHeight="17.0" prefWidth="63.0" text="Location" />
        <TextField id="Location" disable="true" layoutX="115.0" layoutY="149.0" prefHeight="25.0" prefWidth="126.0" promptText="Your location here" />
        <Label id="ErrorMessage" alignment="CENTER" disable="true" layoutX="91.0" layoutY="183.0" prefHeight="17.0" prefWidth="187.0" text="Error Label" textAlignment="CENTER" textFill="#e10724">
            <font>
                <Font name="System Italic" size="12.0" />
            </font>
        </Label>
    </children>
</AnchorPane>

Java控制器代码

package controllers;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.net.URL;

public class LoginController {

    @FXML
    private TextField Username;
    //Buttons
    @FXML
    private TextField Password;
    @FXML
    private TextField Location;
    @FXML
    private Label ErrorMessage;
    @FXML
    private Button Login;

    public void BeginStart(){
        //Loader
        try {
            //debugging fxml path
            URL uri = getClass().getResource("/views/login.fxml");
            System.out.println(uri);
            
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/views/login.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            Stage stage = new Stage();
            stage.setTitle("Hello!");
            stage.setScene(scene);
            stage.show();

            /*Parent root = loader.load();
            Stage stage = new Stage();
            stage.setTitle("Scheduler Login");
            stage.setScene(new Scene(root));
            stage.show();*/
        }catch(Exception e){
            System.out.println("Failed to load login.fxml");
            System.out.println("Exception: " + e.toString());
        }
    }
}

内容的提问来源于stack exchange,提问作者Brenden Delong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 00:30:42