java - 扩展 javafx.scene.chart.LineChart 时出现 NoSuchMethodException <init>()

标签 java javafx javafx-2

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

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

public LiveLineChart(Timeline animation, Axis<Number> xAxis, Axis<Number> yAxis)

public LiveLineChart(Timeline animation, Axis<Number> xAxis, Axis<Number> yAxis, ObservableList<Series<Number, Number>> data)

我的项目可以编译,但是,当我运行时,我得到:

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

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

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)

我缺少什么才能让它运行?

最佳答案

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

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,您需要一个无参构造函数或一个构建器类。无参构造函数不会为您留下任何初始化轴的机制(因为它们由您的父类(super class)管理并且是不可变的,即一旦调用父类(super class)构造函数就无法设置它们)。因此,您很可能需要定义以下内容:

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>

在您的 FXML 中。

关于java - 扩展 javafx.scene.chart.LineChart 时出现 NoSuchMethodException <init>(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24454936/

相关文章:

java - 如何在hibernate中使用命名查询访问子表字段

java - 如何在 Spring Batch 中组合多个监听器(步骤、读取、处理、写入和跳过)

browser - 使用 JavaFX WebView 下载

java - 使用容器框架使 javafx 场景拉伸(stretch)

java - JavaFX 中的多线程会挂起 UI

javafx-2 - JavaFX 中 JComponent 的替代方案

java - Guava CharMatcher - 扩展 ASCII 定义

java - 自动生成加特林场景

multithreading - javafx、套接字编程和线程

java - JavaFX 中 ComboBox 和 ChoiceBox 的区别