我正在创建一个swing应用程序,并希望将JfreeChart添加到Jpanel。我创建了一个方法,我可以在下面创建图表:
创建了一个全局变量:
JFreeChart chart;
创建图表的方法:
public void createChart(){
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
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并将其添加到附加到JFrame的JPanel:
JPanel reporting = new JPanel();
ChartPanel CP = new ChartPanel(chart);
reporting.add(CP,BorderLayout.CENTER);
reporting.validate();
reporting.setBounds(47, 59, 921, 439);
frame.getContentPane().add(reporting);
reporting.setLayout(new BorderLayout(0, 0));
我还创建了一个带有动作监听器的按钮,我在其中调用该方法,如下所示:
JButton btnReporting = new JButton("Reporting");
btnReporting.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
createChart();
}
});
出于某种原因,当我点击按钮时,图表不会显示。
答案 0 :(得分:2)
一堆脱离上下文的代码并不能让你很容易理解你在做什么,而是像......一样......
首先,您需要删除以前在UI中显示的任何内容,否则您最终会在屏幕上显示多个图表,这可能会导致其他问题。
接下来,在创建图表时,您需要实际添加到屏幕。它不会神奇地赢得#34;更新只是因为您创建了一个新图表
// import free chart classes
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JPanel reportingPane;
public TestPane() {
setLayout(new BorderLayout());
reportingPane = new JPanel(new BorderLayout());
JButton btnReporting = new JButton("Reporting");
btnReporting.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFreeChart chart = createChart();
reportingPane.removeAll();
reportingPane.add(new ChartPanel(chart));
revalidate();
repaint();
}
});
add(reportingPane);
add(btnReporting, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
}
public JFreeChart createChart() {
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?
);
return chart;
}
}
至少应该展示基本概念......