我使用xchart java库(http://knowm.org/open-source/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);
}
然而,每当我按下图表窗口上的退出时,它就会关闭整个应用程序。有解决方法吗?
聚苯乙烯。我尝试过改变&#34; JFrame&#34;到WindowsConstants,ApplicationFrame和SwingWrapper查看它是否对它有任何影响,但到目前为止没有运气。
答案 0 :(得分:2)
我终于找到了答案。使用空表单创建一个单独的类,如下所示:
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);
}