【问题标题】:JavaFX Trouble Loading FXMLJavaFX 无法加载 FXML
【发布时间】:2017-02-08 07:57:18
【问题描述】:

我在将我的 FXML 文件登录名输入到我的 JavaFX 应用程序时遇到问题。

谁能告诉我我做错了什么?

我从 Swing 开始对 JavaFX 非常陌生,如果它真的很糟糕或不正确,请见谅。

package application;

import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
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 {
        try {
            AnchorPane page = (AnchorPane) FXMLLoader.load(getClass().getResource("application/MainDisplay.fxml"));
            Scene scene = new Scene(page);
            primaryStage.setScene(scene);
            primaryStage.setTitle("Welcome!");
            primaryStage.show();
        } 
        catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        }
    }
}

这是 fXML 文件

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

<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>


<AnchorPane prefHeight="400.0" prefWidth="600.0"     xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65">
   <children>
      <Text layoutX="177.0" layoutY="137.0" strokeType="OUTSIDE"     strokeWidth="0.0" text="Welcome!" wrappingWidth="140.02001953125">
         <font>
            <Font size="25.0" />
         </font>
         <cursor>
            <Cursor fx:constant="NONE" />
         </cursor>
      </Text>
      <Text layoutX="177.0" layoutY="179.0" strokeType="OUTSIDE"     strokeWidth="0.0" text="User Name:" wrappingWidth="85.9443359375" />
      <Text layoutX="177.0" layoutY="213.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Password:" />
      <Button layoutX="353.0" layoutY="242.0" mnemonicParsing="false" text="Sign In" />
      <TextField layoutX="263.0" layoutY="161.0" />
      <PasswordField layoutX="263.0" layoutY="195.0" />
   </children>
</AnchorPane>

【问题讨论】:

  • 你遇到了什么麻烦?
  • 它给出了一个错误。它不加载 xml 文件并崩溃。
  • 在这种情况下,请发布堆栈跟踪和任何相关信息。我们在这里为您提供帮助,但您需要提供一些非常基本的信息。
  • 请发布错误的堆栈跟踪,以便我们识别问题。 :)
  • 您是否得到未设置位置?如果使用 application 包中的类来加载资源,则应使用 /application/MainDisplay.fxml 或仅使用 MainDisplay.fxml 作为路径...

标签: java javafx fxml fxmlloader


【解决方案1】:

试试这个:

@Override
    public void start(Stage primaryStage){
        try {
            AnchorPane page = (AnchorPane) FXMLLoader.load(getClass().getResource("/application/MainDisplay.fxml"));
            Scene scene = new Scene(page);
            primaryStage.setScene(scene);
            primaryStage.setTitle("Welcome!");
            primaryStage.show();
        } 
        catch (Exception e) {
            e.printStackTrace();
        }

【讨论】:

    猜你喜欢
    • 2017-03-17
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    相关资源
    最近更新 更多