java - 扩展 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>
我该如何解决我的问题?
诺塔贝内
- 我检查了我的 FXML 文件上的包导入。
- 当我删除 2 个被覆盖的方法时,我得到了相同的结果。
解决方案
最后,我通过查看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();
}
}
推荐阅读
- pandas - pandas 在同一张表(相同的数据框)中,如何用新名称和其他行值的总和对不同的行进行分组
- r - Tensorflow 的嵌入问题:dimnames(x) <- dn 中的错误:“dimnames”[1] 的长度不等于数组范围
- android - App crashes with "java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 " only on real device
- react-native - 如何在 React 本机标头中添加 CSRF 令牌
- django - onclick="return Confirm('确定要删除吗?');" 不工作
- javascript - 将 SQL Server 数据加载到前端时出错
- java - 异常说明:xxx 使用非实体 xx 作为关系属性中的目标实体 [方法 xxx]
- r - 通过 data.table 中的可变间隔计算股票的回报
- android - 类似于单选按钮的按钮交互的良好代码设计是什么?
- r - 在 ggplot 版本 3+ 中反转日期时间(POSIXct 数据)轴