Java 获得对JFrame组件的访问权限
如何访问放置在Java 获得对JFrame组件的访问权限,java,swing,jframe,jfreechart,Java,Swing,Jframe,Jfreechart,如何访问放置在JFrame上的XYSeries和XYPlot?当然,我可以使用变量名series和plot,但我的问题是访问这些组件的功能方式,即f.getContentPane()。。。当函数返回JFrame时,这非常有用 JFrame f = new JFrame(title); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new BorderLayout(0, 5)); XYSeries series = ne
JFrame
上的XYSeries
和XYPlot
?当然,我可以使用变量名series
和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);
ChartPanel
是JComponent
,它呈现JFreeChart
;XYSeries
和XYPlot
都是组件,这些对象由JFreeChart
使用。由于它们不是组件,您无法通过遍历组件层次结构来访问它们,您必须从ChartPanel
和JFreeChart
获取它们
在组件层次结构中查找ChartPanel
,使用getChart()
获取JFreeChart
对象,然后从中获取所需的对象,就像在上面的代码中一样:
XYPlot plot = chart.getXYPlot();
这将把
对象定义为局部变量,而不是在for(Component comp:container.getComponents()){`的数组中迭代,并使用实例测试对象(comp instanceof XYSeries){/…}。由于组件(不兼容…)的原因,无法对其进行编译。在执行(或禁用)instanceof之前,WHAT返回调试器、对象类型,而不是XxxChart或XxxGraph用户,但可以基于AWT容器,