chart放入panel_将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);

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?

解决方案

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?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值