扩展JavaFX LineChart时发生异常

问题描述 投票:0回答:1

我想扩展JavaFX LineChart以添加一些功能(允许访问图例,以便可以对其进行自定义,添加可见范围等)。我创建了一个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>

我该如何解决我的问题?

Nota Bene

  • 我已经在我的FXML文件中检查了软件包的导入。
  • 删除两个覆盖的方法时,我得到相同的结果。
java javafx properties javafx-2 linechart
1个回答
0
投票

如果我想在FXML文件中使用xAxisyAxisdata属性,则需要使用@NamedArg批注在构造函数中声明它们。结果看起来像这样:

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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.