本文介绍了将ChartPanel添加到JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我这里有一些不起作用的代码:
I've got some not working code here:
XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries(series); JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, dataset, PlotOrientation.HORIZONTAL, true, true, true); ChartPanel chartpanel = new ChartPanel(chart); chartpanel.setDomainZoomable(true); jPanel4.setLayout(new BorderLayout()); jPanel4.add(chartpanel, BorderLayout.NORTH);因此,问题在于带有图表的jPanel4不可见.当我将图表面板添加到框架中并使其可见时,它将起作用.
So the problem is that the jPanel4 with a chart is not visible. When I add my chartpanel to a frame and make it visible, it works.
有人知道我的错误吗?
Anyone knows what's my mistake?
推荐答案这对我来说效果很好:
import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; public class Main { public static void main(String[] args) { XYSeries series = new XYSeries("asdf"); for (int i = 0; i < 100; i++) series.add(i, Math.random()); XYSeriesCollection dataset = new XYSeriesCollection(series); JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, dataset, PlotOrientation.HORIZONTAL, true, true, true); ChartPanel chartpanel = new ChartPanel(chart); chartpanel.setDomainZoomable(true); JPanel jPanel4 = new JPanel(); jPanel4.setLayout(new BorderLayout()); jPanel4.add(chartpanel, BorderLayout.NORTH); JFrame frame = new JFrame(); frame.add(jPanel4); frame.pack(); frame.setVisible(true); } }您能为我们提供更多代码吗?您是否将其他内容放入jPanel4?每个点(NORTH, SOUTH, WEST, EAST, CENTER)中最多只能有一个组件.您是否将面板放置在框架中?
Can you provide us with a bit more code? Do you put something else into jPanel4? There can not be more than one component in every spot (NORTH, SOUTH, WEST, EAST, CENTER). Do you put your panel into a frame?
更多推荐
将ChartPanel添加到JPanel
发布评论