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

编程入门 行业动态 更新时间:2024-10-11 05:30:02
本文介绍了扩展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;()

本文发布于:2023-11-25 06:37:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628696.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:chart   LineChart   javafx   scene   NoSuchMethodException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!