JFreeChart并未按预期更新。
我有下面的代码。有2个面板,一个带有增量按钮,一个带有显示数据的图表。
每按一次 +1 按钮,就应该更新数据。但图表未显示新数据。
在此先感谢您的帮助。
public class GUItest3 extends JFrame {
private JPanel evoTabbedPanel, outputPanel;
float tempCtr;
public GUItest3(){
tempCtr = 0;
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
JButton incrementorButton = new JButton ("+1");
incrementorButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
tempCtr++;}
});
toolBar.add(incrementorButton);
evoTabbedPanel = new JPanel();
evoTabbedPanel.add(toolBar);
MyGraphPanel myGraphPanel = new MyGraphPanel();
outputPanel = new JPanel();
outputPanel.setLayout(new GridBagLayout());
outputPanel.add(myGraphPanel);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Generations", evoTabbedPanel);
tabbedPane.addTab("Output", outputPanel);
getContentPane().add(tabbedPane);
pack();
setVisible(true);
}
class MyGraphPanel extends JPanel {
JFreeChart myChart;
ChartPanel chartPanel;
XYSeriesCollection myDataset;
private MyGraphPanel(){
XYSeries myData = new XYSeries( "My data" );
myData.add(1.0, 1.0);
myData.add(2.0, tempCtr);
myDataset = new XYSeriesCollection();
myDataset.addSeries(myData);
myChart = ChartFactory.createXYLineChart("My chart", "X axis", "Y axis",
myDataset, PlotOrientation.VERTICAL, true, true, false);
chartPanel = new ChartPanel(myChart);
this.add(chartPanel);
}
}
}
答案 0 :(得分:1)
您的ActionListener
更新了tempCtr
,但是您需要更新统计图的数据集以更新统计图:
@Override
public void actionPerformed(ActionEvent e) {
XYSeries mySeries = myGraphPanel.myDataset.getSeries(0);
mySeries.addOrUpdate(tempCtr, Math.pow(2, tempCtr));
tempCtr++;
}
下面的完整示例说明了两次单击按钮后的结果;它着重于基本问题,并说明了一些其他要点:
在event dispatch thread上仅 构造和操作Swing GUI对象。
在封闭的getPreferredSize()
上覆盖JPanel
,如图here所示,以建立封闭面板的初始大小。
在默认约束下使用GridLayout
或GridBagLayout
,以使图表在调整封闭容器的大小时能够适应。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class GUItest3 {
float tempCtr;
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyGraphPanel myGraphPanel = new MyGraphPanel();
f.add(myGraphPanel, BorderLayout.CENTER);
f.add(new JButton(new AbstractAction("+1") {
@Override
public void actionPerformed(ActionEvent e) {
XYSeries mySeries = myGraphPanel.myDataset.getSeries(0);
mySeries.addOrUpdate(tempCtr, Math.pow(2, tempCtr));
tempCtr++;
}
}), BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
class MyGraphPanel extends JPanel {
JFreeChart myChart;
ChartPanel chartPanel;
XYSeriesCollection myDataset;
private MyGraphPanel() {
setLayout(new GridLayout());
XYSeries myData = new XYSeries("My data");
myData.add(tempCtr, tempCtr++);
myData.add(tempCtr, tempCtr++);
myDataset = new XYSeriesCollection();
myDataset.addSeries(myData);
myChart = ChartFactory.createXYLineChart("My chart", "X axis", "Y axis",
myDataset, PlotOrientation.VERTICAL, true, true, false);
chartPanel = new ChartPanel(myChart);
this.add(chartPanel);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(512, 256);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new GUItest3()::display);
}
}