我正在为学校完成最后的编程任务,并希望以 JavaFx 中的动态饼图的形式在我的 java 应用程序中添加一个小的功能性视觉糖果。
使用 Netbeans 7.2 rc2,我从模板创建了一个 swing/javafx 混合应用程序。我扩展了 JPanelClass 以创建一个 shell,我的主应用程序可以使用它来访问 FX 功能。然而,我面临的问题是,虽然图表的初始化顺利进行,但当我尝试更新爆炸的东西时,它会生成一个空指针异常。这里我相信是相关的代码:
实例化:
public class PieChartJFxPanel extends JApplet {
private static final int JFXPANEL_WIDTH_INT = 500;
private static final int JFXPANEL_HEIGHT_INT = 500;
StackPane root = new StackPane();
PieChart athleteChart;
ObservableList<PieChart.Data> pieChartData =FXCollections.observableArrayList();
private static JFXPanel fxContainer;
private int fAthletes=0;
private int rAthletes=0;
public PieChartJFxPanel(int totalAthletes,int finishedAthletes){
super();
fAthletes=finishedAthletes;
rAthletes=totalAthletes-fAthletes;
}
@Override
public void init() {
fxContainer = new JFXPanel();
fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
add(fxContainer, BorderLayout.CENTER);
// create JavaFX scene
Platform.runLater(new Runnable() {
@Override
public void run() {
createScene();
}
});
}
场景构造器:
private void createScene() {
pieChartData.add(new PieChart.Data("Finished",fAthletes));
pieChartData.add(new PieChart.Data("Outstanding",rAthletes));
athleteChart=new PieChart();
athleteChart.setLegendVisible(false);
athleteChart.setTitle("Race Status");
//athleteChart.setAnimated(true);
athleteChart.setData(pieChartData);
root.getChildren().add(athleteChart);
fxContainer.setScene(new Scene(root));
//
}
以及产生异常的方法:
public void setData(int total, int in){
final ObservableList<PieChart.Data> pieChartDat=FXCollections.observableArrayList();
pieChartDat.add(new PieChart.Data("Finished",fAthletes));
pieChartDat.add(new PieChart.Data("Outstanding",rAthletes));
athleteChart.setData(pieChartDat);
}
还有一个例外:
Exception in thread "main" java.lang.NullPointerException
at piechartjpanel.PieChartJFxPanel.setData(PieChartJFxPanel.java:95)
at piechartjpanel.AthleteIOChart.setAthletes(AthleteIOChart.java:62)
at piechartjpanel.Test.setter(Test.java:34)
at piechartjpanel.Test.main(Test.java:48)
任何帮助表示赞赏:) 在此先感谢