java 将 ChartPanel 添加到 JPanel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12318488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:25:43  来源:igfitidea点击:

Adding a ChartPanel to JPanel

javanetbeansjpaneljfreechart

提问by Doszi89

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. When I add my chartpanel to a frame and make it visible, it works.

所以问题是带有图表的jPanel4是不可见的。当我将图表面板添加到框架并使其可见时,它可以工作。

Anyone knows what's my mistake?

有谁知道我的错误是什么?

回答by brimborium

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? There can not be more than one component in every spot (NORTH, SOUTH, WEST, EAST, CENTER). Do you put your panel into a frame?

你能给我们提供更多的代码吗?你把别的东西放进去了jPanel4吗?每个点 ( NORTH, SOUTH, WEST, EAST, CENTER) 中不能有多个组件。您是否将面板放入框架中?

回答by vels4j

do u have anything in CENTERLayout in jpanel else try adding chart in center

CENTER在 jpanel 的布局中有什么东西吗,否则尝试在中心添加图表

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

NORTHis actually top of the container.

NORTH实际上是容器的顶部。