【问题标题】:Why do I get a runtime exception while trying to load fxml file in my program为什么在我的程序中尝试加载 fxml 文件时出现运行时异常
【发布时间】:2016-06-06 17:51:06
【问题描述】:

我正在学习 javafx,现在我正在尝试将我学到的技术应用到我的 java 应用程序中。我的程序称为“WhatsTheTime”,它是一个简单的程序,由一个带有时钟图像的 AnchorPane 组成。单击窗格时,应该将 ImageView 后面的标签文本设置为当前时间,并将时钟图像设置为不可见。当我尝试运行该应用程序时,我在loader.load() 语句中得到了一个runtimeException。上网查了一下,发现多半是文件名指定不正确,但我查了一下文件名,绝对正确。

这是我的 fxml 代码:

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

<?import javafx.scene.shape.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>

<fx:root type = "AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" 
  minHeight="-Infinity" minWidth="-Infinity" onMouseClicked="#getTheTime"  prefHeight="198.0" prefWidth="208.0" type="AnchorPane"  
  xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="myPrototypes.WhatsTheTime">
  <children>
  <Rectangle arcHeight="5.0" arcWidth="5.0" height="200.0" stroke="BLACK" strokeType="INSIDE" width="208.0" />
  <Label fx:id="timeLabel" layoutX="90.0" layoutY="90.0" text="Label" />
  <ImageView fx:id="clockImage" fitHeight="186.0" fitWidth="193.0" layoutX="7.0" layoutY="7.0">
     <image>
        <Image url="@../../What'sTheTimeIcon.png" />
     </image>
  </ImageView>
  </children>
  </fx:root>

这是我的控制器类:

package myPrototypes;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;

public class WhatsTheTime extends AnchorPane{

@FXML
private ImageView clockImage;

@FXML
private Label timeLabel;

WhatsTheTime(){
    FXMLLoader loader = new FXMLLoader
            (getClass().getResource("WhatsTheTime.fxml"));

    loader.setRoot(this);
    loader.setController(this);

    try{
        loader.load();
    }
    catch(IOException e){
        throw new RuntimeException();
    }
}

@FXML
public void getTheTime(){
    clockImage.setVisible(false);

    SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm a");
    Date date = new Date();

    timeLabel.setText(timeFormat.format(date));
}
}

这是我的主要课程:

package myPrototypes;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class WhatsTheTimeMain extends Application {

@Override
public void start(Stage primaryStage) {
    WhatsTheTime component = new WhatsTheTime();

    Scene scene = new Scene(component);
    primaryStage.setScene(scene);
    primaryStage.sizeToScene();
    primaryStage.show();
}

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

请帮我解决这个问题。如果您需要任何其他信息,请告诉我

【问题讨论】:

  • 请发布异常堆栈跟踪 - 您可以通过在您的 catch 块中添加 e.printStackTrace() 在控制台中获取它。
  • 在 samle 元素中使用 type 属性两次可能不是最好的主意...
  • @fabian,您的想法也与下面的答案一起工作。非常感谢您的贡献

标签: java exception javafx


【解决方案1】:

如果您通过fx:controller 属性在FXML 中指定一个控制器类,您将指示FXMLLoaderload() 过程中实例化该类,并将该实例用作控制器对象。

但是,在您的情况下,您在调用 load 之前在加载程序上明确设置了一个控制器:

loader.setController(this);

由于在加载 FXML 时已经设置了控制器,因此会产生异常。 (如果您阅读堆栈跟踪,您会看到它包含一条错误消息,类似于“已指定控制器”。)

从 FXML 的根元素中移除 fx:controller 属性:

<fx:root type = "AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" 
  minHeight="-Infinity" minWidth="-Infinity" onMouseClicked="#getTheTime"  prefHeight="198.0" prefWidth="208.0"  
  xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" >

(注意:可能还有其他错误:这是显而易见的错误。您应该在问题中包含堆栈跟踪以获得更完整的答案。)

【讨论】:

  • 非常感谢您的想法,现在我可以纠正我的错误,并且当我单击窗格时,我的程序会正确显示时间。此外,通过您的建议,我将能够节省几个小时,而不是重复我现在犯的错误。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 2015-03-31
  • 2016-02-01
  • 1970-01-01
  • 2021-02-27
  • 1970-01-01
相关资源
最近更新 更多