簡體   English   中英

嘗試兩次顯示圖表時,javafx swing崩潰

[英]javafx swing crashes when trying to display chart twice

我正在使用將javafx圖表添加到JFrame的Java swing應用程序。 單擊一個按鈕,它將在新窗口中顯示一個圖表。 第一次都可以正常工作,但是在關閉圖表窗口並再次單擊按鈕時,它會崩潰,然后在該行顯示下一個圖表窗口

fxPanel.setScene(scene);

我認為第一次關閉圖表窗口時需要重置fxPanelscene ,但是到目前為止我沒有嘗試過。 下面是我的圖表繪制類。 任何幫助,不勝感激。

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");
            }
        });
    }

WindowListener方法重寫忽略

我剛才也遇到了這個問題。 通過每次創建一個新的JFXPanel並因此在每個實例上僅設置一次場景,我設法擺脫了它。 它可以解決問題,但可能不是解決此問題的最優雅的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM