我遇到了 JavaFX ScatterChart 组件的恼人错误。我想创建一个不断获得更多积分的图表。为此,我首先创建一个新系列,将其添加到图表中,然后开始向该系列添加点。
这在 LineChart 中工作正常。然而,出于某种原因,IndexOutOfBoundsException
当我尝试对 ScatterChart 执行相同操作时,我得到了一个。
这是针对这种情况的一个孤立的测试用例:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class Test extends Application {
@Override
public void start(Stage stage) {
stage.setTitle("Scatter Chart");
NumberAxis xAxis = new NumberAxis(0, 10, 1);
NumberAxis yAxis = new NumberAxis(0, 10, 1);
ScatterChart<Number, Number> chart = new ScatterChart<Number, Number>(xAxis,yAxis);
XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
chart.getData().add(series);
series.getData().add(new XYChart.Data<Number, Number>(4.2, 2.4));
Scene scene = new Scene(chart, 500, 400);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是异常的相关部分:
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:208)
at javafx.scene.chart.ScatterChart.updateLegend(ScatterChart.java:188)
at javafx.scene.chart.XYChart$2.onChanged(XYChart.java:132)
at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:134)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:48)
at com.sun.javafx.collections.ObservableListWrapper.callObservers(ObservableListWrapper.java:97)
at com.sun.javafx.collections.ObservableListWrapper.add(ObservableListWrapper.java:154)
at com.sun.javafx.collections.ObservableListWrapper.add(ObservableListWrapper.java:144)
at Test.start(Test.java:20)
有谁知道为什么会这样?