简体   繁体   English

将ChartPanel添加到JPanel

[英]Adding a ChartPanel to 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);

So the problem is that the jPanel4 with a chart is not visible. 因此,问题在于带有图表的jPanel4不可见。 When I add my chartpanel to a frame and make it visible, it works. 当我将图表面板添加到框架中并使其可见时,它可以工作。

Anyone knows what's my mistake? 有人知道我的错误吗?

This works perfectly fine for me: 这对我来说很好用:

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);
    }
}

Can you provide us with a bit more code? 您能为我们提供更多代码吗? Do you put something else into jPanel4 ? 您是否在jPanel4了其他jPanel4 There can not be more than one component in every spot ( NORTH, SOUTH, WEST, EAST, CENTER ). 每个地点( NORTH, SOUTH, WEST, EAST, CENTER )的组成部分不得超过一个。 Do you put your panel into a frame? 您是否将面板放在框架中?

do u have anything in CENTER Layout in jpanel else try adding chart in center 您是否在jpanel的CENTER布局中有任何内容,否则尝试在中心添加图表

ChartPanel chartpanel = new ChartPanel(chart);
chartpanel.setDomainZoomable(true);
jPanel4.add(chartpanel, BorderLayout.CENTER);

NORTH is actually top of the container. NORTH实际上是容器的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM