这类似于我昨天问过的问题,但更具体地涉及到问题。 在已包含各种小部件的NetBeans项目中添加JFreeChart的正确方法是什么? 我的updateChart()隐藏了整个JFrame。 我想将JFreeChart添加到JFrame中。
public class MyClass extends javax.swing.JFrame implements TableModelListener {
public MyClass() {
initComponents();
...
updateChart();
}
private void updateChart() {
XYDataset dataset = createXYdataset();
JFreeChart chart = createChart(dataset);
JPanel chartPanel = new ChartPanel(chart);
setContentPane(chartPanel);
}
private XYDataset createXYdataset() {
XYSeries series = new XYSeries("");
int rows = jTable.getRowCount();
if (rows > 0) {
int ms = 0;
for (int row = 0; row < rows; row++) {
series.add(ms, 1);
ms += Integer.parseInt(
jTable.getValueAt(row, PULSE_ON).toString());
series.add(ms, 1);
series.add(ms, 0);
ms += Integer.parseInt(
jTable.getValueAt(row, PULSE_OFF).toString());
series.add(ms, 0);
}
}
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series);
return dataset;
}
private JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createXYLineChart(
null, // chart title
"ms", // x axis label
null, // y axis label
dataset, // data
PlotOrientation.VERTICAL,
false, // include legend
true, // tooltips
false // urls
);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
plot.setRangeGridlinesVisible(false);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
return chart;
}
}
修正后的代码:
private void updateChart() {
XYDataset dataset = createXYdataset();
JFreeChart chart = createChart(dataset);
JPanel chartPanel = new ChartPanel(chart);
chartPanel.setSize(jPanel1.getSize());
jPanel1.add(chartPanel);
jPanel1.getParent().validate();
}
updateChart()
中添加了jPanel1.add(chartPanel); jPanel1.getParent().validate(); and jPanel1.setVisible(true);
,但图表在jPanel1中不可见。还有其他想法吗? - jacknadchartPanel.setSize(jPanel1.getSize());
就可以了。 - jacknad