我正在创建一个swing应用程序,并想向Jpanel添加一个JfreeChart。我创建了一种创建图表的方法,如下所示:
创建一个全局变量:
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();
}
});
由于某些原因,当我单击按钮时,图表未显示。
最佳答案
一堆脱离上下文的代码并不容易让您确切地了解自己在做什么,而是类似...
首先,您需要删除以前从UI中显示的所有内容,否则,您最终将在屏幕上看到多个图表,这可能会导致其他问题。
接下来,当您创建图表时,您需要实际添加到屏幕。它不会仅仅因为您创建新图表而“神奇地”更新
// 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;
}
}
至少应展示基本概念...
关于java - 将JFreeChart添加到JPanel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29290613/