java 如何在 NetBeans 项目中显示 JFreeChart
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3969203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display a JFreeChart in a NetBeans project
提问by Hymannad
This is similar to a question I asked yesterday but more specific to the problem. What is the correct method to add a JFreeChart to a NetBeans project which already contains various widgets? My updateChart() hides the entire JFrame. I'd like to add the JFreeChart to the JFrame.
这类似于我昨天问的一个问题,但更具体地针对该问题。将 JFreeChart 添加到已包含各种小部件的 NetBeans 项目的正确方法是什么?我的 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;
}
}
Corrected code:
更正的代码:
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();
}
回答by camickr
My updateChart() hides the entire JFrame.
我的 updateChart() 隐藏了整个 JFrame。
JFreeChart chart = createChart(dataset);
JPanel chartPanel = new ChartPanel(chart);
setContentPane(chartPanel);
That would be because you are replacing the content pane of your frame with the panel from free chart.
那是因为您正在用免费图表中的面板替换框架的内容面板。
I don't know what layout manager you are using, but you need to "ADD" the free chart panel to the panel that contains all the other components. So maybe when you design the general form in Netbeans you add an empty panel to the place where you want the free chart panel to be added. Then when you add the free chart panel the code would be something like:
我不知道您使用的是什么布局管理器,但您需要将免费图表面板“添加”到包含所有其他组件的面板中。因此,也许当您在 Netbeans 中设计通用表单时,您可以在要添加免费图表面板的位置添加一个空面板。然后,当您添加免费图表面板时,代码将类似于:
emptyFreeChartPanel.add( chartPanel );
emptyFreeChartPanel.getParent().validate();
The validate tells Swing that components have been added so the layout manager will be invoked.
验证告诉 Swing 组件已添加,因此将调用布局管理器。