2017-05-11 3 views
1

私は次のコードを書いています。私は作成したJPanelに出力を入れているようですが、実験はしましたが、グラフを作成して別のチャートフレームのウィンドウまたはそれはまったく表示されません、誰かを助けることができますか?あなたの例ではJavaのFreeChartを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); 
    } 
     } 
+1

[ChartPanel](http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/ChartPanel.html)の後にあるように見えるのは、いくつかの例がありますページの右側の「関連」セクション – MadProgrammer

+2

など'this.add(新しいChartPanel(チャート))'。 – trashgod

+1

@ trashgod完璧でした! – Ingram

答えて

1

、あなたは決してadd()JFrameJavaApplication1で囲まれた任意の容器にchartChartPanelは "JFreeChartオブジェクトを表示するのに便利な選択です。" pack()が暗黙的validate()を起動するので、明示的な呼び出しが必要になることがshould'tこと

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

注意。

関連する問題