2017-03-17 72 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); 

}