[英]Adding ChartPanel to jPanel not working
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. 当我将此面板添加到jFrame时,它不会显示图表。
I'm pretty confident that the problem is lie within the jPanel implementation. 我很确定问题出在jPanel实现内。
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, ie a ChartPanel
, to an existing container dynamically. 看来您正在尝试向现有容器动态添加新的视图组件,即
ChartPanel
。 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. 尽管从技术上来说可以使用
add()
, validate()
和repaint()
在运行时添加新组件,但是随着应用程序的发展,结果的伸缩性很差。
Alternatively, add the view component before invoking pack()
on the enclosing container, as shown here , and update the corresponding model as new data arrives; 可替换地,在调用之前添加视图分量
pack()
封闭容器上,如图这里 ,并更新相应的模型作为新数据到达; 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()
. 如有必要,您始终可以使用
setChart()
替换图表面板随附的图表。 Finally, consider CardLayout
if different charts need different controls. 最后,如果不同的图表需要不同的控件,请考虑使用
CardLayout
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.