将ChartPanel添加到jPanel不起作用 [英] Adding ChartPanel to jPanel not working

查看:154
本文介绍了将ChartPanel添加到jPanel不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个小组:

 公共类StripchartPanel扩展javax.swing.JPanel {
public StripchartPanel() {
XYSeries系列=新的XYSeries(XYGraph);
series.add(1,1);
series.add(1,2);
series.add(2,1);
series.add(3,9);
series.add(4,10);

//将系列添加到数据集
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(系列);

//生成图形
JFreeChart图表= ChartFactory.createXYLineChart(
XY图表,//标题
x轴,// x- axis Label
y轴,// y轴标签
数据集,//数据集
PlotOrientation.VERTICAL,//绘图方向
true,//显示图例
true,//使用工具提示
false //配置图表以生成URL?
);
ChartPanel CP =新ChartPanel(图表);
this.add(CP,BorderLayout.CENTER);
this.validate();
initComponents();

}


@SuppressWarnings(未选中)
//< editor-fold defaultstate =collapseddesc =生成代码 >
private void initComponents(){

setLayout(new java.awt.BorderLayout());
} //< / editor-fold>

}

我添加此面板时,它不会显示图表一个jFrame。



我非常有信心问题出在jPanel实现中。



有人可以给我一些指针。 (其他面板到目前为止似乎没有问题)

解决方案

看起来您正在尝试添加新的视图组件,即 ChartPanel ,动态地对现有容器。虽然技术上


I got this panel:

public class StripchartPanel extends javax.swing.JPanel {
public StripchartPanel() {
    XYSeries series = new XYSeries("XYGraph");
    series.add(1, 1);
    series.add(1, 2);
    series.add(2, 1);
    series.add(3, 9);
    series.add(4, 10);

// Add the series to your data set
    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series);

// Generate the graph
    JFreeChart chart = ChartFactory.createXYLineChart(
            "XY Chart", // Title
            "x-axis", // x-axis Label
            "y-axis", // y-axis Label
            dataset, // Dataset
            PlotOrientation.VERTICAL, // Plot Orientation
            true, // Show Legend
            true, // Use tooltips
            false // Configure chart to generate URLs?
    );
    ChartPanel CP = new ChartPanel(chart);
    this.add(CP, BorderLayout.CENTER);
    this.validate();
    initComponents();

}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    setLayout(new java.awt.BorderLayout());
}// </editor-fold>                        

}

It does not show the chart when I add this panel to a jFrame.

I'm pretty confident that the problem is lie within the jPanel implementation.

Can someone give me some pointer. (Other panels does not seem to have a problem until now)

解决方案

It looks like you are trying to add a new view component, i.e. a ChartPanel, to an existing container dynamically. Although it is technically possible to add a new component at runtime using add(), validate() and repaint(), the result scales poorly as the application evolves.

Alternatively, add the view component before invoking pack() on the enclosing container, as shown here, and update the corresponding model as new data arrives; the listening view will update itself in response. A related example showing multiple strip charts is shown here and pictured below. If necessary, you can always replace the chart panel's enclosed chart using setChart(). Finally, consider CardLayout if different charts need different controls.

这篇关于将ChartPanel添加到jPanel不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆