首页 > 解决方案 > 扩展 JavaFX LineChart 时出现异常

问题描述

我想扩展 JavaFXLineChart以添加一些功能(授予对图例的访问权限,以便对其进行自定义、添加可见边界等)。我创建了一个EnhancedLineChart类并在我的 FXML 文件上声明了一个实例,但它返回了一个异常:

原因:com.sun.javafx.fxml.PropertyNotFoundException:属性“xAxis”不存在或为只读。

这是我的

package com.ratp.oam.widgets.graph;

import com.sun.javafx.charts.Legend;

import javafx.collections.ObservableList;
import javafx.scene.chart.Axis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;

public class EnhancedLineChart extends LineChart<Number, Number> {
    public EnhancedLineChart() {
        super(new NumberAxis(), new NumberAxis());
    }

    public EnhancedLineChart(final NumberAxis xAxis, final NumberAxis yAxis) {
        super(xAxis, yAxis);
    }

    public EnhancedLineChart(final NumberAxis xAxis, final NumberAxis yAxis,
            ObservableList<Series<Number, Number>> data) {
        super(xAxis, yAxis, data);
    }

    @Override
    public Axis<Number> getXAxis() {
        return super.getXAxis();
    }

    @Override
    public Axis<Number> getYAxis() {
        return super.getYAxis();
    }

    /**
     * Fournit la légende du graphique telle qu'elle a été implémentée dans la
     * classe {@link LineChart}.
     * 
     * @return
     */
    public Legend legend() {
        return (Legend) super.getLegend();
    }
}

以及实例化我的组件的FXML 文件(DataGraph.fxml):

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

<?import com.ratp.oam.widgets.graph.EnhancedLineChart?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<fx:root type="javafx.scene.layout.HBox" xmlns:fx="http://javafx.com/fxml/1" spacing="3">
    <ScrollPane fitToWidth="true" HBox.hgrow="ALWAYS" styleClass="scroll-pane">
        <fx:define>
            <EnhancedLineChart fx:id="analogicalChart" createSymbols="false" animated="false" prefHeight="350">
                <padding>
                    <Insets topRightBottomLeft="0" />
                </padding>

                <xAxis>
                    <NumberAxis fx:id="analogicalXAxis" animated="false" side="BOTTOM" forceZeroInRange="false" autoRanging="false" />
                </xAxis>

                <yAxis>
                    <NumberAxis animated="false" side="LEFT" forceZeroInRange="false" autoRanging="false" />
                </yAxis>
            </EnhancedLineChart>
        </fx:define>

        <VBox fx:id="graphBox">
            <VBox fx:id="logicalPane" />
        </VBox>
    </ScrollPane>
</fx:root>

我该如何解决我的问题?

诺塔贝内

标签: javajavafxpropertiesjavafx-2linechart

解决方案


最后,我通过查看LineChart课程找到了解决方案。如果我想使用xAxis,yAxis并且data在我的 FXML 文件中,我需要将@NamedArg注释放在构造函数参数之前,以便 FXML 加载器可以调用实例。结果如下所示:

package com.ratp.oam.widgets.graph;

import com.sun.javafx.charts.Legend;

import javafx.beans.NamedArg;
import javafx.collections.ObservableList;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;

public class EnhancedLineChart extends LineChart<Number, Number> {
    public EnhancedLineChart(@NamedArg("xAxis") NumberAxis xAxis, @NamedArg("yAxis") NumberAxis yAxis) {
        super(xAxis, yAxis);
    }

    public EnhancedLineChart(@NamedArg("xAxis") NumberAxis xAxis, @NamedArg("yAxis") NumberAxis yAxis, @NamedArg("data") ObservableList<Series<Number, Number>> data) {
        super(xAxis, yAxis, data);
    }

    /**
     * Fournit la légende du graphique telle qu'elle a été implémentée dans la
     * classe {@link LineChart}.
     */
    public Legend legend() {
        return (Legend) super.getLegend();
    }
}

推荐阅读