gpt4 book ai didi

java - 将我的 Java FreeChart 放入 JPanel 时遇到问题

转载 作者:行者123 更新时间:2023-11-30 06:44:15 24 4
gpt4 key购买 nike

我有以下代码:但是我似乎无法将输出放入我创建的 JPanel 中,我已经尝试过,但我可以创建图表并使其显示在单独的图表框架窗口中,或者根本不显示, , 有人可以帮忙吗?

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添加到JFrameJavaApplication1所包含的任何容器中>。 ChartPanel是“用于显示 JFreeChart 对象”的便捷选择。

JFreeChart chart = ChartFactory.createBarChart(…);
this.add(new ChartPanel(chart));
this.pack();
this.setVisible(true);

请注意pack()隐式调用 validate(),因此不需要显式调用。

关于java - 将我的 Java FreeChart 放入 JPanel 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43926489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com