扩展javafx.scene.chart.LineChart时出现NoSuchMethodException< init>() [英] NoSuchMethodException <init>() when extending javafx.scene.chart.LineChart

查看:200
本文介绍了扩展javafx.scene.chart.LineChart时出现NoSuchMethodException< init>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展 javafx.scene.chart.LineChart 以添加一些额外的功能。

I am trying to extend javafx.scene.chart.LineChart in order to add some extra features.

我已经实现了两个构造函数

I have implemented the two constructors

public LiveLineChart(时间轴动画,Axis< Number> xAxis,Axis< Number> yAxis)

public LiveLineChart(时间轴动画,Axis< Number> xAxis,Axis< Number> yAxis,ObservableList< Series< Number,Number>> data)

我的项目编译,但是,当我运行,我明白了:

My project compiles, however, when I run, I get this:

Caused by: java.lang.NoSuchMethodException: org.mypackage.LiveLineChart.<init>()
at java.lang.Class.getConstructor0(Class.java:2971)
at java.lang.Class.newInstance(Class.java:403)
... 20 more

如果我尝试实现默认(空)构造函数,我会收到编译错误:

If I try to implement a default (empty) constructor, I get a compile error:

no suitable constructor found for LineChart(no arguments)
constructor LineChart.LineChart(Axis<Number>,Axis<Number>) is not applicable
  (actual and formal argument lists differ in length)
constructor LineChart.LineChart(Axis<Number>,Axis<Number>,ObservableList<Series<Number,Number>>) is not applicable
  (actual and formal argument lists differ in length)

我错过了什么才能让它运行?

What am I missing to be able to get this to run?

推荐答案

LineChart 没有默认构造函数,所以你需要调用一个它从您定义的构造函数中显式声明的构造函数。看看你声明的构造函数,你可能需要这样的东西:

LineChart has no default constructor, so you need to invoke one of the constructors that it declares explicitly from a constructor you define. Looking at the constructors you've said you declared, you probably need something like this:

public LiveLineChart(Timeline animation, Axis<Number> xAxis, Axis<Number> yAxis) {
    super(xAxis, yAxis);
    // ...
}

public LiveLineChart(Timeline animation, Axis<Number> xAxis, Axis<Number> yAxis, ObservableList<Series<Number, Number>> data) {
    super(xAxis, yAxis, data) ;
    // ...
}

如果你想能够从 FXML 创建 LiveLineChart ,您需要一个无参数构造函数或一个构建器类。无参数构造函数不会给你任何初始化轴的机制(因为它们由你的超类管理并且是不可变的,即一旦调用超类构造函数就没有办法设置它们)。所以你很可能需要定义以下内容:

If you want to be able to create a LiveLineChart from FXML, you either need a no-argument constructor, or a builder class. A no-argument constructor will not leave you any mechanism to initialize the axes (since they are managed by your superclass and are immutable, i.e. there is no way to set them once the superclass constructor has been invoked). So you most likely need to define the following:

public class LiveLineChartBuilder {
    private Axis<Number> xAxis ;
    private Axis<Number> yAxis ;
    private Timeline animation ;
    private ObservableList<Series<Number,Number>> data ;

    public static LiveLineChartBuilder create() {
        return new LiveLineChartBuilder();
    }

    public LiveLineChartBuilder xAxis(Axis<Number> xAxis) {
        this.xAxis = xAxis ;
        return this ;
    }

    public LiveLineChartBuilder yAxis(Axis<Number> yAxis) {
        this.yAxis = yAxis ;
        return this ;
    }

    public LiveLineChartBuilder animation(Timeline animation) {
        this.animation = animation ;
        return this ;
    }

    public LiveLineChartBuilder data(Series<Number, Number> data) {
        this.data = data ;
        return this ;
    }

    public LiveLineChart build() {
        // if else may not be necessary, depending on how you define constructors in LiveLineChart
        if (data == null) {
            return new LiveLineChart(animation, xAxis, yAxis);
        } else {
            return new LiveLineChart(animation, xAxis, yAxis, data);
        }
    }
}

这样你就能做到

<LiveLineChart>
<xAxis><NumberAxis><!-- ... --></NumberAxis></xAxis>
<!-- etc -->
</LiveLineChart>

这篇关于扩展javafx.scene.chart.LineChart时出现NoSuchMethodException&lt; init&gt;()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆