gpt4 book ai didi

java - JavaFX PieChart 问题

转载 作者:行者123 更新时间:2023-11-30 07:25:45 27 4
gpt4 key购买 nike

我有一个带有复选框和 2 个动画图表的应用程序:饼图和条形图。选中复选框时,将触发向图表添加新数据的方法。当复选框被 *un* 选中时,会触发从图表中删除数据的方法。

当我点击复选框时速度不是很快,一切正常。但是如果我在它的动画完成之前再次点击复选框,我会收到这个错误:

 java.lang.NullPointerException
at javafx.scene.chart.PieChart$7.handle(PieChart.java:370)
at javafx.scene.chart.PieChart$7.handle(PieChart.java:367)
at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:217)
at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:158)
at javafx.animation.Timeline.impl_playTo(Timeline.java:182)
at com.sun.scenario.animation.shared.SingleLoopClipEnvelope.timePulse(SingleLoopClipEnvelope.java:119)
at javafx.animation.Animation.impl_timePulse(Animation.java:953)
at com.sun.scenario.animation.shared.AnimationPulseReceiver.timePulse(AnimationPulseReceiver.java:117)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:366)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:289)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:454)
at com.sun.javafx.tk.quantum.QuantumToolkit$8.run(QuantumToolkit.java:325)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:62)
at java.lang.Thread.run(Unknown Source)

而这一切只发生在 PieChart 上。条形图正常工作。

这里是 checkBox onAction 事件的方法:

  public void OnCheckFired(ActionEvent event) {
Boolean b = checkBox.isSelected();
chart.modifyChart(currentChart,b);
}

Thant是一个代码,被触发,用于修改饼图:

@Override
public void makeModifications(Chart chart, Boolean b) {
PieChart pie = (PieChart) chart;
ObservableList<PieChart.Data> pieChartData = pie.getData();
pieChartData = modify(b, pieChartData);
pie.setData(pieChartData);
}

private ObservableList<Data> modify(Boolean b, ObservableList<PieChart.Data> pieChartData) {
if (b) {
pieChartData.add(new PieChart.Data("newValue", 34));
} else {
int size = pieChartData.size() - 1;
pieChartData.remove(size);
}
return pieChartData;
}

最佳答案

我已经修改了 Sergey 在他的回答中提到的错误问题所附的代码。将按钮替换为在动画播放时被禁用的复选框,并在动画结束时额外半秒。每次选择更改时,它都会以这种方式运行。

public class Tdesst extends Application {

private void init(Stage primaryStage) {
VBox root = new VBox();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
final ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Sun", 20),
new PieChart.Data("IBM", 12),
new PieChart.Data("HP", 25),
new PieChart.Data("Dell", 22),
new PieChart.Data("Apple", 30));
final PieChart chart = new PieChart(pieChartData);

final CheckBox chkbox = new CheckBox("add/remove data");

final Timeline animation = new Timeline(
new KeyFrame(Duration.seconds(.5),
new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent actionEvent) {
chkbox.setDisable(false);
}
}));
animation.setCycleCount(1);

chkbox.selectedProperty().addListener(new ChangeListener<Boolean>() {

@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldVal, Boolean newVal) {
chkbox.setDisable(true);
if (newVal) {
pieChartData.add(new PieChart.Data("newValue", 34));
} else {
pieChartData.remove(pieChartData.size() - 1);
}
animation.play();
}
});

root.getChildren().add(chart);
root.getChildren().add(chkbox);
}

@Override
public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
primaryStage.setTitle(VersionInfo.getRuntimeVersion());
}

public static void main(String[] args) {
launch(args);
}
}

或者你可以完全禁用动画

chart.setAnimated(false);

关于java - JavaFX PieChart 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10738052/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com