0

如何访问XYSeriesXYPlot放置在JFrame?当然,我可以使用变量名seriesand plot,但我的问题是指访问这些组件的函数式方式,即f.getContentPane()……这在函数返回时很有用JFrame

JFrame f = new JFrame(title);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout(0, 5));
XYSeries series = new XYSeries("");
XYDataset data = createDataset(series,0,indf,oldpop);
JFreeChart chart = ChartFactory.createScatterPlot(title, xtitle, ytitle, data, PlotOrientation.VERTICAL, false, true, false);
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer =
                (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);          plot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
ChartPanel chartPanel = new ChartPanel(chart);
f.add(chartPanel, BorderLayout.CENTER);
chartPanel.setMouseWheelEnabled(true);
chartPanel.setHorizontalAxisTrace(true);
chartPanel.setVerticalAxisTrace(true);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
f.add(panel, BorderLayout.SOUTH);
4

1 回答 1

2

ChartPanelJComponent哪个呈现JFreeChart;两者XYSeriesXYPlot都是组件,这些对象由JFreeChart. 由于它们不是组件,因此您无法通过遍历组件层次结构来访问它们,您必须从ChartPanel和获取它们JFreeChart

ChartPanel在组件层次结构中查找,getChart()用于获取JFreeChart对象,然后从中获取您想要的对象,就像您在上面的代码中所做的那样:

XYPlot plot = chart.getXYPlot();
于 2013-04-23T10:27:00.123 回答