JavaFXSwing在尝试两次显示图表时崩溃
我正在制作一个JavaSwing应用程序,将javafx图表添加到JavaFXSwing在尝试两次显示图表时崩溃,java,swing,javafx,charts,Java,Swing,Javafx,Charts,我正在制作一个JavaSwing应用程序,将javafx图表添加到JFrame。单击按钮,在新窗口中显示图表。第一次一切正常,但在关闭图表窗口并再次单击按钮时,它会在显示下一个图表窗口前崩溃 fxPanel.setScene(scene); 我认为在第一次关闭图表窗口时,需要重置fxPanel或scene,但到目前为止,我所做的任何尝试都不起作用。下面是我的图表绘制课。非常感谢您的帮助 public class PlotChart extends JFrame implements Windo
JFrame
。单击按钮,在新窗口中显示图表。第一次一切正常,但在关闭图表窗口并再次单击按钮时,它会在显示下一个图表窗口前崩溃
fxPanel.setScene(scene);
我认为在第一次关闭图表窗口时,需要重置fxPanel
或scene
,但到目前为止,我所做的任何尝试都不起作用。下面是我的图表绘制课。非常感谢您的帮助
public class PlotChart extends JFrame implements WindowListener {
private JFrame chartWindow = new JFrame("Commitment Of Traders");
public PlotChart(JFXPanel fxPanel) {
plotChart(fxPanel);
}
public void plotChart(JFXPanel fxPanel) {
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
final LineChart<Number,Number> lineChart = new LineChart<Number,Number>(xAxis,yAxis);
final Scene scene = new Scene(lineChart,800,600);
xAxis.setLabel("Week");
lineChart.setTitle("Commitment of Traders");
//Define a series
XYChart.Series commercials = new XYChart.Series<>();
XYChart.Series nonCommercials = new XYChart.Series<>();
XYChart.Series CLong = new XYChart.Series<>();
XYChart.Series CShort = new XYChart.Series<>();
commercials.setName("Dealers");
nonCommercials.setName("Hedge Funds");
//Populate series
for (int i=0;i<ReadCSV.getCommercials().size();i++) {
commercials.getData().add(new XYChart.Data(i,ReadCSV.getCommercials().get(i)));
nonCommercials.getData().add(new XYChart.Data(i,ReadCSV.getNonCommercials().get(i)));
}
lineChart.getData().add(commercials);
lineChart.getData().add(nonCommercials);
fxPanel.setScene(scene); // exception in this line
chartWindow.add(fxPanel);
chartWindow.setSize(800, 600);
chartWindow.setVisible(true);
chartWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
chartWindow.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
ReadCSV.clearVars();
System.out.println("window closed");
}
});
}
公共类PlotChart扩展JFrame实现WindowListener{
私人JFrame chartWindow=新JFrame(“交易者承诺”);
公共绘图仪(JFXPanel fxPanel){
绘图仪(fxPanel);
}
公共无效绘图图(JFXPanel fxPanel){
最终数字axis xAxis=新数字axis();
最终数字axis yAxis=新数字axis();
最终线形图线形图=新线形图(xAxis,yAxis);
最终场景=新场景(线条图,800600);
xAxis.setLabel(“周”);
线形图。setTitle(“交易者承诺”);
//定义一个系列
XYChart.Series commercials=新的XYChart.Series();
XYChart.Series非商业=新的XYChart.Series();
XYChart.Series CLong=新的XYChart.Series();
XYChart.Series CShort=新的XYChart.Series();
商业广告。设置名称(“经销商”);
非商业性。setName(“对冲基金”);
//填充系列
对于(int i=0;i我刚才也遇到了这个问题。我通过每次创建一个新的JFXPanel并因此在每个实例上只设置一次场景,成功地解决了这个问题。
它解决了这个问题,但可能不是解决这个问题的最优雅的方法。欢迎使用Stackoverflow!请在问题中引用堆栈跟踪或错误消息(将堆栈跟踪格式化为代码)。嗨,Ole,感谢您的回复,没有错误消息,它只是冻结。我尝试了使用try/catch,但仍然没有给出错误。。。