无法将我的 Java FreeChart 放入 JPanel
Trouble putting my Java FreeChart into the JPanel
我有以下代码:但是我似乎无法将我的输出放入我创建的 JPanel 中,我已经尝试过但我可以创建图表并使其显示在单独的图表框架中 window或者它根本没有出现,有人可以帮忙吗?
package javaapplication1;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class JavaApplication1 extends JFrame implements ActionListener {
JPanel panel;
Container window = getContentPane();
ChartFrame cframe;
private ArrayList<Solar> sols;
public static void main(String[] args) throws IOException {
JavaApplication1 demo = new JavaApplication1();
demo.setPreferredSize(new Dimension(1000,500));
demo.createGUI();
demo.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
window.setLayout(new FlowLayout());
panel = new JPanel();
JButton button = new JButton("Get Forecast");
panel.setBackground(Color.white);
panel.setPreferredSize(new Dimension(1000,400));
//ChartFrame cframe = new ChartFrame("First", chart);
window.add(panel);
window.add(button);
button.addActionListener(this);
pack();
}
@Override
public void actionPerformed(ActionEvent e) {
sols = new ArrayList();
BufferedReader crunchifyBuffer = null;
final String DELIMITER = ",";
try {
crunchifyBuffer = new BufferedReader(new FileReader("C:\Users\xxxx\Documents\Molar Forecasts\fs-rad.20170421.21.csv"));
} catch (FileNotFoundException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
String line = null;
try {
line = crunchifyBuffer.readLine(); // Read first line
} catch (IOException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
if (line != null) { try {
// Check it's not null
// line = crunchifyBuffer.readLine(); // Read second line
while ((line = crunchifyBuffer.readLine()) != null)
{
//Get all tokens available in line
String[] tokens = line.split(DELIMITER);
Solar sol = new Solar(tokens[0], tokens[1], tokens[2], Double.parseDouble(tokens[3]));
sols.add(sol);
} } catch (IOException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
processdata();
}}
public void processdata() {
System.out.println(sols.size());
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int index = 1 ; index <sols.size ();index++){
dataset.addValue(sols.get(index).details1(), "Molar Irradiation", (sols.get(index).details()));
}
JFreeChart chart = ChartFactory.createBarChart(
"Bar Chart Demo",
// chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include
true, //
false // URLs?
);
// create and display a frame...
add(panel);
pack();
//panel.add(cframe,BorderLayout.CENTER);
panel.validate();
//
//
setVisible(true);
}
}
在您的示例中,您永远不会 add()
chart
到 JFrame
、JavaApplication1
所包含的任何容器。 ChartPanel
是一个方便的选择"for displaying a JFreeChart
object."
JFreeChart chart = ChartFactory.createBarChart(…);
this.add(new ChartPanel(chart));
this.pack();
this.setVisible(true);
请注意 pack()
隐式调用 validate()
,因此不需要显式调用。
我有以下代码:但是我似乎无法将我的输出放入我创建的 JPanel 中,我已经尝试过但我可以创建图表并使其显示在单独的图表框架中 window或者它根本没有出现,有人可以帮忙吗?
package javaapplication1;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class JavaApplication1 extends JFrame implements ActionListener {
JPanel panel;
Container window = getContentPane();
ChartFrame cframe;
private ArrayList<Solar> sols;
public static void main(String[] args) throws IOException {
JavaApplication1 demo = new JavaApplication1();
demo.setPreferredSize(new Dimension(1000,500));
demo.createGUI();
demo.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
window.setLayout(new FlowLayout());
panel = new JPanel();
JButton button = new JButton("Get Forecast");
panel.setBackground(Color.white);
panel.setPreferredSize(new Dimension(1000,400));
//ChartFrame cframe = new ChartFrame("First", chart);
window.add(panel);
window.add(button);
button.addActionListener(this);
pack();
}
@Override
public void actionPerformed(ActionEvent e) {
sols = new ArrayList();
BufferedReader crunchifyBuffer = null;
final String DELIMITER = ",";
try {
crunchifyBuffer = new BufferedReader(new FileReader("C:\Users\xxxx\Documents\Molar Forecasts\fs-rad.20170421.21.csv"));
} catch (FileNotFoundException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
String line = null;
try {
line = crunchifyBuffer.readLine(); // Read first line
} catch (IOException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
if (line != null) { try {
// Check it's not null
// line = crunchifyBuffer.readLine(); // Read second line
while ((line = crunchifyBuffer.readLine()) != null)
{
//Get all tokens available in line
String[] tokens = line.split(DELIMITER);
Solar sol = new Solar(tokens[0], tokens[1], tokens[2], Double.parseDouble(tokens[3]));
sols.add(sol);
} } catch (IOException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
processdata();
}}
public void processdata() {
System.out.println(sols.size());
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int index = 1 ; index <sols.size ();index++){
dataset.addValue(sols.get(index).details1(), "Molar Irradiation", (sols.get(index).details()));
}
JFreeChart chart = ChartFactory.createBarChart(
"Bar Chart Demo",
// chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include
true, //
false // URLs?
);
// create and display a frame...
add(panel);
pack();
//panel.add(cframe,BorderLayout.CENTER);
panel.validate();
//
//
setVisible(true);
}
}
在您的示例中,您永远不会 add()
chart
到 JFrame
、JavaApplication1
所包含的任何容器。 ChartPanel
是一个方便的选择"for displaying a JFreeChart
object."
JFreeChart chart = ChartFactory.createBarChart(…);
this.add(new ChartPanel(chart));
this.pack();
this.setVisible(true);
请注意 pack()
隐式调用 validate()
,因此不需要显式调用。