将 ChartPanel 添加到 jPanel 不起作用
Adding ChartPanel to jPanel not working
我得到了这个面板:
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>
}
当我将此面板添加到 jFrame 时它不显示图表。
我非常确信问题出在 jPanel 实现中。
谁能给我一些指点。 (其他面板到现在好像没问题)
您似乎正在尝试向现有容器动态添加新的视图组件,即 ChartPanel
。尽管在技术上 possible 使用 add()
、validate()
和 repaint()
在运行时添加新组件,但随着应用程序的发展,结果扩展性很差。
或者,添加视图组件before 在封闭容器上调用pack()
,如图所示, 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 and pictured below. If necessary, you can always replace the chart panel's enclosed chart using setChart()
. Finally, consider CardLayout
如果不同的图表需要不同的控件。
我得到了这个面板:
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>
}
当我将此面板添加到 jFrame 时它不显示图表。
我非常确信问题出在 jPanel 实现中。
谁能给我一些指点。 (其他面板到现在好像没问题)
您似乎正在尝试向现有容器动态添加新的视图组件,即 ChartPanel
。尽管在技术上 possible 使用 add()
、validate()
和 repaint()
在运行时添加新组件,但随着应用程序的发展,结果扩展性很差。
或者,添加视图组件before 在封闭容器上调用pack()
,如图所示setChart()
. Finally, consider CardLayout
如果不同的图表需要不同的控件。