2017-03-17 34 views
1

時不關閉整個JVM我創建這樣一個圖表使用xchart Java庫(http://knowm.org/open-source/xchart/如何關閉xchart圖表

public void createHistogram(String title, String xTitle, String yTitle, ArrayList<String> xDataSet, ArrayList<Integer> yDataSet) { 
    CategoryChart chart = new CategoryChartBuilder().width(800).height(600).title(title).xAxisTitle(xTitle).yAxisTitle(yTitle).build(); 

    chart.getStyler().setLegendPosition(LegendPosition.InsideNW); 
    chart.getStyler().setHasAnnotations(true); 

    chart.addSeries("test1", xDataSet, yDataSet); 

    new SwingWrapper(chart).displayChart().setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

} 

但是每一次我按下圖的窗口上退出的時候,它會縮小整個應用。有沒有解決方法?

Ps。我嘗試將「JFrame」更改爲WindowsConstants,ApplicationFrame和SwingWrapper,以查看它是否對它有任何影響,但目前爲止沒有運氣。

+0

您是否試過'JFrame.HIDE_ON_CLOSE'? –

+0

是的。同樣的結果。同時在括號內自己嘗試了「HIDE_ON_CLOSE」和「DISPOSE_ON_CLOSE」,沒有運氣。 – peterxz

回答

1

我終於想出了答案。創建一個單獨的類,它有一個空的形式,就像這樣:

public GraphsInterface() { 
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    setBounds(100, 100, 850, 650); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    contentPane.setLayout(new BorderLayout(0, 0)); 
    setContentPane(contentPane); 
} 

添加一個子程序進入該類

public void createChart(CategoryChart chart) { 
    JPanel panelChart = new XChartPanel(chart); 
    contentPane.add(panelChart); 
    contentPane.validate(); 
} 

然後從那裏你正在創建圖表,只需要創建一個對象,並投影到contentPane

public void createHistogram(String title, String xTitle, String yTitle, ArrayList<String> xDataSet, ArrayList<Integer> yDataSet) { 
    CategoryChart chart = new CategoryChartBuilder().width(800).height(600).title(title).xAxisTitle(xTitle).yAxisTitle(yTitle).build(); 

    chart.getStyler().setLegendPosition(LegendPosition.InsideNW); 
    chart.getStyler().setHasAnnotations(true); 

    chart.addSeries("test1", xDataSet, yDataSet); 

    GraphsInterface graph = new GraphsInterface(); 
    graph.setVisible(true); 
    graph.createChart(chart); 

}